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); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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中设置控件的大小。