qt 按钮互斥效果

复制代码
void SideBar::setBtnVec(QVector<QPushButton*> btnVec, const char *name)
{
    m_propertyName = name;
    m_btnVec = btnVec;

    for (int i = 0; i < m_btnVec.count(); i++)
    {

        m_scrollLayout->addWidget(m_btnVec.at(i));
        connect(m_btnVec.at(i),&QPushButton::clicked,this,[&](){
           auto * btn =  (QPushButton*)sender();
           qDebug() << "点击侧边栏按钮,切换页: " << btn->property(m_propertyName).toInt();
            emit signalSwitchPage(i);

        });
        connect(m_btnVec.at(i),&QPushButton::clicked,this,[&](){
            // 选择按钮后的背景颜色
            auto * btn =  (QPushButton*)sender();
            btn->setStyleSheet(
                "QPushButton {"
                "    background-color: rgba(153, 152, 156, 100);"  // 背景色
                "    color: white;"                // 字体颜色
                "    border-radius: 5px;"           //边框圆角
                "    padding: 5px 10px;"           //内边距
                "}"
            );
            // 互斥效果
            for (int i = 0; i < m_btnVec.count(); i++)
            {
                if (m_btnVec.at(i) != btn)
                {
                    m_btnVec.at(i)->setStyleSheet(
                        "QPushButton {"
                        "    background-color: rgba(0,0,0,0);"  // 背景色
                        "    color: white;"                // 字体颜色
                        //"    border: 2px solid #4CAF50;"    边框颜色和宽度
                        "    border-radius: 5px;"           //边框圆角
                        "    padding: 5px 10px;"           //内边距
                        //"    font-size: 16px;"              字体大小
                        "}"
                        "QPushButton:hover {"
                        "    background-color: rgba(210, 209, 211, 100);"  // 鼠标悬停时背景色
                        "    border-radius: 5px;"           //边框圆角
                        "}"
                        "QPushButton:pressed {"
                        "    background-color: rgba(153, 152, 156, 100);"  // 按下时背景色
                        "    border-radius: 5px;"           //边框圆角
                        "}"
                    );
                }
            }
        });

        // 设置按钮的样式
        m_btnVec.at(i)->setStyleSheet(
            "QPushButton {"
            "    background-color: rgba(0,0,0,0);"  // 背景色
            "    color: white;"                // 字体颜色
            //"    border: 2px solid #4CAF50;"    边框颜色和宽度
            "    border-radius: 5px;"           //边框圆角
            "    padding: 5px 10px;"           //内边距
            //"    font-size: 16px;"              字体大小
            "}"
            "QPushButton:hover {"
            "    background-color: rgba(210, 209, 211, 100);"  // 鼠标悬停时背景色
            "    border-radius: 5px;"           //边框圆角
            "}"
            "QPushButton:pressed {"
            "    background-color: rgba(153, 152, 156, 100);"  // 按下时背景色
            "    border-radius: 5px;"           //边框圆角
            "}"
        );
    }
    // 给布局添加弹簧
    m_scrollLayout->addStretch(0);

    // 将布局添加到侧边栏主控件中
    m_contentWidget->setLayout(m_scrollLayout);

    // 将中央主控件添加到滚动区域窗口中
    m_scrollArea->setWidget(m_contentWidget);
}
复制代码

 

 

 

 

posted @ 2025-02-12 10:32  雾枫  阅读(0)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek-R1本地部署如何选择适合你的版本?看这里
· 开源的 DeepSeek-R1「GitHub 热点速览」
· 传国玉玺易主,ai.com竟然跳转到国产AI
· 揭秘 Sdcb Chats 如何解析 DeepSeek-R1 思维链
· 自己如何在本地电脑从零搭建DeepSeek!手把手教学,快来看看! (建议收藏)
历史上的今天:
2024-02-12 Qt qss 设置字体
2024-02-12 Qt 样式 通过qss文件设置QLabel标签样式,设置背景图片,图片自适应控件大小,在qss中设置控件的大小。
点击右上角即可分享
微信分享提示