Results 1 to 2 of 2

Thread: Q_OBJECT and styleSheet

  1. #1
    Join Date
    Nov 2007
    Posts
    291
    Thanks
    85
    Thanked 1 Time in 1 Post

    Default Q_OBJECT and styleSheet

    hi
    In the following program the stylesheet works for Widget class only when i comment the Q_OBJECT macro.

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui>
    5.  
    6. class Widget:public QWidget
    7. {
    8. Q_OBJECT//if this line is commented the setStyleSheet works.
    9.  
    10. public:
    11. Widget(QWidget *parent=0);
    12. };
    13.  
    14. class MainWindow : public QMainWindow
    15. {
    16. Q_OBJECT
    17.  
    18. public:
    19. MainWindow(QWidget *parent = 0);
    20.  
    21. private:
    22. Widget *widget;
    23. };
    24.  
    25. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp file
    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. Widget::Widget(QWidget *parent) :QWidget(parent)
    4. {
    5. }
    6.  
    7. MainWindow::MainWindow(QWidget *parent)
    8. : QMainWindow(parent)
    9. {
    10. widget = new Widget();
    11. widget->setStyleSheet("background-color:red");
    12. setCentralWidget(widget);
    13. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp file
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    how to set the stylesheet of the Widget Class.

  2. #2
    Join Date
    Jan 2006
    Posts
    667
    Thanks
    10
    Thanked 80 Times in 74 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Q_OBJECT and styleSheet

    Found this from the docs

    QWidget supports only the background, backgorund-clip and background-origin properties.
    If you subclass from QWidget, you need to provide a paintEvent for your custom QWidget as below:
    void CustomWidget:aintEvent(QPaintEvent *)
    {
    QStyleOption opt;
    opt.init(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    }
    The above code is a no-operation if there is no stylesheet set.
    Warning: Make sure you define the Q_OBJECT macro for your custom widget.
    May be this can help.

  3. The following user says thank you to munna for this useful post:

    babu198649 (9th December 2008)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.