Solved How to detect a Qwidget is closed (not destroyed) from another class?
-
Hi Guys! I would like to achive that i press F2 in a QlineEdit whats in class A. Show a Class B Qwidget where u can pick a text from it with enter. When i press enter the widget is closed (but not destoryed) and Class A detect it, and the text, and put it into the Class A QlineEdit.
I could do that detect when closed the Class B widget but just so that destroy the whole class B.
Any ideas?
(Sorry for my bad englsih) -
@Kaguro
Ah...we still need class B, but instead of subclass QWidget, make it subclass QDialog.class B : public QDialog { Q_OBJECT public: explicit B(QWidget *parent = nullptr); QString getText() const; private: QString text; };
B::B(QWidget *parent) : QDialog(parent) { //Remove the default "?" in the title bar setWindowFlag(Qt::WindowContextHelpButtonHint, false); } QString B::getText() const { return text; }
In class B, when you press enter / get the result from the tableview
//assign the result to "text" text = ...; //instead of calling "close()", call "accept()", it sets the result of "exec()" to 1 accept();
So when you want the user to get the result from dialog B:
B b(this); if(b.exec()) { //This is when you call "accept()" (with the result) QString result = b.getText(); } else { //This is when the dialog is closed by the user (without setting the result) }
-
From your description, I feel that using a QDialog as class B would be easier, if you can accept a modal window.
When you press F2, show the dialog with QDialog::exec(), it will wait until the dialog closes, so you can get your result after that.
Just like the predefined standard dialogs (QInputDialog, QFileDialog, etc...)If you don't want that, then the other way would be reimplement closeEvent() of class B, add a signal there. Then you can connect that signal to detect the closing.
-
Install an event filter and respond to the
QCloseEvent
that the other object receives (without actually capturing it). -
@Bonnie I like that idea! I have a QtableView in Class B (Qdialog now). But i am new in Qt, can u give me an example for that solution? If u have enugh of time for this, and mood of course! :)
-
@Kaguro
Ah...we still need class B, but instead of subclass QWidget, make it subclass QDialog.class B : public QDialog { Q_OBJECT public: explicit B(QWidget *parent = nullptr); QString getText() const; private: QString text; };
B::B(QWidget *parent) : QDialog(parent) { //Remove the default "?" in the title bar setWindowFlag(Qt::WindowContextHelpButtonHint, false); } QString B::getText() const { return text; }
In class B, when you press enter / get the result from the tableview
//assign the result to "text" text = ...; //instead of calling "close()", call "accept()", it sets the result of "exec()" to 1 accept();
So when you want the user to get the result from dialog B:
B b(this); if(b.exec()) { //This is when you call "accept()" (with the result) QString result = b.getText(); } else { //This is when the dialog is closed by the user (without setting the result) }
-
@Bonnie And its working!! :DD Many many thanks !!!! I'll come to you with a beer! :D Thanks again! :)