QPushButton有一个checked
状态,可以在按钮被选中时触发该状态。可以使用QSS(Qt样式表)来设置QPushButton的checked状态样式。
以下是一个示例代码:
QPushButton *button = new QPushButton("Check me");
button->setCheckable(true);
button->setFixedSize(100, 50);
button->setStyleSheet("QPushButton { "
"background-color: blue;"
"color: white;"
"}"
"QPushButton:checked { "
"background-color: red;"
"color: black;"
"}");
其中,setCheckable(true)
方法可以将QPushButton设置为可选状态。在QSS中,使用QPushButton:checked
选择器来设置按钮被选中时的样式。在以上示例中,当按钮被选中时,背景颜色变为红色,文字颜色变为黑色。
需要注意的是,在使用QSS设置样式时,需要使用分号;
来结束每个属性的设置。