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

signalMapper



  • I have been looking docs and other topics about singalMappers but I still couldn't make it work properly.

    here is the function I want to call

    void theme(QString a)
    {

    qDebug()<<a;
    

    }

    here is how I connected
    for (int a=0;a<buttons.count();a++){
    QPushButton *button = new QPushButton();

        QSignalMapper *signalMapper = new QSignalMapper();
    
        connect(signalMapper, SIGNAL(mapped(QString)), this, SIGNAL(theme(QString)));
    
        connect(button,  SIGNAL(clicked()), signalMapper,SLOT(map()));
        signalMapper->setMapping(button,button->text());
    

    here is the header file

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();
    signals:
    void theme(QString a);

    private slots:
    void on_pushButton_clicked();

    void on_pushButton_10_clicked();
    
    void on_temalar_button_clicked();
    

    public:
    Ui::MainWindow *ui;
    };

    #endif // MAINWINDOW_H

    But when I click the button, I don't get any debug output. What is the thing that I'm doing wrongly?
    Thanks



  • Hi!
    I think that

    connect(signalMapper, SIGNAL(mapped(QString)), this, SIGNAL(theme(QString)));
    

    should be

    connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(theme(QString)));
    


  • @Wieland
    I did but it is still not working.
    I put theme from signals: to slots: in header file. And did what you said, now I'm having this:

    /home/metin/QTProjects/build-settings-Desktop_Qt_5_5_1_GCC_64bit-Debug/moc_mainwindow.cpp:82: error: undefined reference to `MainWindow::theme(QString)'



  • @Nirvana You probably need to clean your project and run qmake again.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    MainWindow's theme is a signal so basically your initial connect line was correct from that perspective. However, your theme function is just a static function somewhere.

    If you want it to be called, you should make theme a slot, and add the missing MainWindow:: between void and theme and keep the new version of the connect statement.


Log in to reply