Beginners: set external stylesheet for qmessagebox background



  • 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



  • 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.



  • 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 );
    }
    @



  • 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);
    }@



  • 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



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



  • 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);

    @



  • 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());@



  • @w->setstylesheet()@

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



  • 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());
    @



  • 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/



  • 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?



  • 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.



  • 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.



  • 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);
    @



  • Great!!!

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

    Happy Coding.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.