How to retrieve the QPalette colors for the css ::hover state?
-
I write this minimal example that prints the
QPalette
colors for the "states","default"
,hover
anddisabled
, but for thehover
state it didnt retrieve the colors correctly.int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget* widget = new QWidget(); widget->setObjectName("widget"); QSpinBox* spinBox = new QSpinBox(); spinBox->setObjectName("spinBox"); QTextEdit* textEdit = new QTextEdit(); textEdit->setFixedSize(60, 20); QHBoxLayout* layout = new QHBoxLayout(widget); layout->addWidget(spinBox); layout->addWidget(textEdit); // random widget to have something else to grab focus widget->setStyleSheet(R"( #spinBox { background-color: rgba(1, 1, 1, 255); color: rgba(1, 1, 1, 255); } #spinBox::hover { background-color: rgba(2, 2, 2, 255); color: rgba(2, 2, 2, 255); } #spinBox::disabled { background-color: rgba(3, 3, 3, 255); color: rgba(3, 3, 3, 255); } )"); widget->show(); static const auto colorToString = [](const QColor& color) -> QString { return QString("rgba: %1, %2, %3, %4").arg(color.red()).arg(color.green()).arg(color.blue()).arg(color.alpha()); }; static const auto printPallete = [](const QPalette& pal) { #define qq qDebug().noquote().nospace() qq << "\n ------ state ------\n" << pal.currentColorGroup(); //qq << "\nWindowText:\n" << colorToString(pal.color(QPalette::WindowText)); //qq << "\nButton:\n" << colorToString(pal.color(QPalette::Button)); qq << "\color\n" << colorToString(pal.color(QPalette::ButtonText)); //qq << "\nLight:\n" << colorToString(pal.color(QPalette::Light)); //qq << "\nDark:\n" << colorToString(pal.color(QPalette::Dark)); //qq << "\nMidlight:\n" << colorToString(pal.color(QPalette::Midlight)); //qq << "\nText:\n" << colorToString(pal.color(QPalette::Text)); //qq << "\nBrightText:\n" << colorToString(pal.color(QPalette::BrightText)); //qq << "\nBase:\n" << colorToString(pal.color(QPalette::Base)); qq << "\nbackground-color\n" << colorToString(pal.color(QPalette::Window)); //qq << "\nShadow:\n" << colorToString(pal.color(QPalette::Shadow)); //qq << "\nHighlight:\n" << colorToString(pal.color(QPalette::Highlight)); //qq << "\nHighlightedText:\n" << colorToString(pal.color(QPalette::HighlightedText)); //qq << "\nAlternateBase:\n" << colorToString(pal.color(QPalette::AlternateBase)); //qq << "\nPlaceholderText:\n" << colorToString(pal.color(QPalette::PlaceholderText)); //qq << "\nAccent:\n" << colorToString(pal.color(QPalette::Accent)); }; QObject::connect(new QShortcut(QKeySequence(Qt::Key_F1), widget), &QShortcut::activated, [&] { spinBox->setEnabled(true); printPallete(spinBox->palette()); }); QObject::connect(new QShortcut(QKeySequence(Qt::Key_F2), widget), &QShortcut::activated, [&] { spinBox->setEnabled(false); printPallete(spinBox->palette()); }); return a.exec(); }
Pressing the
F1
shortcut while theQSpinBox
is nothovered
:------ state ------ QPalette::Active color rgba: 1, 1, 1, 255 background-color rgba: 1, 1, 1, 255
Pressing the
F1
shortcut while hovering theQSpinBox
:------ state ------ QPalette::Active color rgba: 1, 1, 1, 255 background-color rgba: 1, 1, 1, 255
It should be
2, 2, 2
as the stylesheet:#spinBox::hover { background-color: rgba(2, 2, 2, 255); color: rgba(2, 2, 2, 255); }
Hitting
F2
the shortcut disables the widget, reading the palette data it prints:------ state ------ QPalette::Disabled color rgba: 3, 3, 3, 255 background-color rgba: 3, 3, 3, 255
How i could read the
hover
colors for thetext
andbackground
of aQWidget
styled usingstylesheet
? -
I write this minimal example that prints the
QPalette
colors for the "states","default"
,hover
anddisabled
, but for thehover
state it didnt retrieve the colors correctly.int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget* widget = new QWidget(); widget->setObjectName("widget"); QSpinBox* spinBox = new QSpinBox(); spinBox->setObjectName("spinBox"); QTextEdit* textEdit = new QTextEdit(); textEdit->setFixedSize(60, 20); QHBoxLayout* layout = new QHBoxLayout(widget); layout->addWidget(spinBox); layout->addWidget(textEdit); // random widget to have something else to grab focus widget->setStyleSheet(R"( #spinBox { background-color: rgba(1, 1, 1, 255); color: rgba(1, 1, 1, 255); } #spinBox::hover { background-color: rgba(2, 2, 2, 255); color: rgba(2, 2, 2, 255); } #spinBox::disabled { background-color: rgba(3, 3, 3, 255); color: rgba(3, 3, 3, 255); } )"); widget->show(); static const auto colorToString = [](const QColor& color) -> QString { return QString("rgba: %1, %2, %3, %4").arg(color.red()).arg(color.green()).arg(color.blue()).arg(color.alpha()); }; static const auto printPallete = [](const QPalette& pal) { #define qq qDebug().noquote().nospace() qq << "\n ------ state ------\n" << pal.currentColorGroup(); //qq << "\nWindowText:\n" << colorToString(pal.color(QPalette::WindowText)); //qq << "\nButton:\n" << colorToString(pal.color(QPalette::Button)); qq << "\color\n" << colorToString(pal.color(QPalette::ButtonText)); //qq << "\nLight:\n" << colorToString(pal.color(QPalette::Light)); //qq << "\nDark:\n" << colorToString(pal.color(QPalette::Dark)); //qq << "\nMidlight:\n" << colorToString(pal.color(QPalette::Midlight)); //qq << "\nText:\n" << colorToString(pal.color(QPalette::Text)); //qq << "\nBrightText:\n" << colorToString(pal.color(QPalette::BrightText)); //qq << "\nBase:\n" << colorToString(pal.color(QPalette::Base)); qq << "\nbackground-color\n" << colorToString(pal.color(QPalette::Window)); //qq << "\nShadow:\n" << colorToString(pal.color(QPalette::Shadow)); //qq << "\nHighlight:\n" << colorToString(pal.color(QPalette::Highlight)); //qq << "\nHighlightedText:\n" << colorToString(pal.color(QPalette::HighlightedText)); //qq << "\nAlternateBase:\n" << colorToString(pal.color(QPalette::AlternateBase)); //qq << "\nPlaceholderText:\n" << colorToString(pal.color(QPalette::PlaceholderText)); //qq << "\nAccent:\n" << colorToString(pal.color(QPalette::Accent)); }; QObject::connect(new QShortcut(QKeySequence(Qt::Key_F1), widget), &QShortcut::activated, [&] { spinBox->setEnabled(true); printPallete(spinBox->palette()); }); QObject::connect(new QShortcut(QKeySequence(Qt::Key_F2), widget), &QShortcut::activated, [&] { spinBox->setEnabled(false); printPallete(spinBox->palette()); }); return a.exec(); }
Pressing the
F1
shortcut while theQSpinBox
is nothovered
:------ state ------ QPalette::Active color rgba: 1, 1, 1, 255 background-color rgba: 1, 1, 1, 255
Pressing the
F1
shortcut while hovering theQSpinBox
:------ state ------ QPalette::Active color rgba: 1, 1, 1, 255 background-color rgba: 1, 1, 1, 255
It should be
2, 2, 2
as the stylesheet:#spinBox::hover { background-color: rgba(2, 2, 2, 255); color: rgba(2, 2, 2, 255); }
Hitting
F2
the shortcut disables the widget, reading the palette data it prints:------ state ------ QPalette::Disabled color rgba: 3, 3, 3, 255 background-color rgba: 3, 3, 3, 255
How i could read the
hover
colors for thetext
andbackground
of aQWidget
styled usingstylesheet
?@Christiian said in How to retrieve the QPalette colors for the css ::hover state?:
How i could read the hover colors for the text and background of a QWidget styled using stylesheet?
Unsatisfactory answer:
Trystylesheet()
.But the new colors/style when hovering the widget apply, right?!