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. Subclass doesn't work

Subclass doesn't work

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 848 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.
  • C Offline
    C Offline
    Corvette653
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #5

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

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      JonBJ 1 Reply Last reply
      5
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

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

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        1
        • C Offline
          C Offline
          Corvette653
          wrote on last edited by
          #3

          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.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

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

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            3
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #5

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

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              JonBJ 1 Reply Last reply
              5
              • Christian EhrlicherC Christian Ehrlicher

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

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6

                @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?

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Corvette653
                  wrote on last edited by
                  #7

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

                  JonBJ 1 Reply Last reply
                  0
                  • C Corvette653

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #8

                    @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.)

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by Christian Ehrlicher
                      #9

                      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().

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      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