Qt QPushButton启用检查时更改大小

时间:2019-07-12 08:06:42

标签: c++ qt user-interface

我在QPushButton上有两个QGridLayout,其中一个按钮处于选中状态,另一个未被选中:

class MyFrame : QFrame()
{
    MyFrame() { init(); }

    void init()
    {
        setStyleSheet("QPushButton {background-color:#292929; color: white;} QPushButton:hover {background-color: #505050; color: white;} QPushButton:pressed{background-color: #F79862;} QPushButton:checked{background-color: #F79862;border:none;} QToolTip{ color: #404040; }");

        auto layout = new QGridLayout();
        auto btn1 = new QPushButton("btn1");
        auto btn2 = new QPushButton("btn2");

        btn1->setCheckable(true);
        btn2->setCheckable(true);

        btn1->setChecked(false);
        btn2->setChecked(true);

        layout->addWidget(btn1, 0, 0, 1, 1);
        layout->addWidget(btn2, 0, 1, 1, 1);
    }
}

QVBoxLayout* grid = new QVBoxLayout();
MyFrame* myFrame = new MyFrame();
grid->addWidget(myFrame);

但是,当我运行该程序时,一个按钮似乎比另一个按钮大。如何使按钮大小相似?

enter image description here

我正在使用Qt 5.11.2。

1 个答案:

答案 0 :(得分:0)

问题出在您的styleSheet中,只需从border:none;中删除QPushButton:checked

setStyleSheet("QPushButton {"
              "   background-color:#292929; "
              "   color: white;"
              "} "
              ""
              "QPushButton:hover {"
              "   background-color: #505050; "
              "   color: white;"
              "} "
              ""
              "QPushButton:pressed {"
              "   background-color: #F79862;"
              "} "
              ""
              "QPushButton:checked {"
              "   background-color: #F79862;"
              "} "
              ""
              "QToolTip { "
              "   color: #404040; "
              "}");

这是结果:

Solution image