Results 1 to 2 of 2

Thread: Set CSS in QPushButton's subclass's constructor

  1. #1
    Join Date
    Oct 2015
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Set CSS in QPushButton's subclass's constructor

    I'm creating my custom push button class by subclassing QPushButton. However for some reason setting that class's CSS in its constructor has no effect; I have to do it in for example paintEvent, then everything is fine. I could just have a global .qss file and set it for the entire application, but I want the class to manage its own styles. Why doesn't my approach work?

    Here is some code:

    .h
    Qt Code:
    1. class CustomPushButton: public QPushButton
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit CustomPushButton(QWidget *parent = 0);
    7. ~CustomPushButton() = default;
    8. };
    To copy to clipboard, switch view to plain text mode 

    .cpp
    Qt Code:
    1. CustomPushButton::CustomPushButton(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. setStyleSheet("background-color: black;"); // this does not work
    5. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jun 2015
    Location
    India
    Posts
    185
    Thanks
    8
    Thanked 14 Times in 14 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Set CSS in QPushButton's subclass's constructor

    It worked for me when I changed constructor to
    //your code did not even get compiled on my machine

    CustomPushButton::CustomPushButton(QWidget *parent) : QPushButton(parent)
    Last edited by prasad_N; 4th October 2015 at 21:19.
    Thanks :-)

Similar Threads

  1. subclass qpushbutton
    By Cremers in forum Newbie
    Replies: 6
    Last Post: 23rd June 2013, 23:17
  2. Replies: 2
    Last Post: 15th April 2013, 07:33
  3. Replies: 8
    Last Post: 12th February 2010, 03:41
  4. Replies: 2
    Last Post: 24th July 2008, 21:34

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.