I my Qt application written in C++, I would like to wait for a mousePressEvent to do something after the mouse pressed.
Would you please hehp?
Thanks in advance.
Kind regards.
You can use the signal/slots to connect your mouseEvent to a specific slot.
Or you can create a QEventLoop and quit it by emitting "mSignalTriggert" when a mouseEvent happend.
QEventLoop l;
connect(this, &MainWindow::mSignalTriggerd, &l, &QEventLoop::quit);
l.exec();
Related
In my project, I want to call a variable QImage image generated in the mainwindow.h, in other class files, e.g., called SegSetupDialog.h. Here the image is loaded by clicking a pushbutton in mainwindow.ui, and the SegSetupDialog is a QDialog, which pops up by clicking one pushbutton in mainwindow.ui.
I tried to use a signal-slot connection to send qimage of mainwindow to SegSetupDialog as follows.
For Class MainWindow:
SegSetupDialog *segsetup;
if(image.isNull()==false)
{
emit sendImgData(image);
qDebug()<<"sendImgData emitted!";
if(segsetup==NULL) segsetup = new SegSetupDialog();
connect(this, SIGNAL(sendImgData(QImage)),segsetup,SLOT(getImgData(QImage)),Qt::QueuedConnection);
}
In SegSetupDialog::getImgData
void SegSetupDialog::getImgData(QImage qimage)
{
qImg = qimage;
qDebug()<<"qimage received!";
}
The above connection seems not to work since the qDebug message in getImgData is not printed out. Anyone can help check something wrong with codes, or suggest other methods for accessing image of mainwindow? Thanks!!
You need to do the signal/slot connection before you emit the signal. Connections are done once, usually in the constructor.
However, you should probably do the connect() in the constructor of SegSetupDialog instead of the MainWindow one. It's SegSetupDialog that wants to be notified about image data updates, so you should establish the connection there.
Also, to make sure the signals and slots are specified correctly, don't use Qt 4 connect() calls. Use Qt 5 ones that are checked at compile-time:
connect(this, &MainWindow::sendImgData,
segsetup, &SegSetupDialog::getImgData, Qt::QueuedConnection);
(Of course change it appropriately if you move it to the SegSetupDialog constructor.)
I'm trying to enable a QPushButton after another QPushButton is clicked and I've run into a problem. The first QPushButton can emit a clicked() signal while the second QPushButton only has slots of the form setEnabled(bool) and setDisabled(bool).
Basically, I'm trying to do
connect(ui->pbViewVolume, SIGNAL(clicked()),
ui->pbSaveAsImage, SLOT(setEnabled(true)));
Since it's not possible to send a signal with fewer parameters than the slot, how can I best do this? The only way I see is to create a public slot for my MainWindow like
void EnableSaveAsImageButton(){
ui->pbSaveAsImage->setEnabled(true);
}
but I'd rather not fill my MainWindow with this sort of rubbish function.
You have to create that slot.
You don't need to make that slot public, make it private.
This function is not rubbish.
I need to send the status of my Qt application through and Upd socket every seconds. How can I achive this? Should I subclass QThread e using QThread::sleep or is there a best approch ? thanks
In the end I use a different approch:
a QTimer:
QTimer iAmAliveTimer.setInterval(1500);
iAmAliveTimer.setSingleShot(true);
connect(&iAmAliveTimer, SIGNAL(timeout()), this, SLOT(sendIamAlive()), Qt::UniqueConnection);
iAmAliveTimer.start();
when the SLOT is called, I send what I need through my socket.
I am wondering if i need to disconnect singals and slots if i destroy the signal emitting object. Here is an example:
QAudioOutput * audioOutput = new QAudioOutput(format,mainWindow);
connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),this,SLOT(stateChanged(QAudio::State)));
delete audioOutput;
audioOutput = new QAudioOutput(format,mainWindow);
connect(audioOutput,SIGNAL(stateChanged(QAudio::State)),this,SLOT(stateChanged(QAudio::State)));
Will this automatically disconnect the signal from the old audioOutput, or will it lead to mem leaks or some other undefined behavior ?
Thank you in advance.
The signals are automatically disconnected when you call the QObject destructor.
Have a look at the Qt documentation: QObject Destructor
You don't have to manually disconnect() signals and slots, the QObject destruction cleans them up automatically.
I have a Qt application with this kind of main()...
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWin;
... A separate, non-GUI thread is launched here
mainWin.Init();
mainWin.show();
app.exec();
}
This other thread that is created before the mainWin needs to know when it can start communicating with the mainWin. But since the mainWin uses Qt signals, slots, timers, etc, it's not truly ready to rock until the event loop is running (via exec()).
My question is: is there some signal or event that is emitted when the event loop has started?
Consider this. In mainWin.Init(), you can create something like a QTimer and even call .start() to kick it off. But it won't actually be run and trigger events until exec() has been called. This is why I need to know when the event loop has truly started.
You can send a signal to your window before the exec() call. This will place an entry in app's signal queue.
When exec() is running, the signal will be delivered and your window will know that the event loop is running.
A simple way would be to use QTimer::singleShot(0, &mainWin, SLOT(onEventLoopStarted())); which connects to a custom slot of your window class.
Since emitted signals don't get lost when the event loop is not yet running, your thread may not necessarily need to know when your window is ready.
Your thread could start sending signals to the window right away but it will only receive signals from the window when the event loop is running.
You can do it in the following order:
QApplication app(argc, argv);
Mainwinwdow mainWin;
QThread yourThread;
//connect the signals from the thread to the mainWin here
mainWin.Init();
mainWin.show();
yourThread.start();
return app.exec();