How to create qmessage box with shoew
-
Hello, I'm new to the qt world. I have a problem with
show()
andexec()
.I am reading a series of data over the serial port and I want to show a message on the screen like please wait until the data comes from my port. When my serial port is empty, I hide that text.
When I use it this way, the messagebox doesn't continue with other processes until it closes
QMessageBox msgBox; msgBox.setWindowFlags(Qt::WindowType::Popup); msgBox.setText("Please Wait"); msgBox.exec();
.If I type
show()
instead ofexec()
, the messagebox appears as I want and continues with other operations. but the please wait text that I defined does not appear.QMessageBox msgBox; msgBox.setWindowFlags(Qt::WindowType::Popup); msgBox.setText("Please Wait"); msgBox.show();
If my serial port is empty, I hide the messagebox.
here is my all code.
void MainWindow::on_pushButton_4_clicked() { QMessageBox msgBox; msgBox.setWindowFlags(Qt::WindowType::Popup); msgBox.setText("Please Wait"); msgBox.show(); //Here I'm getting datas with QSerialPort after create message. if(serialInput.isEmpty()){ msgBox.hide(); QMessageBox::information(this, "Info", "Proggress Completed.."); }
What can I do to show the Please wait message and continue at the same time?
-
Hello, I'm new to the qt world. I have a problem with
show()
andexec()
.I am reading a series of data over the serial port and I want to show a message on the screen like please wait until the data comes from my port. When my serial port is empty, I hide that text.
When I use it this way, the messagebox doesn't continue with other processes until it closes
QMessageBox msgBox; msgBox.setWindowFlags(Qt::WindowType::Popup); msgBox.setText("Please Wait"); msgBox.exec();
.If I type
show()
instead ofexec()
, the messagebox appears as I want and continues with other operations. but the please wait text that I defined does not appear.QMessageBox msgBox; msgBox.setWindowFlags(Qt::WindowType::Popup); msgBox.setText("Please Wait"); msgBox.show();
If my serial port is empty, I hide the messagebox.
here is my all code.
void MainWindow::on_pushButton_4_clicked() { QMessageBox msgBox; msgBox.setWindowFlags(Qt::WindowType::Popup); msgBox.setText("Please Wait"); msgBox.show(); //Here I'm getting datas with QSerialPort after create message. if(serialInput.isEmpty()){ msgBox.hide(); QMessageBox::information(this, "Info", "Proggress Completed.."); }
What can I do to show the Please wait message and continue at the same time?
@TheCeylann
Hi,You have to create a instance variable in MainWindow.
In the MainWindow.hQMessageBox* mskBox=nullptr;
and in .cpp
if(msgBox==nullptr) { msgBox=new QMessageBox(this); } msgBox->setText("Please Wait"); msgBox->show();
Or create the msgBox instance in the construtor.
Here I'm getting datas with QSerialPort after create message.
For that, use signals & slots.
-
@TheCeylann said in How to create qmessage box with shoew:
What can I do to show the Please wait message and continue at the same time?
The reason this is not occurring now is the lifetime of the QMessageBox instance you create on the stack is the duration of the
MainWindow::on_pushButton_4_clicked()
call. When the slot exits, the QMessageBox you want to show() is destroyed.@mpergand provides a way to ensure the lifetime of the object extends beyond the
MainWindow::on_pushButton_4_clicked()
call. -
Hello, I'm new to the qt world. I have a problem with
show()
andexec()
.I am reading a series of data over the serial port and I want to show a message on the screen like please wait until the data comes from my port. When my serial port is empty, I hide that text.
When I use it this way, the messagebox doesn't continue with other processes until it closes
QMessageBox msgBox; msgBox.setWindowFlags(Qt::WindowType::Popup); msgBox.setText("Please Wait"); msgBox.exec();
.If I type
show()
instead ofexec()
, the messagebox appears as I want and continues with other operations. but the please wait text that I defined does not appear.QMessageBox msgBox; msgBox.setWindowFlags(Qt::WindowType::Popup); msgBox.setText("Please Wait"); msgBox.show();
If my serial port is empty, I hide the messagebox.
here is my all code.
void MainWindow::on_pushButton_4_clicked() { QMessageBox msgBox; msgBox.setWindowFlags(Qt::WindowType::Popup); msgBox.setText("Please Wait"); msgBox.show(); //Here I'm getting datas with QSerialPort after create message. if(serialInput.isEmpty()){ msgBox.hide(); QMessageBox::information(this, "Info", "Proggress Completed.."); }
What can I do to show the Please wait message and continue at the same time?
@TheCeylann
In addition to the preceding comments about the message box. After you goshow()
you have//Here I'm getting datas with QSerialPort after create message.
followed by testing the
serialInput
and maybe hiding the message box. This is not the way to go in Qt. What does your commented code actually do? If it blocks while waiting for serial input it will also block your UI from user responsiveness because you are inside a slot.The Qt way is to display your modeless message box (per what the others have said about scope) and then exit the slot. Then to allow the serial input to arrive in the background and use signals and slots to communicate when data has arrived and close the message box at a future time.
-
@TheCeylann
Hi,You have to create a instance variable in MainWindow.
In the MainWindow.hQMessageBox* mskBox=nullptr;
and in .cpp
if(msgBox==nullptr) { msgBox=new QMessageBox(this); } msgBox->setText("Please Wait"); msgBox->show();
Or create the msgBox instance in the construtor.
Here I'm getting datas with QSerialPort after create message.
For that, use signals & slots.
@mpergand When I try thıs ,
msgBox->setText("Please Wait");
shown like that. -
@TheCeylann
In addition to the preceding comments about the message box. After you goshow()
you have//Here I'm getting datas with QSerialPort after create message.
followed by testing the
serialInput
and maybe hiding the message box. This is not the way to go in Qt. What does your commented code actually do? If it blocks while waiting for serial input it will also block your UI from user responsiveness because you are inside a slot.The Qt way is to display your modeless message box (per what the others have said about scope) and then exit the slot. Then to allow the serial input to arrive in the background and use signals and slots to communicate when data has arrived and close the message box at a future time.
@JonB
My commented code. If the serial connection is open, it sends a command to my card that allows it to send data and waits for the data to arrive. When my serial port stops sending data. I'm showing all data read receipts. When I send the command and the data starts coming in. I need to show the message Please wait. until my serial port is isEmpty. but when I do it with show, the text I wrote with setText does not appear. -
@TheCeylann
In addition to the preceding comments about the message box. After you goshow()
you have//Here I'm getting datas with QSerialPort after create message.
followed by testing the
serialInput
and maybe hiding the message box. This is not the way to go in Qt. What does your commented code actually do? If it blocks while waiting for serial input it will also block your UI from user responsiveness because you are inside a slot.The Qt way is to display your modeless message box (per what the others have said about scope) and then exit the slot. Then to allow the serial input to arrive in the background and use signals and slots to communicate when data has arrived and close the message box at a future time.
allows it to send data and waits for the data to arrive
@JonB said in How to create qmessage box with shoew:
The Qt way is to display your modeless message box (per what the others have said about scope) and then exit the slot. Then to allow the serial input to arrive in the background and use signals and slots to communicate when data has arrived and close the message box at a future time.
-
@mpergand When I try thıs ,
msgBox->setText("Please Wait");
shown like that.Use msgBox.open() instead, turns out show() use modal mode !
or do: msgBox.setModal(false);