PDA

View Full Version : Qt5/C++ - Signals and slots, QMainwindow to QDialog



jimbo
1st October 2016, 15:45
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

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
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);

anda_skoa
1st October 2016, 16:14
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,
_

jimbo
1st October 2016, 17:47
Hello 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

anda_skoa
1st October 2016, 17:53
What log output and where?

Your qDebug() statements.



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.



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,
_

jimbo
1st October 2016, 18:49
Hello anda_skoa,


But passing the main window to the dialog for the dialog to set the connection up is OK too, just not as common.


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.

d_stranz
1st October 2016, 22:03
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():



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
myNoobs->setAttribute( Qt::WA_DeleteOnClose, true ); before calling exec(), which will cause Qt to delete the dialog instance when the dialog is closed.

jimbo
2nd October 2016, 14:40
Hello d_stranz,

Thanks for a very good explanation of the methods.
Both work fine.
I've gone down the conventional route.

Regards