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's child show doesn't work
Forum Updated to NodeBB v4.3 + New Features

QWidget's child show doesn't work

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 805 Views
  • 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.
  • M Offline
    M Offline
    Milosz
    wrote on last edited by
    #1

    I have a strange problem:
    My MainWindow is QWidget type and its constructor is like that:

    ...............
    loggingWidget = new QWidget;
    loggingWidget->setGeometry(0,0,1920,1080);
    loggingWidget->setStyleSheet("background-image: url(FinalOutput_01_10.png); border: none;");

    (1) QWidget *rulesWidget = new QWidget(loggingWidget);
    rulesWidget->setGeometry(200,200,1020,680);
    rulesWidget->setStyleSheet("background-image: url(FinalOutput.png); border: none;");

    rulesWidget->show();
    

    This operates OK.
    But when I inherite QWidget into RulesWidget class and line (1) is:
    RulesWidget *rulesWidget = new RulesWidget(loggingWidget);

    that show() doesn't work.

    definition of RulesWidget:
    class RulesWidget : public QWidget
    {
    Q_OBJECT
    public:
    explicit RulesWidget(QWidget *parent = nullptr);
    ......

    implementation
    RulesWidget::RulesWidget(QWidget *parent) : QWidget(parent)
    {

    }

    JonBJ 1 Reply Last reply
    0
    • M Milosz

      I have a strange problem:
      My MainWindow is QWidget type and its constructor is like that:

      ...............
      loggingWidget = new QWidget;
      loggingWidget->setGeometry(0,0,1920,1080);
      loggingWidget->setStyleSheet("background-image: url(FinalOutput_01_10.png); border: none;");

      (1) QWidget *rulesWidget = new QWidget(loggingWidget);
      rulesWidget->setGeometry(200,200,1020,680);
      rulesWidget->setStyleSheet("background-image: url(FinalOutput.png); border: none;");

      rulesWidget->show();
      

      This operates OK.
      But when I inherite QWidget into RulesWidget class and line (1) is:
      RulesWidget *rulesWidget = new RulesWidget(loggingWidget);

      that show() doesn't work.

      definition of RulesWidget:
      class RulesWidget : public QWidget
      {
      Q_OBJECT
      public:
      explicit RulesWidget(QWidget *parent = nullptr);
      ......

      implementation
      RulesWidget::RulesWidget(QWidget *parent) : QWidget(parent)
      {

      }

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

      @Milosz said in QWidget's child show doesn't work:

      My MainWindow is QWidget type

      I thought it had to be QMainWindow type?

      loggingWidget = new QWidget;

      Where is its parent specified, or what is it added to?

      I don't know if these are relevant to your issue, but I wouldn't mind understanding them anyway...

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Milosz
        wrote on last edited by
        #3

        Ok,
        so I show all the code

        MainWindow::MainWindow(QWidget *parent) :
        QWidget(parent)
        {

        this->setWindowFlags(Qt::CustomizeWindowHint);
        this->setGeometry(0,0,1920, 1080);
        loggingWidget = new QWidget;
        loggingWidget->setGeometry(0,0,1920,1080);
        loggingWidget->setStyleSheet("background-image: url(Ekran_01_FinalOutput_01_10.png); border: none;");
        
        
        RulesWidget *rulesWidget = new RulesWidget(loggingWidget);
        rulesWidget->setGeometry(200,200,1020,680);
        rulesWidget->setStyleSheet("background-image: url(Regulamin_FinalOutput.png); border: none;");
        
        
        rulesWidget->show();
        
        
        QHBoxLayout *mainLayout = new QHBoxLayout;
        mainLayout->addWidget(loggingWidget);
        
        setLayout(mainLayout);
        

        }

        and definition of MainWindow
        class MainWindow : public QWidget
        {
        Q_OBJECT

        public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();

        private:

        QWidget *loggingWidget;
        

        };

        JonBJ 1 Reply Last reply
        0
        • M Milosz

          Ok,
          so I show all the code

          MainWindow::MainWindow(QWidget *parent) :
          QWidget(parent)
          {

          this->setWindowFlags(Qt::CustomizeWindowHint);
          this->setGeometry(0,0,1920, 1080);
          loggingWidget = new QWidget;
          loggingWidget->setGeometry(0,0,1920,1080);
          loggingWidget->setStyleSheet("background-image: url(Ekran_01_FinalOutput_01_10.png); border: none;");
          
          
          RulesWidget *rulesWidget = new RulesWidget(loggingWidget);
          rulesWidget->setGeometry(200,200,1020,680);
          rulesWidget->setStyleSheet("background-image: url(Regulamin_FinalOutput.png); border: none;");
          
          
          rulesWidget->show();
          
          
          QHBoxLayout *mainLayout = new QHBoxLayout;
          mainLayout->addWidget(loggingWidget);
          
          setLayout(mainLayout);
          

          }

          and definition of MainWindow
          class MainWindow : public QWidget
          {
          Q_OBJECT

          public:
          explicit MainWindow(QWidget *parent = 0);
          ~MainWindow();

          private:

          QWidget *loggingWidget;
          

          };

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

          @Milosz
          At least that makes sense now!

          Again, I don't know, but you call rulesWidget->show(); before you have ever added it (or anything) to the main window. I wouldn't even try to do that, seems real odd to me, can a widget deal with show() when it's not anywhere on the page? Does it make any difference if you put that as the very last line?

          1 Reply Last reply
          0
          • M Offline
            M Offline
            Milosz
            wrote on last edited by
            #5

            I marked it's strange (:-)).
            I have put it at the very last line but no effect :-(

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

              I would guess you have not implemented sizeHint() and therefore QWidget::sizeHint() is called which simply returns and invalid size since it doesn't know better.

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

              M 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                I would guess you have not implemented sizeHint() and therefore QWidget::sizeHint() is called which simply returns and invalid size since it doesn't know better.

                M Offline
                M Offline
                Milosz
                wrote on last edited by
                #7

                @Christian-Ehrlicher No effect.
                :-(

                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