Results 1 to 8 of 8

Thread: How to use stylesheet of custom widget

  1. #1
    Join Date
    May 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to use stylesheet of custom widget

    Hi all,

    I made my custom widget based on QWidget.

    I use stylesheet like this:

    customWidget->setStyleSheet("QWidget { border: none; background-color: rgb(195, 198, 201);} QWidget:hover {background-color: rgb(255, 0, 0);"}

    But it has no effect, so why and how to perform?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to use stylesheet of custom widget

    Maybe you want to replace QWidget with the class name of your subclassed widget.

  3. #3
    Join Date
    May 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use stylesheet of custom widget

    Quote Originally Posted by Lykurg View Post
    Maybe you want to replace QWidget with the class name of your subclassed widget.
    I tried to use the class name of my subclass, Unfortunately, It is no effect either. But thanks for your heartful.

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to use stylesheet of custom widget

    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0): QWidget(parent)
    5. {}
    6. };
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11.  
    12. QString css = "background-color: #ffA500; border: 5px solid #FF0000;";
    13.  
    14. MyWidget b;
    15. b.setAutoFillBackground(true);
    16. b.setStyleSheet(css);
    17. b.show();
    18.  
    19. return app.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 
    Works for me. If you have your own paint event use something like
    Qt Code:
    1. void paintEvent(QPaintEvent *e)
    2. {
    3. QWidget::paintEvent(e); // draws the background etc.
    4. //or use
    5. QStylePainter p(this);
    6. //to draw your needed elements.
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    May 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use stylesheet of custom widget

    Quote Originally Posted by Lykurg View Post
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0): QWidget(parent)
    5. {}
    6. };
    To copy to clipboard, switch view to plain text mode 
    OK, your code works fine in my system too.
    But if you add the Q_OBJECT in subclass define, no effect either.
    Qt Code:
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. MyWidget(QWidget *parent = 0): QWidget(parent) {}
    6. };
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication app(argc, argv);
    11.  
    12. QString css = "background-color: #ffA500; border: 5px solid #FF0000;";
    13.  
    14. MyWidget b;
    15. b.setAutoFillBackground(true);
    16. b.setStyleSheet(css);
    17. b.show();
    18.  
    19. return app.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to use stylesheet of custom widget

    Qt Code:
    1. #include <QtGui>
    2.  
    3. class MyWidget : public QWidget
    4. {
    5. Q_OBJECT
    6. public:
    7. MyWidget(QWidget *parent = 0): QWidget(parent) {}
    8. };
    9.  
    10. int main(int argc, char *argv[])
    11. {
    12. QApplication app(argc, argv);
    13.  
    14. QString css = "background-color: #ffA500;";
    15.  
    16. MyWidget b;
    17. b.setAutoFillBackground(true);
    18. b.setStyleSheet(css);
    19. b.show();
    20.  
    21. return app.exec();
    22. }
    23.  
    24. #include "main.moc"
    To copy to clipboard, switch view to plain text mode 
    does draw a orange background on my machine. Have you rerun qmake?

  7. #7
    Join Date
    May 2010
    Posts
    19
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use stylesheet of custom widget

    Quote Originally Posted by Lykurg View Post
    does draw a orange background on my machine. Have you rerun qmake?
    My machine still no effect if add Q_OBJECT.
    Sorry, what did you mean of "rerun qmake"? I am a beginner, just install the Qt Creator and write code. How to rerun qmake? Thanks a lot.

  8. #8
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How to use stylesheet of custom widget

    I don't know how it is called on the english ui, but have a look at the menu bar "compilation" or something like that. Call "clean all" and "run qmake" and "compile". Or on the qt command promt type (in the project/build directory)
    make clean
    del Makefile*
    qmake
    make

Similar Threads

  1. Using custom Q_PROPERTY with Stylesheet
    By hubbobubbo in forum Qt Programming
    Replies: 7
    Last Post: 30th September 2010, 10:48
  2. Replies: 2
    Last Post: 21st March 2010, 08:20
  3. Usability of StyleSheet in Pure Custom Widget Plugin?
    By umituzun84 in forum Qt Programming
    Replies: 2
    Last Post: 5th March 2010, 14:22
  4. Replies: 1
    Last Post: 5th November 2006, 23:50
  5. Replies: 9
    Last Post: 8th May 2006, 14:21

Tags for this Thread

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.