Qt QMessageBox 显示无颜色按钮
Qt QMessageBox show buttons without color
我创建了一个带有自定义按钮的 QMessageBox,它们显示为灰色,如下图所示:
运行 Linux 没问题!但是在 Raspberry 上它给我带来了麻烦。
我写的代码片段如下:
#include "alertmessage.h"
#include <QDebug>
#include <QAbstractButton>
#include <QCoreApplication>
AlertMessage::AlertMessage(QMessageBox *parent): QMessageBox(parent)
{
this->setFont(QFont("Roboto"));
QFont font = this->font();
font.setPointSize(26);
this->setMaximumHeight(250);
this->setModal(true);
this->setMaximumWidth(this->minimumHeight());
this->setWindowTitle(QString("Falha de conexão"));
this->setChecker(new QCheckBox("Não mostrar essa menssagem novamente.", this));
this->setText(QString("<p style='margin-bottom: 0cm; line-height: 100%; text-align: justify;'>"
"Houve uma falha de comunicação com um ou mais sensores, isso poderá "
"afetar o desempenho do sistema.</p>"));
this->setInformativeText(QString("<p style='margin-bottom: 0cm; line-height: 100%; text-align:justify;'><strong>Você"
" quer continuar ou <span style='color: #ff0000;'>PARAR</span> a aplicação?</strong></p>"));
this->setStandardButtons(QMessageBox::No | QMessageBox::Yes);
this->setButtonText(QMessageBox::No, QString("Parar").toUpper());
this->setButtonText(QMessageBox::Yes, QString("Continuar"));
QPalette okPalette = this->button(QMessageBox::Yes)->palette();
QPalette noPalette = this->button(QMessageBox::No)->palette();
okPalette.setColor(QPalette::Button, QColor(13, 71, 161));
okPalette.setColor(QPalette::ButtonText, QColor(Qt::white));
noPalette.setColor(QPalette::Button, QColor(127, 0, 0));
noPalette.setColor(QPalette::ButtonText, QColor(Qt::white));
this->button(QMessageBox::Yes)->setPalette(okPalette);
this->button(QMessageBox::No)->setPalette(noPalette);
this->setIcon(QMessageBox::Warning);
this->setCheckBox(this->getChecker());
this->connect(this->button(QMessageBox::Yes), SIGNAL(clicked()), this, SLOT(turnVisible()));
this->connect(this->button(QMessageBox::No), SIGNAL(clicked()), this, SLOT(turnOFF()));
}
我在 Linux 和 windows 中尝试过,如果你使用 QPalette
它们都使用你的 OS 和系统样式,因为如果你想要拥有自己的风格,最好使用 StyleSheet .
我在您的代码中看到的另一件事是,如果您将代码放在构造函数中,则不需要使用 this
。
我更改了一些代码,结果是这样的:
setFont(QFont("Roboto"));
setAutoFillBackground(true);
QFont font = this->font();
font.setPointSize(26);
setMaximumHeight(250);
setModal(true);
setMaximumWidth(minimumHeight());
setWindowTitle(QString("Falha de conexão"));
setChecker(new QCheckBox("Não mostrar essa menssagem novamente.", this));
setText(QString("<p style='margin-bottom: 0cm; line-height: 100%; text-align: justify;'>"
"Houve uma falha de comunicação com um ou mais sensores, isso poderá "
"afetar o desempenho do sistema.</p>"));
this->setInformativeText(QString("<p style='margin-bottom: 0cm; line-height: 100%; text-align:justify;'><strong>Você"
" quer continuar ou <span style='color: #ff0000;'>PARAR</span> a aplicação?</strong></p>"));
setStandardButtons(QMessageBox::No | QMessageBox::Yes);
setButtonText(QMessageBox::No, QString("Parar").toUpper());
setButtonText(QMessageBox::Yes, QString("Continuar"));
button(QMessageBox::Yes)->setStyleSheet(QString::fromUtf8("background-color: rgb(13, 71, 161);color:white;"));
button(QMessageBox::No)->setStyleSheet(QString::fromUtf8("background-color: rgb(127, 0, 0);color:white;"));
setIcon(QMessageBox::Warning);
setCheckBox(getChecker());
connect(this->button(QMessageBox::Yes), SIGNAL(clicked()), this, SLOT(turnVisible()));
connect(this->button(QMessageBox::No), SIGNAL(clicked()), this, SLOT(turnOFF()));
我解决了,每个 OS 都有一些默认样式,Qt 会搜索它们以使其看起来更“原生”。考虑到这一点,我需要强制我的应用程序采用不同于覆盆子标准样式的样式。解决该问题的代码:
QApplication app(argc, argv);
qDebug() << QStyleFactory::keys(); //See available styles
app.setStyle("Fusion");
我创建了一个带有自定义按钮的 QMessageBox,它们显示为灰色,如下图所示:
运行 Linux 没问题!但是在 Raspberry 上它给我带来了麻烦。 我写的代码片段如下:
#include "alertmessage.h"
#include <QDebug>
#include <QAbstractButton>
#include <QCoreApplication>
AlertMessage::AlertMessage(QMessageBox *parent): QMessageBox(parent)
{
this->setFont(QFont("Roboto"));
QFont font = this->font();
font.setPointSize(26);
this->setMaximumHeight(250);
this->setModal(true);
this->setMaximumWidth(this->minimumHeight());
this->setWindowTitle(QString("Falha de conexão"));
this->setChecker(new QCheckBox("Não mostrar essa menssagem novamente.", this));
this->setText(QString("<p style='margin-bottom: 0cm; line-height: 100%; text-align: justify;'>"
"Houve uma falha de comunicação com um ou mais sensores, isso poderá "
"afetar o desempenho do sistema.</p>"));
this->setInformativeText(QString("<p style='margin-bottom: 0cm; line-height: 100%; text-align:justify;'><strong>Você"
" quer continuar ou <span style='color: #ff0000;'>PARAR</span> a aplicação?</strong></p>"));
this->setStandardButtons(QMessageBox::No | QMessageBox::Yes);
this->setButtonText(QMessageBox::No, QString("Parar").toUpper());
this->setButtonText(QMessageBox::Yes, QString("Continuar"));
QPalette okPalette = this->button(QMessageBox::Yes)->palette();
QPalette noPalette = this->button(QMessageBox::No)->palette();
okPalette.setColor(QPalette::Button, QColor(13, 71, 161));
okPalette.setColor(QPalette::ButtonText, QColor(Qt::white));
noPalette.setColor(QPalette::Button, QColor(127, 0, 0));
noPalette.setColor(QPalette::ButtonText, QColor(Qt::white));
this->button(QMessageBox::Yes)->setPalette(okPalette);
this->button(QMessageBox::No)->setPalette(noPalette);
this->setIcon(QMessageBox::Warning);
this->setCheckBox(this->getChecker());
this->connect(this->button(QMessageBox::Yes), SIGNAL(clicked()), this, SLOT(turnVisible()));
this->connect(this->button(QMessageBox::No), SIGNAL(clicked()), this, SLOT(turnOFF()));
}
我在 Linux 和 windows 中尝试过,如果你使用 QPalette
它们都使用你的 OS 和系统样式,因为如果你想要拥有自己的风格,最好使用 StyleSheet .
我在您的代码中看到的另一件事是,如果您将代码放在构造函数中,则不需要使用 this
。
我更改了一些代码,结果是这样的:
setFont(QFont("Roboto"));
setAutoFillBackground(true);
QFont font = this->font();
font.setPointSize(26);
setMaximumHeight(250);
setModal(true);
setMaximumWidth(minimumHeight());
setWindowTitle(QString("Falha de conexão"));
setChecker(new QCheckBox("Não mostrar essa menssagem novamente.", this));
setText(QString("<p style='margin-bottom: 0cm; line-height: 100%; text-align: justify;'>"
"Houve uma falha de comunicação com um ou mais sensores, isso poderá "
"afetar o desempenho do sistema.</p>"));
this->setInformativeText(QString("<p style='margin-bottom: 0cm; line-height: 100%; text-align:justify;'><strong>Você"
" quer continuar ou <span style='color: #ff0000;'>PARAR</span> a aplicação?</strong></p>"));
setStandardButtons(QMessageBox::No | QMessageBox::Yes);
setButtonText(QMessageBox::No, QString("Parar").toUpper());
setButtonText(QMessageBox::Yes, QString("Continuar"));
button(QMessageBox::Yes)->setStyleSheet(QString::fromUtf8("background-color: rgb(13, 71, 161);color:white;"));
button(QMessageBox::No)->setStyleSheet(QString::fromUtf8("background-color: rgb(127, 0, 0);color:white;"));
setIcon(QMessageBox::Warning);
setCheckBox(getChecker());
connect(this->button(QMessageBox::Yes), SIGNAL(clicked()), this, SLOT(turnVisible()));
connect(this->button(QMessageBox::No), SIGNAL(clicked()), this, SLOT(turnOFF()));
我解决了,每个 OS 都有一些默认样式,Qt 会搜索它们以使其看起来更“原生”。考虑到这一点,我需要强制我的应用程序采用不同于覆盆子标准样式的样式。解决该问题的代码:
QApplication app(argc, argv);
qDebug() << QStyleFactory::keys(); //See available styles
app.setStyle("Fusion");