Hello. I want to make different icons for checked and unchecked state of QPushButton. The code:
MainWindow
::MainWindow(QWidget *parent
){
widget->setLayout(layout1);
setCentralWidget(widget);
icon1.
addFile(QStringLiteral
(":/norecord.png"),
QSize(),
QIcon::Normal,
QIcon::Off);
icon1.
addFile(QStringLiteral
(":/active.png"),
QSize(),
QIcon::Normal,
QIcon::On);
button1->setCheckable(true);
button1->setText("Sample Text");
button1->setIcon(icon1);
layout1->addWidget(button1);
}
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
QBoxLayout *layout1 = new QVBoxLayout();
QWidget *widget = new QWidget(this);
widget->setLayout(layout1);
setCentralWidget(widget);
QIcon icon1;
icon1.addFile(QStringLiteral(":/norecord.png"), QSize(), QIcon::Normal, QIcon::Off);
icon1.addFile(QStringLiteral(":/active.png"), QSize(), QIcon::Normal, QIcon::On);
QPushButton *button1 = new QPushButton(this);
button1->setCheckable(true);
button1->setText("Sample Text");
button1->setIcon(icon1);
layout1->addWidget(button1);
}
To copy to clipboard, switch view to plain text mode
I use Qt 5.15.2. This code works in Windows, but not in Linux and Android, there always shows an icon for a unchecked state.
On Qt 5.11.3 and earlier versions it worked.
As far as I could understand, the problem causes this code in the Fusion style, file src/widgets/styles/qfusionstyle.cpp:
case CE_PushButtonLabel:
// no PM_ButtonShiftHorizontal and PM_ButtonShiftVertical for fusion style
b.state &= ~(State_On | State_Sunken);
}
break;
case CE_PushButtonLabel:
if (const QStyleOptionButton *button = qstyleoption_cast<const QStyleOptionButton *>(option)) {
QStyleOptionButton b(*button);
// no PM_ButtonShiftHorizontal and PM_ButtonShiftVertical for fusion style
b.state &= ~(State_On | State_Sunken);
QCommonStyle::drawControl(element, &b, painter, widget);
}
break;
To copy to clipboard, switch view to plain text mode
I want to ask: Is it a correct code? Is it a bug or style feature?
Bookmarks