Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. activeSubWindow is unreliable. Is this a bug?

activeSubWindow is unreliable. Is this a bug?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 252 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    ntos
    wrote on last edited by
    #1

    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();
        }
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Which version of Qt are you using ?
      On which platform ?
      If Linux, which desktop environment ? Which window manager ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • N Offline
        N Offline
        ntos
        wrote on last edited by
        #3

        @SGaist . 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)

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved