QWidget displays 'black window"
-
This is a follow on to this
https://forum.qt.io/topic/154394/cannot-get-second-qwidget-window-to-show?_=1708302629937
I'm trying to find out why the QWidget::show doesn't display the screen as expected.
I've got a Widget that displays the connections status and waits for the continue or Cancel button to be pressed.
#include "connectionstatus.h" #include "ui_connectionstatus.h" class Controller; ConnectionStatus::ConnectionStatus(QWidget *parent) : QWidget(parent) , ui_(new Ui::ConnectionStatus) { ui_->setupUi(this); ui_->connectionStatus->setReadOnly(true); QWidget::connect(ui_->continueButton, &QAbstractButton::pressed, this, &ConnectionStatus::Continue ); QWidget::connect(ui_->cancelButton, &QAbstractButton::pressed, this, &ConnectionStatus::Cancel ); } ConnectionStatus::~ConnectionStatus() { delete ui_; } bool ConnectionStatus::statusOk() { return statusOk_; } void ConnectionStatus::DisplayText(QString text) { ui_->connectionStatus->setText(text); } void ConnectionStatus::Continue() { statusOk_= true; } void ConnectionStatus::Cancel() { statusOk_ = false; }
I have a controller class that displays the connection status and acts on which button is pressed.
void Controller::Init(QString ipAddr, int port) { ipAddr_ = ipAddr; port_ = port; connection_->Init(ipAddr_, port_); statusWindow_ = new ConnectionStatus(); statusWindow_->activateWindow(); statusWindow_->DisplayText("Connected to Server at " + ipAddr_ + " Port " + QString::number(port_)); statusWindow_->show(); }
While trying to figure out why this isn't working as I expect, I'm usinf the debugger and found that if I step through the controller code, after I step past statusWindow_->show() I get a window to pop up, but it has no content
If I run the code without the debugger I get this.At what stage does the show() display the window 'correctly?
Thanks -
@Poldi The QWidget::show() results in a spontaneous show event to draw the widget client area. This will be acted upon when the program reaches the Qt event loop. When you are stopped after show(), but before returning from Init() the Qt event loop has yet to be reached.
-
@ChrisW67 Thanks, but when I set a break in controller_->DoWork();, the window is still black
void MainWindow::connect() { ipAddress_ = ui_->ipAddress->text(); port_ = ui_->port->value(); ui_->connectButton->setEnabled(false); controller_->Init(ipAddress_, port_); controller_->DoWork(); qDebug() << "Window popup"; }
-
@ChrisW67 said in QWidget displays 'black window":
This will be acted upon when the program reaches the Qt event loop.
Qt requires your code to fall through and return control to the Qt event loop, i.e. this should be the default "resting" state of your application. Until DoWork() and this MainWindow::connect() finish execution that has not happened.
-