QDialog is blank with show command - c++

If I create my QDialog and show it modal with exec() everything works fine, but I need this no modal!
With show() the Dialog is empty!
ProgramLoading *programLoading = new ProgramLoading();
programLoading->show();
// some code
programLoading->done(0);
Constructor
ProgramLoading::ProgramLoading(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
setWindowFlags( Qt::CustomizeWindowHint ); // remove window border
}
Don't think something with Dialog code is wrong because it works with exec()!
Any hints? Thank you!
PS: I'm using QT plugin for VisualStudio 2008

mw (Default with Window Decoration):
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
sd = new SplashDialog();
sd->show();
QTimer::singleShot(10000,this,SLOT(closeSplashDialog()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::closeSplashDialog()
{
sd->close();
delete sd;
}
splash (UI having Label and Button):
SplashDialog::SplashDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::SplashDialog)
{
ui->setupUi(this);
setWindowFlags( Qt::CustomizeWindowHint );
}
SplashDialog::~SplashDialog()
{
delete ui;
}
Splash Dialog opens and is being closed 10 seconds later as intended

Related

How can I programmatically make a window fullscreen?

How can I programmatically make a window fullscreen?
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
scene = new QNewScene(this);
ui->graphicsView->setScene(scene);
// here is some code that will make the window fullscreen
}
For example:
QTimer::singleShot(0, this, SLOT(showFullScreen()));
method to make your window full screen:
QWidget::showFullScreen()
check it here: https://doc.qt.io/qt-5/qwidget.html#showFullScreen

Connecting to a same signal in two windows

Can you think of a reason why this second scenario not working?
I have signal connection in two windows , Settings and Patients
In Settings:
Settings::Settings( QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Settings)
{
ui->setupUi(this);
connect(&api, &tetra_grip_api::tetraGripEvent,this, &Settings::eventHandler);
...
}
api is global instance of a class.
And in Patients:
Patients::Patients( QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Patients)
{
ui->setupUi(this);
connect(&api, &tetra_grip_api::tetraGripEvent,this, &Patients::eventHandlerTwo);
}
Situation1 - Working
I construct both Settings and Patients window from Main
#include <QApplication>
tetra_grip_api api;
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
api.openSerialPort();
QObject::connect(api.serial, SIGNAL(readyRead()), &api, SLOT(readData()));
Settings w(nullptr); //---->this behaves right
Patients v(nullptr); //---- >this behaves right
v.show();
w.show();
return a.exec();
}
By working I mean , both the slots are being called , and QLabel set text accordingly
Situation 2 - Not working
I call Patients from Settings:
void Settings::on_pushButton_patients_clicked()
{
this->close();
stagetwo = new Patients(this);
stagetwo -> show();
}
where stagetwo is public
public:
Settings(QString,QWidget *parent = nullptr);
~Settings();
Patients *stagetwo;
Here Settings works just fine (slot being called) but Patients::eventhandlerTwo is not being called at all.
EDIT (Found the bug)
It's bit late to understand that I need to call a method in the API (battery_percentage) to emit signal tetraGripEvent ( bettery_percentage will contact "battery register" in the device and this will force API to emit signal)
basically I need to do this on both Settings and Patients
#include "settings.h"
#include "ui_settings.h"
Settings :: Settings (QWidget *parent) : QMainWindow(parent)
QMainWindow(parent)
, ui(new Ui::Settings )
{
ui->setupUi(this);
connect(&api, &tetra_grip_api::tetraGripEvent,this, &Settings::eventHandler);
...
tetra_grip_api::battery_percentage(); ------> calling this only emit the signal
}
and in Patients
#include "patients.h"
#include "ui_patients.h"
Patients::Patients(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Patients)
{
ui->setupUi(this);
connect(&api, &tetra_grip_api::tetraGripEvent,this, &Patients::eventHandlerTwo);
...
tetra_grip_api::battery_percentage();
}
As per Qt documentation for QWidget::close(), which QMainWindow is being inherited from:
The QApplication::lastWindowClosed() signal is emitted when the last
visible primary window (i.e. window with no parent) with the
Qt::WA_QuitOnClose attribute set is closed. By default this attribute
is set for all widgets except transient windows such as splash
screens, tool windows, and popup menus.
So, in the second scenario, when you set the parent of Patients to a Settings instance, you should expect an undefined behavior like this. Because by calling Settings::close() should delete Settings and quit the application when the call returns to the event loop. You can explicitly change it by :
Settings::Settings( QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Settings)
{
ui->setupUi(this);
this->setAttribute(Qt::WA_QuitOnClose, false);
connect(&api, &tetra_grip_api::tetraGripEvent,this, &Settings::eventHandler);
...
}
You can also reparent both windows to another window.

C++ QT Add a widget to scroll area without layout

so I an new to C++ and QT and I am trying to add a widget to a scroll area but without a layout as I want the widgets to be click and dragged.
Here's my code (main window.cpp):
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "player_window.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
player_window *player = new player_window(ui->scrollArea_WidgetContents);
player->setGeometry(50,50,450,130);
ui->scrollArea_WidgetContents->layout()->addWidget(player);
}
Thanks!

Insert QSplitter in QStackedWidget

I have a QStackedWidget with a custom widget in it.
//mainwindow.h
private:
CentralWidget m_centralWidget;
//mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->stackedWidget->addWidget(&m_centralWidget);
}
Now i want to create a QSplitter, fill the Splitter with content and add this Splitter to the StackedWidget.
CentralWidget::CentralWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::CentralWidget)
{
ui->setupUi(this);
connect(ui->pbAddSplit, SIGNAL(pressed()), this, SLOT(addSplit()));
}
void CentralWidget::addSplit()
{
QWidget* parent = parentWidget();
QStackedWidget* stackedWidget = qobject_cast<QStackedWidget*>(parent);
if(!stackedWidget) {
return;
}
QSplitter* splitter = new QSplitter(Qt::Vertical);
splitter->addWidget(new QLabel("Banana"));
splitter->addWidget(this);
int nIndex = stackedWidget->addWidget(splitter);
stackedWidget->setCurrentIndex(nIndex);
}
The result should be a QLabel("Banana") as one Widget of the splitter and the CentralWidget as the other splitter-widget.
But i just get the QLabel("Banana") - SplitterHandle and the CentralWidget got lost somehow...
splitter->addWidget(this) is obviously wrong...
Replacing it with splitter->addWidget(new QLabel("Cherry")) will result in a correct SplitterWidget...
The problem seems to be the parenting, but i cant figure it out.
Any suggestions would be most welcome!

How do I open a dialog and retrieve strings from it, in Qt4?

This is the main window so far and the second window is a dialog window. How do I get the text from a textbox on window2 when it closes?
Thanks.
#include "mainwindow.h"
#include "window2.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(closeProgram()));
connect(ui->openWindowBtn, SIGNAL(clicked()), this, SLOT(openSecondWindow()));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::openSecondWindow()
{
Window2 w2;
w2.exec();
}
void MainWindow::closeProgram()
{
close();
}
Found Solution
All I had to do is create a getString() function in the Window2 class to retreive the text from ui->...
QString Window2::getString()
{
return ui->textEdit->text();
}
Look at your .ui file in designer (or the resulting generated file from the uic), and access the QLineEdit object by name (the same way you connect that signal). You can retrieve the text with the lineEdit::text() accessor.