Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to retrieve the QPalette colors for the css ::hover state?
Forum Updated to NodeBB v4.3 + New Features

How to retrieve the QPalette colors for the css ::hover state?

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 310 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Christiian
    wrote on last edited by
    #1

    I write this minimal example that prints the QPalette colors for the "states", "default", hover and disabled, but for the hover 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 the QSpinBox is not hovered:

    ------ state ------
    QPalette::Active
    color
    rgba: 1, 1, 1, 255
    
    background-color
    rgba: 1, 1, 1, 255
    

    Pressing the F1 shortcut while hovering the QSpinBox:

     ------ 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 the text and background of a QWidget styled using stylesheet?

    Pl45m4P 1 Reply Last reply
    0
    • C Christiian

      I write this minimal example that prints the QPalette colors for the "states", "default", hover and disabled, but for the hover 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 the QSpinBox is not hovered:

      ------ state ------
      QPalette::Active
      color
      rgba: 1, 1, 1, 255
      
      background-color
      rgba: 1, 1, 1, 255
      

      Pressing the F1 shortcut while hovering the QSpinBox:

       ------ 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 the text and background of a QWidget styled using stylesheet?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #2

      @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:
      Try stylesheet().

      But the new colors/style when hovering the widget apply, right?!


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved