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. Connecting open subwindow menu actions to setActiveSubWindow slot
Qt 6.11 is out! See what's new in the release blog

Connecting open subwindow menu actions to setActiveSubWindow slot

Scheduled Pinned Locked Moved General and Desktop
qt current subwhow to connectconnecting contmenu actions to
9 Posts 3 Posters 5.0k Views 2 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.
  • KyefK Offline
    KyefK Offline
    Kyef
    wrote on last edited by
    #1

    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!

    Kyef Elliot
    If Can Imagine It...I Can Do It!!

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jalomic
      wrote on last edited by
      #2

      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()));

      1 Reply Last reply
      0
      • KyefK Offline
        KyefK Offline
        Kyef
        wrote on last edited by
        #3

        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 Elliot
        If Can Imagine It...I Can Do It!!

        J 1 Reply Last reply
        0
        • KyefK Kyef

          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!!

          J Offline
          J Offline
          jalomic
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jalomic
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • KyefK Offline
              KyefK Offline
              Kyef
              wrote on last edited by
              #6

              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!

              Kyef Elliot
              If Can Imagine It...I Can Do It!!

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Do you have the stack trace of the crash ?

                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
                • KyefK Offline
                  KyefK Offline
                  Kyef
                  wrote on last edited by Kyef
                  #8

                  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!!

                  Kyef Elliot
                  If Can Imagine It...I Can Do It!!

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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.
                    }
                    

                    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

                    • Login

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