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. Cannot get second QWidget window to show

Cannot get second QWidget window to show

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 402 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.
  • P Offline
    P Offline
    Poldi
    wrote on last edited by
    #1

    I'm learning Qt and cannot figure out why the second window doesn't display

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include "connectionstatus.h"
    
    QT_BEGIN_NAMESPACE
    namespace Ui {
    class MainWindow;
    }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void connect();
    
    private:
        Ui::MainWindow *ui_;
        ConnectionStatus *statusWindow_;
    };
    #endif // MAINWINDOW_H
    
    #include <QDebug>
    #include "mainwindow.h"
    #include "./ui_mainwindow.h"
    
    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui_(new Ui::MainWindow)
    {
        ui_->setupUi(this);
        QWidget::connect(ui_->connectButton, &QAbstractButton::pressed, this, &MainWindow::connect);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui_;
    }
    
    void MainWindow::connect()
    {
        // statusWindow_ = new ConnectionStatus(this);
        statusWindow_ = new ConnectionStatus();
        statusWindow_->setParent(this);
        statusWindow_->activateWindow();
        statusWindow_->show();
        qDebug() << "Window popup";
    }
    
    #ifndef CONNECTIONSTATUS_H
    #define CONNECTIONSTATUS_H
    
    #include <QWidget>
    
    namespace Ui {
    class ConnectionStatus;
    }
    
    class ConnectionStatus : public QWidget
    {
        Q_OBJECT
    
    public:
        explicit ConnectionStatus(QWidget *parent = nullptr);
        ~ConnectionStatus();
    
    private:
        Ui::ConnectionStatus *ui;
    };
    
    #endif // CONNECTIONSTATUS_H
    
    
    #include "connectionstatus.h"
    #include "ui_connectionstatus.h"
    
    ConnectionStatus::ConnectionStatus(QWidget *parent)
        : QWidget(parent)
        , ui(new Ui::ConnectionStatus)
    {
        ui->setupUi(this);
    }
    
    ConnectionStatus::~ConnectionStatus()
    {
        delete ui;
    }
    
    

    Thanks

    C 1 Reply Last reply
    0
    • P Poldi

      I'm learning Qt and cannot figure out why the second window doesn't display

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      #include "connectionstatus.h"
      
      QT_BEGIN_NAMESPACE
      namespace Ui {
      class MainWindow;
      }
      QT_END_NAMESPACE
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      
      private slots:
          void connect();
      
      private:
          Ui::MainWindow *ui_;
          ConnectionStatus *statusWindow_;
      };
      #endif // MAINWINDOW_H
      
      #include <QDebug>
      #include "mainwindow.h"
      #include "./ui_mainwindow.h"
      
      MainWindow::MainWindow(QWidget *parent)
          : QMainWindow(parent)
          , ui_(new Ui::MainWindow)
      {
          ui_->setupUi(this);
          QWidget::connect(ui_->connectButton, &QAbstractButton::pressed, this, &MainWindow::connect);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui_;
      }
      
      void MainWindow::connect()
      {
          // statusWindow_ = new ConnectionStatus(this);
          statusWindow_ = new ConnectionStatus();
          statusWindow_->setParent(this);
          statusWindow_->activateWindow();
          statusWindow_->show();
          qDebug() << "Window popup";
      }
      
      #ifndef CONNECTIONSTATUS_H
      #define CONNECTIONSTATUS_H
      
      #include <QWidget>
      
      namespace Ui {
      class ConnectionStatus;
      }
      
      class ConnectionStatus : public QWidget
      {
          Q_OBJECT
      
      public:
          explicit ConnectionStatus(QWidget *parent = nullptr);
          ~ConnectionStatus();
      
      private:
          Ui::ConnectionStatus *ui;
      };
      
      #endif // CONNECTIONSTATUS_H
      
      
      #include "connectionstatus.h"
      #include "ui_connectionstatus.h"
      
      ConnectionStatus::ConnectionStatus(QWidget *parent)
          : QWidget(parent)
          , ui(new Ui::ConnectionStatus)
      {
          ui->setupUi(this);
      }
      
      ConnectionStatus::~ConnectionStatus()
      {
          delete ui;
      }
      
      

      Thanks

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by ChrisW67
      #2

      Do you see your qDebug() output? I am going to assume yes.

      I suspect you want a separate free-floating window. Making this ConnectionStatus widget a child of the existing one will also place it inside the client area of that parent widget. Your new widget may be rendered underneath other content in that area (especially as it is not part of that layout involved).
      Try removing the call to parent().

      If you parented the widget to ensure that the object is deleted then you'll need an alternate approach to that.

      Currently you will create a new ConnectionStatus every time the user clicks the button. This may not be the intent.

      P 1 Reply Last reply
      3
      • C ChrisW67

        Do you see your qDebug() output? I am going to assume yes.

        I suspect you want a separate free-floating window. Making this ConnectionStatus widget a child of the existing one will also place it inside the client area of that parent widget. Your new widget may be rendered underneath other content in that area (especially as it is not part of that layout involved).
        Try removing the call to parent().

        If you parented the widget to ensure that the object is deleted then you'll need an alternate approach to that.

        Currently you will create a new ConnectionStatus every time the user clicks the button. This may not be the intent.

        P Offline
        P Offline
        Poldi
        wrote on last edited by
        #3

        @ChrisW67 Thanks for thew quick reply
        After making the second window a new window it did show up.
        I also had only one display only field on the second window, which when I made it part of the parent didn't show up, so I thought it didn't work.
        Thanks

        1 Reply Last reply
        0
        • P Poldi has marked this topic as solved on
        • P Poldi referenced this topic on

        • Login

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