QWidget Subclassed Stylesheet



  • Well I am trying to subclass a QWidget like so. When I try to change the background color, or add a border it doesn't do anything to the widget. If I just set the style to "background-color: black;" all the controls with the layout change background colors.

    I added a QWidget using the designer, than added a button to it. I looked at the generated code and it is exactly what I have here. So I am wondering if there is a problem with sub-classing and QWidget?

    @
    class Base : public QWidget {
    public:
    Base(QWidget* p) : QWiget(p) { }
    }

    class Higher : public Base {
    Q_OBJECT

    public:
    Higher(QWidget* p) : Base(p) {
    this->setObjectName( QString::fromUtf8( "Higher" ) );
    this->setStyleSheet( "#Higher { background-color: black; }" );

          QVBoxLayout* layout = new QVBoxLayout(this);
          // add buttons to layout here etc...
     } 
    

    }
    @

    On a side note, I see "QVBoxLayout* layout = new QVBoxLayout( parent );" but I never see anyone freeing this memory. Does the QWidget free this memory in it's deconstructor?



  • Try adding a Q_OBJECT to your Base class delcaration. A good rule of thumb is to always add the Q_OBJECT macro to all classes that inherit Q_OBJECT.

    Concerning the QVBoxLayout it is a QObject whose ownership belongs to its parent (your widget). It will be destroyed when the widget is itself destroyed. Look at the documentation of QObject for more informations.


Log in to reply
 

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