QDialog doesn't close
-
Hi,
I'm trying to close a QDialog from a QMessageBox using the following code:QMessageBox msgBox; QPushButton *OKButton = msgBox.addButton (tr("OK"),QMessageBox::RejectRole); msgBox.setText ("<b><font size = '16' color = 'green'>The Friend was added to the database. To add more press CTRL A."); msgBox.exec (); if(msgBox.clickedButton () == OKButton) { qDebug() << "OK was clicked."; this->reject ();
There is no error message, but t doesn't close or do anything. The messagebox is generated by the same QDialog I'm trying to close and it was opened usint Additem.exe(). What am I doing incorrectly?
Thank you for your help. -
@gabor53 said:
It is entering the if clause but ignores the reject(). It also ignores close(), finished(), accept(). No error message just does nothing.
Ok so the button is correctly evaluated. Did you eventually override
QDialog::closeEvent(...)
? -
hi
is the messagbox code inside the QDialog ?
there is no way it can ignore this->close(); ???
unless u override
void QDialog::closeEvent(QCloseEvent * e)
and ignore it. -
@kshegunov
Hi,
I tried the standard buttons but they didn't work either. Here is the whole function:void Additem::FunctAddtoDb(QString sIDr, QString namer, QString newWhatr, QString newMaterialr, QString newColorr, QString descriptionr, QString monthr, QString dayr, QString yearr, QString newSignedbyr, QString historyr, QString ager, QString notesr, QByteArray byteArrayr) { { QSqlDatabase db = QSqlDatabase::addDatabase ("QSQLITE","add"); db.setDatabaseName ("C:/Programming/Projects/FolkFriends/db.db"); bool OKadd = db.open (); QSqlQuery querys(db); // if(!db.open ()) if(OKadd == false) { qDebug() << "The database is not open (submit)!"; } else { qDebug() << "The database is open (submit)!"; } // qDebug() << "Original data " <<byteArray.size() << "bytes."; // qDebug() <<"Byte size: " << byteArray.size(); // qDebug() << "sID (from FunctAdd): " << sIDr; // qDebug() << "name (from FunctAdd): " << namer; querys.prepare("INSERT INTO Items (ID, Name, Pic, Description, Month, Day, Year, History, Age, Notes, Color, Material, Signed, What) VALUES(:ID, :Name, :Pic, :Description, :Month, :Day, :Year, :History, :Age, :Notes, :Color, :Material, :Signed, :What)" ); querys.bindValue (":ID",sIDr); querys.bindValue (":Name",namer); querys.bindValue (":Pic",byteArrayr); querys.bindValue (":Description",descriptionr); querys.bindValue (":Month",monthr); querys.bindValue (":Day",dayr); querys.bindValue (":Year",yearr); querys.bindValue (":History",historyr); querys.bindValue (":Age",ager); querys.bindValue (":Notes",notesr); querys.bindValue (":Color",newColorr); querys.bindValue (":Material",newMaterialr); querys.bindValue (":Signed",newSignedbyr); querys.bindValue (":What",newWhatr); bool result = querys.exec (); if(!result) { qDebug() <<"Error inserting into the main db!" << querys.lastError (); QMessageBox::warning (this,"Add to Database Warning","<b><font size='16' color='red'>Error 1002: The Friend was not added to the database."); } else { qDebug() << "Entered FunctAdd OK loop."; QMessageBox msgBox; QPushButton *OKButton = msgBox.addButton (tr("OK"),QMessageBox::RejectRole); msgBox.setText ("<b><font size = '16' color = 'green'>The Friend was added to the database. To add more press CTRL A."); msgBox.exec (); if(msgBox.clickedButton () == OKButton) { qDebug() << "OK was clicked."; this->reject (); } } db.close (); } QSqlDatabase::removeDatabase ("add"); } void Additem::close() { qDebug() << "Close was called"; Additem::reject (); }
Thank you.
-
@gabor53 you no need to do that for use QMessageBox.
The easy could be (for me) to call messageBox from conditionnal error test, and use the buttons allready provided (if need to use/add button, it steal easy to):bool error(true); QString my_datas("test"); if(error) QMessageBox::warning(this, tr("my title window"), tr("my error message: %1").arg(my_datas));
also, QMessageBox (like all Dialog box by default) will send back a int answer type object. So "switch()" C++ function should catch answer back after close.
And also, you can give name of your buttons directly from warning member function, et the end...). Look at API for QMessageBox, there is some examples.
Also, if your code is outside of messageBox (that seems to be the fact in your code), first, your condition will be read after the messagebox closed... if you want to read things before, so you have to put the code inside (so inherit QMessageBox for that). or use "QObject::connect" method for pass signal from QMEssageBox to a slot who will do what you want other.
But if it is just for close QMEssageBox, you absolutly not need all of this you try to do, but only call QMessageBox as i show you, it will have OK button allready inside, and it will work directly out of the "QMessageBox"... if not, there is a problem.Also... if you want to cath QMessageBox buttons events, you have to first declare a pointer on QMessageBox for use it from a QObject::connect (and your class has to have QOBJECT macro inside declaration somewhere for ba able to use connections).
example:QMessageBox *mgx; mgx = new QMessageBox(this); mgx.warning(this, tr("my title window"), tr("my text")); mgx.setStyleSheet("QLabel{color: green; font-size: 16;} QWidget{background-color: white;}"); // all the CSS style you want to give to your content... should go there in CSS style code with some features like wich object should have wich CSS "paint" connect(mgx, SIGNAL(buttonClicked(QAbstractButton), this, SLOT(on_OK_mgx_clicked(QAbstractButton)); mgx->open();
also, you have to create the function member declared as private (or public) slot, and then define it in cpp file (void on_OK_mgx_clicked(QAbstractButton button); in header file, then define it in source file linked.
that way you will catch button clicked returned by QAbstractButton object from clecked event in QMessageBox. But i think it is not what you are searching for... this is more complictae than use default mechanism for print a message with a close button.If you want more features, try first like that, play around after read the API on that, and ask more precisely what you try to do with QMessageBox (what is the finality).
http://doc.qt.io/qt-4.8/qmessagebox.html#buttonClicked
Hope that help. -
@jerome_isAviable
Thanks. The thing is I want to close the dialog called Additem that called QMessageBox.
QMessageBox closes wonderfully but any attempt to close Additem is disregarded. -
@gabor53
As you'd check any other pointer:QAbstractButton * clickedButton = msgBox.clickedButton(); if (!clickedButton) { qDebug() << "Well, we are out of luck. " << "The message box was dismissed with a key so this check: " << "msgBox.clickedButton () == OKButton is meaningless" << endl; }
-
@kshegunov
I did the following (before you posted your last message):if(msgBox.clickedButton () == NULL) { qDebug() << "ClickButton is null"; } else { qDebug() << "ClickButton is NOT null."; }
According to this code the clickedButton() is not NULL.
-
@gabor53
Okay then, going to the basics. Can you confirm that thethis->reject()
is called? That is, do you get the debug message"OK was clicked."
.
Another thing I just noticed:
Why do you have an overload forreject()
? This is usually not necessary, can you show that function and its declaration?Kind regards.
-
@gabor53 ok, well i understand better.
so you need to use it like that:QMessageBox mgx; int answer = mgx.warning(this, "my fix title", "my fix text"); if(answer == Qt::accept()) this->close();
that's all.
or with a question to validate:
int answer = QMessageBox::question( this, tr("Question tag..."), tr("read the error:/n%1 ").arg(error_string), tr("Close"), tr("Not close")); if(answer == 1) this->close();
and... don't forget to tell us (for share for other who will have same problem) finally what solution you choosed and worked for you and make the subject as "resolved" (said thank you for helpers time is also a nice option i like sometimes to read or write...).
thank you. -
@jerome_isAviable
I implemented this:{ qDebug() <<"Error inserting into the main db!" << querys.lastError (); QMessageBox::warning (this,"Add to Database Warning","<b><font size='16' color='red'>Error 1002: The Friend was not added to the database."); } else { qDebug() << "Entered FunctAdd OK loop."; QMessageBox msgBox; int answer = msgBox.information (this, tr("Confirmation"), tr("<b><font size = '16' color = 'green'>The Friend was added to the database. To add more press CTRL A."), QMessageBox::Close); if(answer == QMessageBox::Close) { this->close(); } } db.close (); } }
There are no error messages, and it enters the right loop
(displays " qDebug() << "Entered FunctAdd OK loop.";)
but the Additem dialog is still not closed. I have no idea why. -
@gabor53 try to use default message box button (not add QMessageBox::Close in information)
and use condition if(answer == 0) instead.
But... the principe is that, if your messagebox is modal, then the message box is waiting for you push button "OK" before the code ga ahead... right ?
so finally, condition serve nothing (there is only one button... condition serve for what ?).
finaly, just don't use condition but this->close() after QMessageBox::information.
Also, no need to declare QMessageBox mgx in this situation, directly call QMessageBox::information(....);
declare a variable for contain an object (QMessageBox or other) serve for add stuff/options and use it in other situation you not need here.so the code you need should also be:
QMessageBox::information (this, tr("Confirmation"), tr("<b><font size = '16' color = 'green'>The Friend was added to the database. To add more press CTRL A.")); this->close();
as soon as possible: do it simple, do it easy.
on my codes (if i well understand what you are doing), i use a QMessageBox::question for ask if the user want to create an other one new object, if he want i clear entries or not for that or if it is finish. Then from there, i catch the answer (who is the number of button pushed) for call action like close or not, and function for delete entries of this dialog box or not. So when i'm waiting for know what the user want to do next, i ask him what him want. (i not said it is a good idea... there is many... but... it is an idea).
Did you try the code i share with you with QMessageBox::question ? this one works sure.
also, for see what int answer give you, you could try to add a "qDebug() << answer;" The retrurn depend of the function you call and the button you push. This would help you to see what to catch in condition for do what you want (i do like that when i'm not sure or when something not works like i would like...). -
@jerome_isAviable
Thank you. I really understand how this work. I guess the problem is thatthis->close()
doesn't work no matter how I write QMessageBox. It doesn't even work, when there is no QMessageBox.