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
Qt 6.11 is out! See what's new in the release blog

Beginners: set external stylesheet for qmessagebox background

Scheduled Pinned Locked Moved General and Desktop
16 Posts 3 Posters 13.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
    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