1

I've created an QMessageBox with customized buttons and they are showing up in gray as the image bellow: buttons in gray

Running on Linux is fine! But on Raspberry it gives me in trouble. The snippet of code that I wrote is the following:

#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&ccedil;&atilde;o com um ou mais sensores, isso poder&aacute; "
"afetar o desempenho do sistema.</p>"));
    this->setInformativeText(QString("<p style='margin-bottom: 0cm; line-height: 100%; text-align:justify;'><strong>Voc&ecirc;"
" quer continuar ou <span style='color: #ff0000;'>PARAR</span> a aplica&ccedil;&atilde;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()));


}
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • why you didn't handle the style of your message box with Stylesheet? – Parisa.H.R Sep 10 '21 at 22:59
  • In my first attempt, I did It. I'm thinking if there is some relation with the Rasp "style" like Fusion, Windows style. – Iaggo Capitanio Sep 10 '21 at 23:46
  • I mean to use `setStyleSheet` and define your Style, not style like Fusion, Windows-style – Parisa.H.R Sep 11 '21 at 00:13
  • @Parisa.H.R, I got you. I've already tested it. But, I guess that I need to set some thing like this: app.setStyle(QStyleFactory::keys()[1]); – Iaggo Capitanio Sep 13 '21 at 01:42
  • Sorry, Do you test my answer and didn't work???? I add my screenshot too , it works for me. – Parisa.H.R Sep 13 '21 at 01:54
  • @Parisa.H.R, your sugestion didn't work. But I solved it, each OS has some default styles and Qt will search for they to look more "native". Taking that into account I need to force my application to take a Style different from the raspberry standards styles. – Iaggo Capitanio Sep 13 '21 at 14:46
  • @Parisa.H.R, your solution doesn't work, because you are running on Windows and Linux. I'm not using those Operational Systems. How I said, I'm using Raspberry Pi OS. – Iaggo Capitanio Sep 13 '21 at 17:11
  • Yes, I don't try raspberry pi. and it's strange thing because Qt is cross-platform. – Parisa.H.R Sep 13 '21 at 21:13

2 Answers2

2

I try it in Linux and windows, in both of them if you use QPalette they use your OS and System Styles, because of that if you want to have your style it's better to use StyleSheet .

another thing that I see in your code, if you put your code in a constructor it's not required to use this.

I change something of the code and the result is 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&ccedil;&atilde;o com um ou mais sensores, isso poder&aacute; "
"afetar o desempenho do sistema.</p>"));
    this->setInformativeText(QString("<p style='margin-bottom: 0cm; line-height: 100%; text-align:justify;'><strong>Voc&ecirc;"
" quer continuar ou <span style='color: #ff0000;'>PARAR</span> a aplica&ccedil;&atilde;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()));

enter image description here

Parisa.H.R
  • 3,303
  • 3
  • 19
  • 38
0

I solved it, each OS has some default styles and Qt will search for they to look more "native". Taking that into account I need to force my application to take a Style different from the raspberry standards styles. The snipe of code that solved that:

QApplication  app(argc, argv);
qDebug() << QStyleFactory::keys(); //See available styles
app.setStyle("Fusion");