<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[activeSubWindow is unreliable. Is this a bug?]]></title><description><![CDATA[<p dir="auto">This is a modification to the mdi program from the old but good book, foundations of QT development.<br />
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.</p>
<p dir="auto">Is this a bug that we cannot rely on activeSubWindow returning a pointer to  the active subwindow?</p>
<pre><code>void MdiWindow::fileNew()
{
    DocumentWindow *document = new DocumentWindow();
    QMdiSubWindow *subWindow = workspace-&gt;addSubWindow(document);
    workspace-&gt;setActiveSubWindow(subWindow);
    connect(document, &amp;DocumentWindow::copyAvailable, this, &amp;MdiWindow::updateEditActions);

    // create action for this submenu item
    // QString text = tr("&amp;%1 %2").arg(orderedSubWindows.size() + 1).arg(document-&gt;windowTitle());

   // QString text = document-&gt;getName();;
    QAction *action = new QAction("", this);
    action-&gt;setCheckable(true);
    connect(action, &amp;QAction::triggered, this, [this, subWindow]() {
        workspace-&gt;setActiveSubWindow(subWindow);
    });
    windowActions[subWindow] = action;
    orderedSubWindows.append(subWindow);

    // connect(subWindow, &amp;QObject::destroyed, this, [this, subWindow]() {
    //     windowActions.remove(subWindow);
    //     orderedSubWindows.removeOne(subWindow);
    //     updateWindowMenu();  // Update menu when a subwindow is destroyed
    // });

    document-&gt;show();
    //subWindow-&gt;show();
   // workspace-&gt;setActiveSubWindow(subWindow);
    enableActions();

    qDebug() &lt;&lt; "After setActiveSubWindow:";
    qDebug() &lt;&lt; "  Active subwindow:" &lt;&lt; (workspace-&gt;activeSubWindow() ? "exists" : "does not exist");
    qDebug() &lt;&lt; "  Is our new window active?" &lt;&lt; (workspace-&gt;activeSubWindow() == subWindow);
    qDebug() &lt;&lt; "  Does our new window have focus?" &lt;&lt; subWindow-&gt;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, &amp;QObject::destroyed, this, [this, subWindow]() {
        QAction* action = windowActions.take(subWindow);
        if (action) {
            windowMenu-&gt;removeAction(action);
            // delete action;
        }
        orderedSubWindows.removeOne(subWindow);
        //updateWindowMenu();
    });
    updateWindowMenu();  // Update menu when a new subwindow is created
}
</code></pre>
<p dir="auto">openFile.cpp</p>
<pre><code>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(&amp;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-&gt;activeSubWindow() &amp;&amp; !workspace-&gt;subWindowList().isEmpty()) {
    //     workspace-&gt;setActiveSubWindow(workspace-&gt;subWindowList().first());
    // }

    DocumentWindow *docToUse = nullptr;

    // First, check if the active document is blank to open a text file.
    QMdiSubWindow *activeSubWindow = workspace-&gt;activeSubWindow();

    qDebug() &lt;&lt; "Number of subwindows:" &lt;&lt; workspace-&gt;subWindowList().size();
    qDebug() &lt;&lt; "Active subwindow before if(activeSubWindow):" &lt;&lt; (workspace-&gt;activeSubWindow() ? "exists" : "does not exist");
   
    if(activeSubWindow)
    {
        qDebug() &lt;&lt; "Active subwindow exists inside if(activeSubWindow) ";
        DocumentWindow *activeDoc = qobject_cast&lt;DocumentWindow*&gt;(activeSubWindow-&gt;widget());
        if(activeDoc &amp;&amp; activeDoc-&gt;toPlainText().isEmpty())
        {
            docToUse = activeDoc;
            showTextFile(docToUse, content, filename);
        }
        else
        {
            QList&lt;QMdiSubWindow *&gt; subWindows = workspace-&gt;subWindowList();
            for(QMdiSubWindow *subWindow : subWindows)
            {
                DocumentWindow *doc = qobject_cast&lt;DocumentWindow*&gt;(subWindow-&gt;widget());
                if(doc &amp;&amp; doc-&gt;toPlainText().isEmpty())
                {
                    workspace-&gt;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-&gt;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, &amp;DocumentWindow::copyAvailable, this, &amp;MdiWindow::updateEditActions);
        subWindow-&gt;show();
        workspace-&gt;setActiveSubWindow(subWindow);
        enableActions();
        updateWindowMenu();
    }
}
</code></pre>
]]></description><link>https://forum.qt.io/topic/157517/activesubwindow-is-unreliable-is-this-a-bug</link><generator>RSS for Node</generator><lastBuildDate>Tue, 06 Oct 2026 14:57:04 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/157517.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 30 Jun 2024 09:26:24 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to activeSubWindow is unreliable. Is this a bug? on Sun, 30 Jun 2024 23:25:41 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> . Sorry. I am running ubuntu 22.04 wayland and Qt Creator 13.0.2 based on Qt 6.6.3 (GCC 10.3.1 20210422 (Red Hat 10.3.1-1), x86_64)</p>
]]></description><link>https://forum.qt.io/post/803859</link><guid isPermaLink="true">https://forum.qt.io/post/803859</guid><dc:creator><![CDATA[ntos]]></dc:creator><pubDate>Sun, 30 Jun 2024 23:25:41 GMT</pubDate></item><item><title><![CDATA[Reply to activeSubWindow is unreliable. Is this a bug? on Sun, 30 Jun 2024 19:10:22 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">Which version of Qt are you using ?<br />
On which platform ?<br />
If Linux, which desktop environment ? Which window manager ?</p>
]]></description><link>https://forum.qt.io/post/803848</link><guid isPermaLink="true">https://forum.qt.io/post/803848</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Sun, 30 Jun 2024 19:10:22 GMT</pubDate></item></channel></rss>