Good day.
There is an MDI application. When you click the menu items open QMdiSubWindow 's. It is necessary that in the presence of an already open internal window, the same does not open when you click on the same menu item again.
So far I solve this problem by checking the WindowTitle parameter in each list object from the mdiArea->subWindowList() method for a match (so that there are not two identical ones). But this method looks painfully crutch. Surely there is a more elegant way. I ask experts to prompt.
Thank.
Shl I reached this method, has the right to exist ?:

 void MainWindow::CatProducts_open() { CatProducts *buf = QObject::findChild<CatProducts*>("catProducts"); if(buf==0) { CatProducts *catProducts = new CatProducts; catProducts->setObjectName("catProducts"); mdiArea->addSubWindow(catProducts); catProducts->show(); } } 

    2 answers 2

    Surely there is a more elegant way.

    The created QMdiSubWindow 's inherit QWidget , which in turn inherits QObject . The latter has two great methods QObject::setProperty and QObject::property . The first method we can assign a property (named value), the second - to consider it.

    Thus, creating the next window - you need to designate it with this very property. Further, according to the list of windows, it is easy to find it by this property.

    The advantage of this approach is that we have the right to make the value of properties unique (as it should be), but the window headers can be non-unique. Therefore, searching by properties is a simple and correct solution.

    • Thanks It works. Actually, this is the same thing that I wanted to do, only more literate (I agree with the argument about uniqueness). - Artik
    • But now I found the option via setObjectName () and the subsequent findChild <> (). Just do not understand how to implement it. - Artik
    • In the case of setPropery / property, you can "identify" windows with simple integers. And in the case of setObjectName, you have to “reinvent” strings. And you need it? - Majestio
    • Is it possible to identify windows with informative numbers? - Artik
    • Identification == finding the right one. Unique numbers are enough. Another question is "what and how to do with the windows?" Here you can store a hash of pointers (hash key => the same number). The rest of the info can be extracted by signs. - Majestio

    If the signal from QAction used to create the window (which is usually the case), then, as an option, you can beat on the switching of slots:

     void MainWindow::CatProducts_open() { QAction *action = qobject_cast<QAction*>(sender()); if(action == Q_NULLPTR) return; // Отключаем "action" от текущего слота. // Если используется иной сигнал, то соответствующим // образом правим строку ниже. disconnect(action, &QAction::triggered , this, &MainWindow::CatProducts_open); CatProducts *catProducts = new CatProducts; catProducts->setObjectName("catProducts"); mdiArea->addSubWindow(catProducts); catProducts->show(); connect(action, &QAction::triggered , catProducts, &CatProducts::show); } 
    • It is also an option. Thank. - Artik