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. Beginners: set external stylesheet for qmessagebox background
Forum Update on Tuesday, May 27th 2025

Beginners: set external stylesheet for qmessagebox background

Scheduled Pinned Locked Moved General and Desktop
16 Posts 3 Posters 12.4k 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
    SherifOmran
    wrote on last edited by
    #1

    Hello guys,

    i am trying to set style sheet (externally) for a message box to change its background colors. At the same time, I have a class that inherits Qdialog and want to do the same with it but I don't know how. For the class, it works with setstylesheet but this is not externally, I hardcode it.

    @
    MyClass *w = new myclass;
    MyClass.setstylesheet="";
    @
    Can any body help me?

    thanks

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

      There are many topics on the same question in the forum, You can have a look "here":http://qt-project.org/forums/viewthread/19691/ that will give you a brief information regarding the implementation of the same.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SherifOmran
        wrote on last edited by
        #3

        I don't find the answer. If i create a new class ex:

        @
        int main(int argc, char *argv[])
        {
        QApplication a(argc, argv);
        myclass w;
        w.exec();
        }
        @

        what should I write in the style sheet to give myclass the style required. Should it be called w or myclass? This does not work with me. Any idea?

        @
        myclass {
        border: 3px solid gray;
        background: QLinearGradient(x1: 0, y1: 0, x2: 0.9, y2: 0.9, stop: 0 #FF0000, stop: 1 #FFFFFF );
        }
        @

        1 Reply Last reply
        0
        • S Offline
          S Offline
          Sam
          wrote on last edited by
          #4

          In order to set the style sheet for your class. You can simply write the stylesheet code in a .qss or .css file. For Eg

          1. Create a file stylesheet.qss and inside the file write the required styles for different widgets that are being used.

          @QFrame
          {
          background-color: rgb(237,236,237);
          }

          QPushButton
          {
          background-color: rgb(230,232,232);
          border-radius: 4px;
          border: 1px solid gray;
          }@

          1. Then next step is that you can either create a resource file and add the stylesheet.qss file to the resource or directly use the path where the file is stored.

          2. Then in the constructor of your class you can read and load the stylesheet file as

          @QFile styleSheet(":/Stylesheet/yourStylesheet.qss"); //path where the file is stored
          if (!styleSheet.open(QIODevice::ReadOnly)
          {
          qWarning("Unable to open ::/Stylesheet/yourStylesheet.qss");
          return;
          }
          qApp->setStyleSheet(styleSheet.readAll());@

          If your class inherits from a QWidget then to make the stylesheet work you need to override the paintEvent() and write

          @void MyClass::paintEvent(QPaintEvent *)
          {
          QStyleOption opt;
          opt.init(this);
          QPainter p(this);
          style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
          }@

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SherifOmran
            wrote on last edited by
            #5

            my class inherits Qdialog. Is it the same, should I override as for QWidget?

            @
            Browse::Browse(QString input_filename, QString App_Path, QWidget *parent) :
            QDialog(parent)
            {
            }
            @

            thanks

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Sam
              wrote on last edited by
              #6

              For the QDialog no need to override the paintEvent().

              1 Reply Last reply
              0
              • S Offline
                S Offline
                SherifOmran
                wrote on last edited by
                #7

                It does not work.

                I added the following in the style sheet editor, my class is called Browse, it is a browse window with listview and tree
                @
                Browse {
                border: 3px solid gray;
                background: QLinearGradient(x1: 0, y1: 0, x2: 0.9, y2: 0.9, stop: 0 #FF0000, stop: 1 #FFFFFF );
                }
                @

                in cpp
                @
                void Browse::paintEvent(QPaintEvent *)
                {
                QStyleOption opt;
                opt.init(this);
                QPainter p(this);
                style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
                }
                @

                in h
                @

                protected:
                void paintEvent(QPaintEvent* event);

                @

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  Sam
                  wrote on last edited by
                  #8

                  If you are subclassing QDialog you need not override paintEvent() .

                  Can you paste the implementation code for your class.
                  Also try #Browse in your stylesheet.qss and check if it works.

                  If you are creating an instance of your class you can also use
                  @yourClassInstance->setStyleSheet(styleSheet.readAll());@

                  1 Reply Last reply
                  0
                  • R Offline
                    R Offline
                    Rahul Das
                    wrote on last edited by
                    #9

                    @w->setstylesheet()@

                    w is the instance. And use -> , and not . (dot) operator for the pointer to the instances.


                    Declaration of (Platform) independence.

                    1 Reply Last reply
                    0
                    • S Offline
                      S Offline
                      SherifOmran
                      wrote on last edited by
                      #10

                      It does not work
                      @
                      Browse * w = new Browse(dB_Users, AppPath);
                      w->setObjectName("myBrowse");
                      Browse.setStyleSheet(styleSheet.readAll());
                      int ret=w->exec();
                      @

                      I tried the following but it does not work either. (Qwidget.style sheet does not have class type)
                      @
                      w->setstylesheet(stylesheet.readall());
                      @

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        Sam
                        wrote on last edited by
                        #11

                        Ok for a test application I have created a dialog that consist of a single pushButton :-

                        .h
                        @class Browse : public QDialog
                        {
                        Q_OBJECT

                        public:
                        explicit Browse(QWidget *parent = 0);
                        ~Browse();

                        private:
                        Ui::Browse *ui;
                        };@

                        .cpp
                        @Browse::Browse(QWidget *parent) :
                        QDialog(parent),
                        ui(new Ui::Browse)
                        {
                        ui->setupUi(this);
                        }

                        Browse::~Browse()
                        {
                        delete ui;
                        }@

                        main.cpp

                        @int main(int argc, char *argv[])
                        {
                        QApplication a(argc, argv);
                        Browse *w = new Browse();

                        QFile styleSheet("C:/temp/style.qss");
                        
                        if (!styleSheet.open(QIODevice::ReadOnly)) {
                            qWarning("Unable to open style.qss");
                            return 0;
                         }
                        w->setStyleSheet(styleSheet.readAll());
                        w->show();
                        
                        
                        return a.exec();
                        

                        }@

                        and for the style.qss file i have specified :-

                        @QDialog{
                        border: 3px solid gray;
                        background-color: qlineargradient(x1: 0, y1: 0, x2: 0.6, y2: 0.6, stop: 0.6 white, stop: 1 gray);
                        }

                        QPushButton {
                        background-color: yellow;
                        border: 2px solid red;
                        }@

                        and here is the output :- !http://img163.imageshack.us/img163/3977/capturehug.png(text)!

                        "Check here":http://imageshack.us/photo/my-images/163/capturehug.png/

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          SherifOmran
                          wrote on last edited by
                          #12

                          Thank you SAM but the difference from my application is that Browse is created from a login class and not from the main window. Could you please test it?

                          • main
                            • login
                              • browse

                          Stylesheet of login works. But for browse not. Also, I don't use a style sheet file, I use the style sheet editor when i open login form and right click, select change style. does this make a difference?

                          1 Reply Last reply
                          0
                          • S Offline
                            S Offline
                            Sam
                            wrote on last edited by
                            #13

                            Just for the test , you can use write the required styles to a .qss file and read the file and use setStyleSheet() .

                            Also for the approach that you are using through the Qt Designer. Try to use

                            @QDialog {
                            background-color: red
                            }@

                            or

                            @#Browse {
                            background-color: red
                            }@

                            but then do not use setStyleSheet() inside the code otherwise it will overwrite the above.

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SherifOmran
                              wrote on last edited by
                              #14

                              Thats what I made, but it does not work on my MAC. I don't use setstylesheet but when I used it once, it worked. Setstylesheet will hardcode it.

                              1 Reply Last reply
                              0
                              • S Offline
                                S Offline
                                SherifOmran
                                wrote on last edited by
                                #15

                                I found the mistake . Thank you guys

                                @
                                Browse * w = new Browse(dB_Users, AppPath);
                                @

                                should have been

                                @
                                Browse * w = new Browse(dB_Users, AppPath,this);
                                @

                                1 Reply Last reply
                                0
                                • S Offline
                                  S Offline
                                  Sam
                                  wrote on last edited by
                                  #16

                                  Great!!!

                                  Kindly Edit your first post and prepend [Solved] to it.

                                  Happy Coding.

                                  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