Cannot get second QWidget window to show
-
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
-
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
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.
-
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 Poldi has marked this topic as solved on
-
P Poldi referenced this topic on