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. Replacing deprecated QtSignalMapper class to forward Signals in Qt5
QtWS25 Last Chance

Replacing deprecated QtSignalMapper class to forward Signals in Qt5

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 2.3k Views
  • 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.
  • S Offline
    S Offline
    sandro4912
    wrote on last edited by
    #1

    I have this code which makes a mdi window written for Qt 4:

    class MdiWindow : public QMainWindow
    {
        Q_OBJECT
    ...
    private:
        QWorkspace* workspace
        QSignalMapper* mapper
    }
    
    
    MdiWindow::MdiWindow( QWidget *parent ) : QMainWindow( parent )
    {
      ...
    
      workspace = new QWorkspace;
      setCentralWidget( workspace );
      
      connect( workspace, SIGNAL(windowActivated(QWidget *)), this, SLOT(enableActions()));
      mapper = new QSignalMapper( this );
      connect( mapper, SIGNAL(mapped(QWidget*)), workspace, SLOT(setActiveWindow(QWidget*)) );
    
      ....
    }
    

    According to the documentation QWorkspace should be replaced with QMdiArea.

    I did that and wrote the first connect like this:

    connect(workspace, &QMdiArea::subWindowActivated,
            this, &MdiWindow::enableActions);
    

    But what about QSignalMapper also that is also deprecated.

    So how can i update this line:

    mapper = new QSignalMapper( this );
    connect( mapper, SIGNAL(mapped(QWidget*)), workspace, SLOT(setActiveWindow(QWidget*)) );
    

    I read QSignalMapper can be replaced with lamdas but how in this case?
    If i understand right mapper forwards all the signals from this to workspaces active window?

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

      Hi,

      What signal do you connect to the mapper ?

      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
      2
      • S Offline
        S Offline
        sandro4912
        wrote on last edited by
        #3

        What do you mean with that theres only these two lines going on in the constructor:

        mapper = new QSignalMapper( this );
        connect( mapper, SIGNAL(mapped(QWidget*)), workspace, SLOT(setActiveWindow(QWidget*)) );
        

        Doesn't the mapper forward all the signals from class `MdiWindow`` here?

        In midi Window iI have several QAction

        ...
        private:
        
            QMdiArea* workspace;
            QSignalMapper* mapper;
        
            QAction *newAction;
            QAction *closeAction;
            QAction *exitAction;
        
            QAction *cutAction;
            QAction *copyAction;
            QAction *pasteAction;
        ...
        

        I thought by creation of QSignalMapper with this it knows about them....

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

          No it doesn't. How would it know what you want exactly to connect ? That's not the goal of this class.

          Take a look at the example of QSignalMapper's documentation.

          But from what you wrote, it simply hasn't been used at all and you can remove it from your code.

          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
          3
          • S Offline
            S Offline
            sandro4912
            wrote on last edited by
            #5

            found later int the code it gets used like this:

            void MdiWindow::updateWindowList()
            {
            windowMenu->clear();

            windowMenu->addAction(tileAction);
            windowMenu->addAction(cascadeAction);
            windowMenu->addSeparator();
            windowMenu->addAction(nextAction);
            windowMenu->addAction(previousAction);
            windowMenu->addAction(separatorAction);
            
            auto i = 1;
            for (auto &window : workspace->subWindowList()) {
                QString text;
                
                if (i<10) {
                    text = QString("&%1 %2").arg(i++).arg(window->windowTitle());    
                }
                else{
                    text = window->windowTitle();
                }
                
                auto action = windowMenu->addAction(text);
                action->setCheckable(true);
                action->setChecked(window->widget() == activeDocument());
                
                        
                connect(action, &QAction::triggered, mapper, map);
                mapper->setMapping(action, w); 
            }  
            

            }

            So how to get rid of the mapper here?

                connect(action, &QAction::triggered, mapper, map);
                mapper->setMapping(action, w);
            
            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Something like:

              connect(action, &QAction::triggered, [=] { setActiveWindow(window); });
              

              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
              3
              • S Offline
                S Offline
                sandro4912
                wrote on last edited by
                #7

                @SGaist said in Replacing deprecated QtSignalMapper class to forward Signals in Qt5:

                connect(action, &QAction::triggered, [=] { setActiveWindow(window); });

                that works. But then comming back to the constrcutor lines. How to ged rid of them:

                mapper = new QSignalMapper( this );
                connect( mapper, SIGNAL(mapped(QWidget*)), workspace, SLOT(setActiveWindow(QWidget*)) );
                
                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @sandro4912 said in Replacing deprecated QtSignalMapper class to forward Signals in Qt5:

                  How to ged rid of them:

                  You don't need it at all anymore with the solution @SGaist suggested...

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  2
                  • S Offline
                    S Offline
                    sandro4912
                    wrote on last edited by
                    #9

                    yes now its working even without. I had some other crash when in ported from QWorkspace to QMdiWindow but know everything works. thanks.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      The please mark this thread as solved, thx :)

                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                      Visit the Qt Academy at https://academy.qt.io/catalog

                      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