Qt5/C++ - Signals and slots, QMainwindow to QDialog
Qt5 5.70
Qt Creator 4.1.0
Hello,
I've got a modal QDialog on top of a QMainwindow.
I start a mainwindow QProcess from the QDialog, this seems to work.
I'd like to get the value from the QMainwindow/QProcess to show on on the QDialog QProgressBar.
So far, no luck.
I'm obviously missing sommething and would appreciate some help.
Thanks in advance.
Regards
Code:
mainwinndow .cpp
void MainWindow::readyReadStandardOutput4() //slot
{
QString temp
= process
->readAllStandardOutput
();
outputList = temp.split("per");
qDebug() << outputList.at(0).toInt();
emit progressUpdate(outputList.at(0).toInt());
}
mainwinndow .hpp
signals:
void progressUpdate(int);
QDebug output:
1
2
...
...
99
100
Code:
noobsForm .cpp
QObject::connect(&mainwindow,
SIGNAL(progressUpdate
(int)),
this, SLOT(updateProgress(int)));
void noobsForm::updateProgress(int value) //slot
{
ui->progressBar->setValue(value);
qDebug() << "value " << value;
}
noobsForm .hpp
public slots:
void updateProgress(int);
Re: Qt5/C++ - Signals and slots, QMainwindow to QDialog
That looks ok, though one would usually do the connect from outside the dialog instead of passing a reference to the mainwindow.
Since you have log output, which of those to you get?
Cheers,
_
Re: Qt5/C++ - Signals and slots, QMainwindow to QDialog
Hello anda_skoa,
Quote:
Originally Posted by
anda_skoa
That looks ok, though one would usually do the connect from outside the dialog instead of passing a reference to the mainwindow.
Since you have log output, which of those to you get?
Thanks for your reply.
What log output and where?
I don't understamd the "from outside" bit.
Any way the progress bar on the dialog does not update.
Regards
Re: Qt5/C++ - Signals and slots, QMainwindow to QDialog
Quote:
Originally Posted by
jimbo
What log output and where?
Your qDebug() statements.
Quote:
Originally Posted by
jimbo
I don't understamd the "from outside" bit.
I assume you are creating the dialog somewhere in MainWindow and then you call exec() on that dialog object.
"outside" here means outside of the dialog code, i.e. in MainWindow, e.g. before the call to exec().
But passing the main window to the dialog for the dialog to set the connection up is OK too, just not as common.
Quote:
Originally Posted by
jimbo
Any way the progress bar on the dialog does not update.
Hence the attempt to narrow it down by checking which of your outputs you get.
Cheers,
_
Re: Qt5/C++ - Signals and slots, QMainwindow to QDialog
Hello anda_skoa,
Quote:
But passing the main window to the dialog for the dialog to set the connection up is OK too, just not as common.
Code:
myNoobs = new noobsForm(driveDataList, file, ui->cboxDevice->currentText());
QObject::connect(this,
SIGNAL(progressUpdate
(int)), myNoobs,
SLOT(updateProgress
(int)));
myNoobs->exec();
Thats the way, it works fine now, many thanks.
Regards.
Re: Qt5/C++ - Signals and slots, QMainwindow to QDialog
Quote:
That's the way, it works fine now, many thanks.
If this dialog is being created and posted in a slot (say, in response to a button click or something), then the code you have written looks like it will result in a memory leak when the slot exits. If the dialog is meant to be transient (i.e. posted only for the duration of the slot's execution), then the more conventional way is to create the dialog on the stack instead of on the heap through new():
Code:
void MainWindow::someSlot()
{
noobsForm myNoobs(driveDataList, file, ui->cboxDevice->currentText());
QObject::connect( this,
SIGNAL(progressUpdate
(int)),
&myNoobs,
SLOT(updateProgress
(int)));
myNoobs.exec();
}
In this way, the lifetime of the dialog is the same as the lifetime of the slot; when the slot exits, the "myNoobs" instance on the stack is automatically deleted.
Alternatively, you could use your heap-based method, but add a call to
Code:
myNoobs->setAttribute( Qt::WA_DeleteOnClose, true );
before calling exec(), which will cause Qt to delete the dialog instance when the dialog is closed.
Re: Qt5/C++ - Signals and slots, QMainwindow to QDialog
Hello d_stranz,
Thanks for a very good explanation of the methods.
Both work fine.
I've gone down the conventional route.
Regards