Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QWidget Subclassed Stylesheet
Qt 6.11 is out! See what's new in the release blog

QWidget Subclassed Stylesheet

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.9k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    exiled
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      rcari
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved