/* QButtonGroup 实现QPushButton 互斥管理, 一个按钮按下,其它按钮会被自动弹起, 按下的按钮用背景标蓝表示
* 功能简单,逻辑请看注释,直接上代码了.
*/
#include <QtGui>
QString but_style=QString("\
QPushButton:checked { /*按钮被选择时*/\
background-color:blue;\
}");
int main(int argc, char *argv[])
{
QApplication app(argc,argv);
QPushButton *but1 = new QPushButton("but 1"); //创建按钮
QPushButton *but2 = new QPushButton("but 2");
QPushButton *but3 = new QPushButton("but 3");
but1->setStyleSheet(but_style); //设置按钮style
but2->setStyleSheet(but_style);
but3->setStyleSheet(but_style);
but1->setCheckable(true); //设置按钮可选择
but2->setCheckable(true);
but3->setCheckable(true);
QVBoxLayout *layout = new QVBoxLayout(); //布局管理
layout->addWidget(but1);
layout->addWidget(but2);
layout->addWidget(but3);
QButtonGroup *pushButtonGroup = new QButtonGroup(); //ButtonGroup 没有布局功能,但有互斥组管理功能
pushButtonGroup->setExclusive(true);
pushButtonGroup->addButton(but1); //将按钮加入组
pushButtonGroup->addButton(but2);
pushButtonGroup->addButton(but3);
QWidget *widget = new QWidget(); //显示
widget->setLayout(layout);
widget->show();
return app.exec();
}