代码之家  ›  专栏  ›  技术社区  ›  LeftFrog

Qt中macOS上的按钮颜色不变

  •  1
  • LeftFrog  · 技术社区  · 1 年前

    我正在尝试将QPushButton的颜色更改为QPalette::Accent,但颜色不变。

    起初我试着这样做:

     QPushButton button("Apply");
     QPalette palette = QPalette();
     palette.setColor(QPalette::Button, palette.color(QPalette::Accent));
     button.setPalette(palette);
     button.setAutoFillBackground(true);
    

    但我只知道:

    button1

    当我尝试使用样式表时,我得到了一个失去macOS风格的按钮

    QPushButton* button = new QPushButton("open");
     button->setStyleSheet("background-color: rgb(255,255,0);");
    

    button2

    但我想达到这样的结果:

    button3

    我为这个愚蠢的问题道歉,但我已经搜索了整个互联网,我不知道该怎么做。

    1 回复  |  直到 1 年前
        1
  •  0
  •   ypnos    1 年前

    MacOS中的蓝色按钮,如您的示例“button3”中所示,表示这是默认按钮。因此,例如按下Enter键将按下此按钮。

    要实现相同的行为(和外观),只需调用 button.setDefault(true) 。这可能仅限于对话框窗口。

    将按钮染成蓝色而不将其设为默认按钮会误导用户。