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. Set a stylesheet for QColorDialog
Forum Updated to NodeBB v4.3 + New Features

Set a stylesheet for QColorDialog

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 4 Posters 1.2k 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.
  • S Offline
    S Offline
    Sucharek
    wrote on last edited by
    #1

    Hi, I'm wondering if you can set a stylesheet for a QColorDialog. I've tried something, but it doesn't work. Here's what I tried:

    QColorDialog pick;
    pick.setStyleSheet("background-color: black;");
    QColor color = pick.getColor();
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sucharek
      wrote on last edited by Sucharek
      #8

      Hi, so I just now realised, that I've made a mistake in my code example. Everytime you click cancel the color that it returns is black.
      EDIT: It's a problem because I'm setting a widgets color, and when I click cancel, it sets without me wanting it to.
      Here it's fixed:

      QString color;
      QColorDialog pick;
      pick.setStyleSheet("stylesheet"); 
      pick.setCurrentColor("10, 255, 10"); //optional
      if (pick.exec() == QColorDialog::Accepted) {
          QColor const color  = pick.selectedColor().name();
          color = color.name();
          qDebug() << color;
          }
      

      Another EDIT: Here's an example of 2 different stylesheets:
      Light mode:

      QColorDialog {background-color: rgb(244, 244, 244);}
      QPushButton {background-color: rgb(244, 244, 244); color: black;}
      QLabel {background-color: rgb(244, 244, 244); color: black;} 
      QLineEdit {background-color: white; color: black;}
      QSpinBox {background-color: white; color: black;}
      

      Dark mode:

      QColorDialog {background-color: rgb(6, 6, 14);}
      QPushButton {color: rgb(211, 213, 201); background-color: rgb(36, 36, 44);}
      QLabel {color: rgb(211, 213, 201); background-color: rgb(6, 6, 14);}
      QLineEdit {color: rgb(211, 213, 201); background-color: rgb(36, 36, 44);}
      QSpinBox {color: rgb(211, 213, 201); background-color: rgb(36, 36, 44);}
      
      1 Reply Last reply
      2
      • Christian EhrlicherC Online
        Christian EhrlicherC Online
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        Since QColorDialog::getColor() is a static function you won't be able to set a stylesheet when you want to use getColor(). Use a non-static function instead.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        S 1 Reply Last reply
        3
        • Christian EhrlicherC Christian Ehrlicher

          Since QColorDialog::getColor() is a static function you won't be able to set a stylesheet when you want to use getColor(). Use a non-static function instead.

          S Offline
          S Offline
          Sucharek
          wrote on last edited by
          #3

          @Christian-Ehrlicher, ok that works, but how do I get the selected color now?

          JoeCFDJ 1 Reply Last reply
          0
          • S Sucharek

            @Christian-Ehrlicher, ok that works, but how do I get the selected color now?

            JoeCFDJ Offline
            JoeCFDJ Offline
            JoeCFD
            wrote on last edited by
            #4

            @Sucharek https://doc.qt.io/qt-5/qcolordialog.html#colorSelected

            S 1 Reply Last reply
            2
            • JoeCFDJ JoeCFD

              @Sucharek https://doc.qt.io/qt-5/qcolordialog.html#colorSelected

              S Offline
              S Offline
              Sucharek
              wrote on last edited by
              #5

              Hi @JoeCFD, that didn't work for me, but I know I'm doing it wrong, but I don't know how to make it right. It says that I'm "missing emit keyword on signal call". Am I supposed to QObject::conect or something? If yes, please provide an example, because I don't know how to properly use that.

              JonBJ 1 Reply Last reply
              0
              • S Sucharek

                Hi @JoeCFD, that didn't work for me, but I know I'm doing it wrong, but I don't know how to make it right. It says that I'm "missing emit keyword on signal call". Am I supposed to QObject::conect or something? If yes, please provide an example, because I don't know how to properly use that.

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6

                @Sucharek
                QColor QColorDialog::selectedColor() const

                Returns the color that the user selected by clicking the OK or equivalent button.

                S 1 Reply Last reply
                2
                • JonBJ JonB

                  @Sucharek
                  QColor QColorDialog::selectedColor() const

                  Returns the color that the user selected by clicking the OK or equivalent button.

                  S Offline
                  S Offline
                  Sucharek
                  wrote on last edited by
                  #7

                  Hi @JonB, thanks, it works perfectly.
                  If anyone wants to use it, here's a snippet for it:

                  QColorDialog pick;
                  pick.setStyleSheet("stylesheet");
                  pick.setCurrentColor("10, 255, 10"); //optional
                  pick.exec();
                  QColor const color  = pick.selectedColor().name();
                  if (!color.isValid()) return;
                     QString selectedCol = color.name();
                  qDebug() << selectedCol;
                  
                  1 Reply Last reply
                  1
                  • S Offline
                    S Offline
                    Sucharek
                    wrote on last edited by Sucharek
                    #8

                    Hi, so I just now realised, that I've made a mistake in my code example. Everytime you click cancel the color that it returns is black.
                    EDIT: It's a problem because I'm setting a widgets color, and when I click cancel, it sets without me wanting it to.
                    Here it's fixed:

                    QString color;
                    QColorDialog pick;
                    pick.setStyleSheet("stylesheet"); 
                    pick.setCurrentColor("10, 255, 10"); //optional
                    if (pick.exec() == QColorDialog::Accepted) {
                        QColor const color  = pick.selectedColor().name();
                        color = color.name();
                        qDebug() << color;
                        }
                    

                    Another EDIT: Here's an example of 2 different stylesheets:
                    Light mode:

                    QColorDialog {background-color: rgb(244, 244, 244);}
                    QPushButton {background-color: rgb(244, 244, 244); color: black;}
                    QLabel {background-color: rgb(244, 244, 244); color: black;} 
                    QLineEdit {background-color: white; color: black;}
                    QSpinBox {background-color: white; color: black;}
                    

                    Dark mode:

                    QColorDialog {background-color: rgb(6, 6, 14);}
                    QPushButton {color: rgb(211, 213, 201); background-color: rgb(36, 36, 44);}
                    QLabel {color: rgb(211, 213, 201); background-color: rgb(6, 6, 14);}
                    QLineEdit {color: rgb(211, 213, 201); background-color: rgb(36, 36, 44);}
                    QSpinBox {color: rgb(211, 213, 201); background-color: rgb(36, 36, 44);}
                    
                    1 Reply Last reply
                    2
                    • S Offline
                      S Offline
                      Sucharek
                      wrote on last edited by
                      #9
                      This post is deleted!
                      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