activeSubWindow is unreliable. Is this a bug?
-
This is a modification to the mdi program from the old but good book, foundations of QT development.
I start the program and click on fileNew to create a new blank subwindow. Then I click on open to open some text on this subwindow. No text is shown on it as the debug message says: Active subwindow before if(activeSubWindow): does not exist. If I create a second subwindow and open a text file on it, then the operation is successful. Sometimes this isn't at all. And you can easily see when it is not successful by looking at the debug message: Active subwindow before if(activeSubWindow): does not exist.Is this a bug that we cannot rely on activeSubWindow returning a pointer to the active subwindow?
void MdiWindow::fileNew() { DocumentWindow *document = new DocumentWindow(); QMdiSubWindow *subWindow = workspace->addSubWindow(document); workspace->setActiveSubWindow(subWindow); connect(document, &DocumentWindow::copyAvailable, this, &MdiWindow::updateEditActions); // create action for this submenu item // QString text = tr("&%1 %2").arg(orderedSubWindows.size() + 1).arg(document->windowTitle()); // QString text = document->getName();; QAction *action = new QAction("", this); action->setCheckable(true); connect(action, &QAction::triggered, this, [this, subWindow]() { workspace->setActiveSubWindow(subWindow); }); windowActions[subWindow] = action; orderedSubWindows.append(subWindow); // connect(subWindow, &QObject::destroyed, this, [this, subWindow]() { // windowActions.remove(subWindow); // orderedSubWindows.removeOne(subWindow); // updateWindowMenu(); // Update menu when a subwindow is destroyed // }); document->show(); //subWindow->show(); // workspace->setActiveSubWindow(subWindow); enableActions(); qDebug() << "After setActiveSubWindow:"; qDebug() << " Active subwindow:" << (workspace->activeSubWindow() ? "exists" : "does not exist"); qDebug() << " Is our new window active?" << (workspace->activeSubWindow() == subWindow); qDebug() << " Does our new window have focus?" << subWindow->hasFocus(); /* * When a subwindow is closed, make sure to remove it from both windowActions and orderedSubWindows. * You can do this by connecting to the subWindow's destroyed signal: * We put this connect function here right before updateWindowMenu so that we don't have to * call it twice: once in connect and once outside of connect. */ connect(subWindow, &QObject::destroyed, this, [this, subWindow]() { QAction* action = windowActions.take(subWindow); if (action) { windowMenu->removeAction(action); // delete action; } orderedSubWindows.removeOne(subWindow); //updateWindowMenu(); }); updateWindowMenu(); // Update menu when a new subwindow is created }openFile.cpp
void MdiWindow::openFile() { QString filename = QFileDialog::getOpenFileName(this, tr("Open file"), "", tr("Text Files (*.txt);;All Files (*)") ); if(filename.isEmpty()) return; QFile file(filename); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox::warning(this, tr("Error"), tr("Cannot open file: ") + file.errorString(), QMessageBox::Ok ); return; } QTextStream in(&file); QString content = in.readAll(); file.close(); if(content.isEmpty()) { QMessageBox::information(this, tr("Empty File"), tr("The selected file is empty.")); return; } // if (!workspace->activeSubWindow() && !workspace->subWindowList().isEmpty()) { // workspace->setActiveSubWindow(workspace->subWindowList().first()); // } DocumentWindow *docToUse = nullptr; // First, check if the active document is blank to open a text file. QMdiSubWindow *activeSubWindow = workspace->activeSubWindow(); qDebug() << "Number of subwindows:" << workspace->subWindowList().size(); qDebug() << "Active subwindow before if(activeSubWindow):" << (workspace->activeSubWindow() ? "exists" : "does not exist"); if(activeSubWindow) { qDebug() << "Active subwindow exists inside if(activeSubWindow) "; DocumentWindow *activeDoc = qobject_cast<DocumentWindow*>(activeSubWindow->widget()); if(activeDoc && activeDoc->toPlainText().isEmpty()) { docToUse = activeDoc; showTextFile(docToUse, content, filename); } else { QList<QMdiSubWindow *> subWindows = workspace->subWindowList(); for(QMdiSubWindow *subWindow : subWindows) { DocumentWindow *doc = qobject_cast<DocumentWindow*>(subWindow->widget()); if(doc && doc->toPlainText().isEmpty()) { workspace->setActiveSubWindow(subWindow); docToUse = doc; showTextFile(docToUse, content, filename); break; } } } } else { // Create a new subwindow and put the text file on it. docToUse = new DocumentWindow(); QMdiSubWindow *subWindow = workspace->addSubWindow(docToUse); showTextFile(docToUse, content, filename); // Unlike the exisiting subwindows above, this is a new one so we must do the following chores for it. connect(docToUse, &DocumentWindow::copyAvailable, this, &MdiWindow::updateEditActions); subWindow->show(); workspace->setActiveSubWindow(subWindow); enableActions(); updateWindowMenu(); } } -
Hi,
Which version of Qt are you using ?
On which platform ?
If Linux, which desktop environment ? Which window manager ?