Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Subclass doesn't work



  • In my project I have few vertical line so instead of writing:

        QWidget *vertical_line_1 = new QWidget;
        vertical_line_1->setFixedHeight(1);
        vertical_line_1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        vertical_line_1->setStyleSheet("background-color: black;");
    

    every time i made subclass:

    class VVerticalLine : public QWidget
    {
       Q_OBJECT
    public:
       VVerticalLine(QWidget *parent = 0) : QWidget(parent) {
           setFixedHeight(1);
           setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
           setStyleSheet("background-color: black;");
       }
       ~VVerticalLine() {}
    };
    

    unfortunately, lines created in this way don't appear
    Can someone take a look, and tell me where I make a mistake?


  • Lifetime Qt Champion

    Ok, you have to set Qt::WA_StyledBackground attribute by yourself when you don't use a plain QWidget.


  • Lifetime Qt Champion

    Please provide a minimal, compilable example on how you use your class when it does not work.



  • mainwindow.h:

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QGridLayout>
    #include <QScreen>
    
    class VVerticalLine : public QWidget
    {
       Q_OBJECT
    public:
       VVerticalLine(QWidget *parent = 0) : QWidget(parent) {
           setFixedHeight(10);
           setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
           setStyleSheet("background-color: black;");
       }
       ~VVerticalLine() {}
    };
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow();
        ~MainWindow();
    
        QWidget *vertical_line_1 = new QWidget;
        VVerticalLine *vertical_line_2 = new VVerticalLine;
    
        QWidget *widget = new QWidget;
        QGridLayout *layout = new QGridLayout;
    };
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp:

    #include "mainwindow.h"
    
    MainWindow::MainWindow()
    {
        showMaximized();
        setGeometry(screen()->geometry());
    
        vertical_line_1->setFixedHeight(10);
        vertical_line_1->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
        vertical_line_1->setStyleSheet("background-color: black;");
    
        layout->addWidget(vertical_line_1, 0, 0);
        layout->addWidget(vertical_line_2, 0, 1);
    
        widget->setLayout(layout);
        setCentralWidget(widget);
    }
    
    MainWindow::~MainWindow(){}
    
    

    It should be clear enough.


  • Lifetime Qt Champion

    Thx, can confirm this really strange behavior now. Removing Q_OBJECT fixes it (for now, until I find the real problem)


  • Lifetime Qt Champion

    Ok, you have to set Qt::WA_StyledBackground attribute by yourself when you don't use a plain QWidget.



  • @Christian-Ehrlicher
    This applies to any sub-classed widget where you want it to have a stylesheet background color? You don't have to set it on a supplied sub-class, like say QLabel/QPushbutton etc.? But you do have to set it if you subclass from those?



  • Thank you so much, I would never find it.
    @JonB, I have QLabel subclass and it works normally



  • @Corvette653 said in Subclass doesn't work:

    I have QLabel subclass and it works normally

    Oh! Now of course QLabel is a subclass of QWidget itself, so might have expected the same problem. Does @Christian-Ehrlicher mean that this Qt::WA_StyledBackground only needs to be added to direct subclasses of QWidget? (And presumably QLabel itself has already done that.)


  • Lifetime Qt Champion

    From my pov it should not work for QLabel but it does. Need to create a bug report for this.

    QLabel is a QFrame so the attribute is set inside QStyleSheetStyle::polish().


Log in to reply