Connecting open subwindow menu actions to setActiveSubWindow slot



  • Am new on to Qt Programming. I have managed to create a menu on the Menubar listing the open subwindows in the MdiArea; however, I cant connect the slot to activate and show a given subwindow. Here's my code for creation of the contextual menu:

    void MainWin::subWmenuL()
    {
    QList<QMdiSubWindow *> subWins = ui->workspace->subWindowList();
    // Make sure it isn't empty
    if(subWins.isEmpty())
    {
    return;
    }
    ui->menuOpWindows->clear();
    for(int i = 0; i < subWins.size(); ++i){
    QMdiSubWindow *child = subWins.at(i);

        ui->menuOpWindows->addAction(child->windowTitle());
    }
    

    }

    I have attached this function to MdiArea's signal subWindowActivated(QMdiSubWindow*) and it creates menu actions whenever a subwindow is opened. But I cannot connect the following slot:

    void MainWin::wMenuActivated(int i)
    {
    QList<QMdiSubWindow *> subWin = ui->workspace->subWindowList();

    if(subWin.isEmpty()){
    return;
    }
    QMdiSubWindow *child = subWin.at(i);
    {ui->workspace->setActiveSubWindow(child);}
    

    }

    please help!



  • Open documentation. And read this:

    QAction * QMenu::addAction ( const QString & text )
    This is an overloaded function.
    This convenience function creates a new action with text. The function adds the newly created action to the menu's list of actions, and returns it.

    And now look here:

    QAction myaction *= ui->menuOpWindows->addAction(child->windowTitle());
    connect(myaction, SIGNAL(triggered()), this, SLOT(mySlot()));



  • Thanks Jalomic,
    Surely you have put some light on the subject...but when I try to implement , the the signal/slot connection is successfully compiled; however, I get runtime error:

    QObject::connect: Incompatible sender/receiver arguments
    QAction::triggered() --> MainWin::wMenuActivated(int)

    I cannot connect the signal to the slot!!



  • @Kyef said:

    QAction::triggered() --> MainWin::wMenuActivated(int)

    signal triggered have no arguments, but the wMenuActivated slot does have. You must to create slot without params. Every slot and signal must have same signatures.
    You can connect menu signal "triggered ( QAction * action )" with slot wMenuActivated ( QAction * action ) if you want to handle all actions in one slot.



  • Sorry. Signal with params can be connected to slot without params, but Signal without params can't be connected to slot with params



  • sorry jalomic,
    I didn't get back to you; I've been busy on other stuff, I have tried to modify my code eliminating the int params:

    void MainWin::wMenuActivated()
    {

    foreach(QMdiSubWindow *child, ui->workspace->subWindowList())
    {
    QString currentSbw = ui->menuOpWindows->activeAction()->text();
    if (child->windowTitle().contains(currentSbw),Qt::CaseInsensitive)
    ui->workspace->setActiveSubWindow(child);
    }

    }

    Again this compiles very well but when QAction is triggered my app crushes...
    What's wrong, I can't figure out the problem!


  • Lifetime Qt Champion

    Hi,

    Do you have the stack trace of the crash ?



  • Thanks SGaist,
    Well, when I debug, I get the following warning:

    The inferior stopped because it received a signal from the Operating System.
    Signal name : SIGSEGV
    Signal meaning : Segmentation fault

    The Dissembler point to the following line 5:
    0x61dc243a <+0x000a> 8b 48 04 mov 0x4(%eax),%ecx
    Stack view shows 2Levels:
    0 - ZNK7QAction4textEv- C:\Qt\5.4\mingw491_32\binQt5Widgets.dll-
    1 -??

    On my function, the debugger stops pointing to the following line:
    QString currentSbw = ui->menuOpWindows->activeAction()->text();

    please note that I am so so fresh to Qt/C++ programming and am intensely reading to understand these debugging details. Any assistance given will be highly appreciated!!


  • Lifetime Qt Champion

    That's not a debug build

    Anyway, you don't check that activeAction actually returns something.

    QAction *activeAction = ui->menuOpWindows->activeAction();
    if (activeAction) {
        QString currentSbw = activeAction->text();
        etc.
    }
    


Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.