QT, I made modaless dialog, and now I want to close all dialogs by clicking a button

lll

I made a non-modal dialog (see the code below). How can I close all dialogs by clicking a button?

mainwindow.h

private:
    Ui::MainWindow *ui;
    Dialog *dialog;
};

mainwindow.cpp

void MainWindow::on_pushButton_clicked()
{
    dialog = new Dialog(this);
    dialog->show();
}

void MainWindow::on_closeButton_Clicked()
{
    //here I want to close all opened dialogs.

}
Scheff

Title and question text are a bit incongruently. Where title asks

I want to close all dialogs by clicking a button

the question text exposes a sample code for one dialog only where pointer to dialog is available as member.

This answer refers to the title itself:

One option to close all opened dialogs would be just to remember their pointers in a std::vector<QDialog*> and call hide() for them e.g. in MainWindow::on_closeButton_Clicked().

However, the book-keeping of dialogs in an extra vector would make me uncomfortable. Actually, there is already such storage as the dialog constructors are called with the MainWindow pointer as parent (which is surely derived from QMainWindow or QWidget).

Every QObject stores a child list of objects which are constructed with the former as parent. (This is the Qt way to simplify memory management.) This is what can be used here → all created dialogs should occur in this list.

I made a small sample testQCloseAllDlgs.cc to test/illustrate this:

#include <QtWidgets>

int main(int argc, char **argv)
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  QApplication app(argc, argv);
  // setup GUI
  QMainWindow qWin;
  qWin.setWindowTitle("Close Dialogs Test");
  for (int i = 0; i < 3; ++i) {
    QDialog *pQDlg = new QDialog(&qWin);
    pQDlg->setWindowTitle(QString("Dialog %1").arg(i + 1));
    //pQDlg->show();
  }
  QPushButton qBtnCloseDlgs("Close All Dialogs");
  qWin.setCentralWidget(&qBtnCloseDlgs);
  qWin.show();
  for (QObject *pQChild : qWin.children()) {
    if (QDialog *pQDlg = dynamic_cast<QDialog*>(pQChild)) {
      pQDlg->show();
    }
  }
  // install signal handlers
  QObject::connect(&qBtnCloseDlgs, &QPushButton::clicked,
    [&qWin](bool){
      for (QObject *pQChild : qWin.children()) {
        if (QDialog *pQDlg = dynamic_cast<QDialog*>(pQChild)) {
          pQDlg->hide();
        }
      }     
    });
  // run time loop
  return app.exec();
}

and a resp. project file testQCloseAllDlgs.pro:

SOURCES = testQCloseAllDlgs.cc

QT += widgets

Compiled and tested in cygwin64:

$ qmake-qt5 testQCloseAllDlgs.pro

$ make && ./testQCloseAllDlgs
g++ -c -fno-keep-inline-dllexport -D_GNU_SOURCE -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_WIDGETS_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I. -isystem /usr/include/qt5 -isystem /usr/include/qt5/QtWidgets -isystem /usr/include/qt5/QtGui -isystem /usr/include/qt5/QtCore -I. -I/usr/lib/qt5/mkspecs/cygwin-g++ -o testQCloseAllDlgs.o testQCloseAllDlgs.cc
g++  -o testQCloseAllDlgs.exe testQCloseAllDlgs.o   -lQt5Widgets -lQt5Gui -lQt5Core -lGL -lpthread 
Qt Version: 5.9.4

Snapshot of testQCloseAllDlgs

When the Close All Dialogs button is clicked (the only widget of main window) all 3 dialogs vanish.

Please, note that hide() doesn't delete the dialogs – the become invisible only.

This is what I prefer personally → to recycle dialogs.

  • To make them visible again, the show() method might be called again.
  • To "recycle" the first currently hidden dialog, the loop might be modified to check the visibility of dialogs until an invisible is found.

Alternatetively, dialogs might be deleted. (They will be excluded from the children list of parent automatically.) This is another point where it will pay off to use the already existing QObject::children() member instead of an own book-keeping because the latter might be not quite easy to implement correctly.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

On clicking on close button of page i want to clear all selectbox value

I want to make a dropdown in React. Clicking a button should open a submenu. Clicking outside of the submenu should close it

Made an edit to homebrew formula, now I want to save it

I want a custom dialogue with close button

I want to increase the size of the close button on the openbox

In ionic inAppBrowser I want custom close button

I made a javascript function and now I want to shorten it, how do I do that?

I want to Close Navigation Drawer if and only if I click the Button

How I can close dialog, from another thread? Qt

i want to change background after clicking Button in android with kotlin

while clicking on export button i want to export aspx gridview?

In React after clicking a button I want to change it immediately to the previous state?

I want to delete the added item in the list by index when clicking on the button

i need a popupview when clicking button and again clicking i want to disapper that view in objective c

How do I print the date by clicking a button in Qt Designer?

I want a alert dialog to come up when a radio button is checked

I want to Get a list when clicking on regions of pie chart made by using plotly and rshiny package

How to dismiss all the dialogs from button click of last opened dialog?

I have made a scrollbar, but I want my button response. The second frame is not adjusting as per the window

I made a mobile app design and now I want to know which one is better for coding, and performance my flutter app

I made this to print "Hello World !", but now I want to know other ways to print "Hello World !" in C++, simplify it if can

I want to close a div when clicking on the body but omit the parent div using .not( ) or a similar simple function

jQueryUI dialog scrolls to top when clicking close button in IE

i want to check a manufacturer already exists or not by clicking the submit button,i am doing this in codeigniter,

how do I add an extra Dialog Widget close button to a jquery ui Dialog window

i have designed ui from pyqt designer now i want to connect that button and label together

i want to show a div when clicking a specific button (a help button) with html /css / javascript

I want to add 'show more' button for overflowed elements then only after clicking on shoe more, I want to show new line

How can I configure one button to open/close all accordions?