Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Replacing deprecated QtSignalMapper class to forward Signals in Qt5



  • 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?


  • Lifetime Qt Champion

    Hi,

    What signal do you connect to the mapper ?



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


  • Lifetime Qt Champion

    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.



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

  • Lifetime Qt Champion

    Something like:

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


  • @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*)) );
    

  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    The please mark this thread as solved, thx :)


Log in to reply