Results 1 to 8 of 8

Thread: What's wrong with this signalMapper

  1. #1
    Join Date
    Jan 2016
    Posts
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default What's wrong with this 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
    Qt Code:
    1. void theme(QString a)
    2. {
    3.  
    4. qDebug()<<a;
    5. }
    To copy to clipboard, switch view to plain text mode 
    here is how I connected
    Qt Code:
    1. for (int a=0;a<buttons.count();a++){
    2. QPushButton *button = new QPushButton();
    3.  
    4. QSignalMapper *signalMapper = new QSignalMapper();
    5.  
    6. connect(signalMapper, SIGNAL(mapped(QString)), this, SIGNAL(theme(QString)));
    7.  
    8. connect(button, SIGNAL(clicked()), signalMapper,SLOT(map()));
    9. signalMapper->setMapping(button,button->text());
    To copy to clipboard, switch view to plain text mode 
    here is the header file
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10. class MainWindow : public QMainWindow
    11. {
    12. Q_OBJECT
    13.  
    14. public:
    15. explicit MainWindow(QWidget *parent = 0);
    16. ~MainWindow();
    17. signals:
    18. void theme(QString a);
    19.  
    20. private slots:
    21. void on_pushButton_clicked();
    22.  
    23. void on_pushButton_10_clicked();
    24.  
    25. void on_temalar_button_clicked();
    26.  
    27. public:
    28. Ui::MainWindow *ui;
    29. };
    30.  
    31. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

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

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What's wrong with this signalMapper

    You declare "theme" to be a signal, but you want it as a slot.
    I am surprised this even compiles, MOC should have created an implementation of theme and the linker should have complained about the symbol being defined twice.

    Btw, you don't need a new signal mapper for every button, just allocate it outside the loop.

    And either pass a parent object or store the pointer as a member and delete it manually, otherwise you are leaking it.

    Cheers,
    _

  3. #3
    Join Date
    Jan 2016
    Posts
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: What's wrong with this signalMapper

    I transformed theme into slot both in header file and the signalmapper, and now I'm having this problem
    Qt Code:
    1. /home/metin/QTProjects/build-settings-Desktop_Qt_5_5_1_GCC_64bit-Debug/moc_mainwindow.cpp:82: error: undefined reference to `MainWindow::tema_izleme(QString)'
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. connect(signalMapper, SIGNAL(mapped(QString)), this, SLOT(theme(QString)));
    To copy to clipboard, switch view to plain text mode 
    Thanks for your help so far

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What's wrong with this signalMapper

    Apparently you have something called "tema_izleme", likely a function, that you have declared but not defined.

    Cheers,
    _

  5. #5
    Join Date
    Jan 2016
    Posts
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: What's wrong with this signalMapper

    I actually have
    Qt Code:
    1. void tema_izleme(QString a)
    2. {
    3. qDebug()<<"SUCCES!";
    4. }
    To copy to clipboard, switch view to plain text mode 
    header:
    Qt Code:
    1. private slots:
    2.  
    3. void tema_izleme(QString a);
    To copy to clipboard, switch view to plain text mode 
    error exists

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What's wrong with this signalMapper

    Quote Originally Posted by Nirvana View Post
    error exists
    Which is no surprise.
    You have declared a member function (method) but implemented a stand-alone function that incidentally has the same name.

    Cheers,
    _

  7. #7
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    120
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What's wrong with this signalMapper

    Hello,

    have you thought about throwing away the SignalHandler? You are using Qt5, and Qt5 provides a couple of new static connect functions. Combining them with lambdas from C++11 the code can be simplified. See http://doc.qt.io/qt-5/qobject.html#connect-4 for a reference.

    What I have understood from your example: You have a couple of buttons associated with "themes" and clicking on a button should set the associated theme to your software (currently you are simply showing the theme name on the debug console). To reproduce your setup, I've created a simple application with three buttons pushButton0, ..., pushButton2 in the main window with text "Theme 1", ..., "Theme 2".

    For compiling with C++11 (gcc option -std=c++11) add the following line to your .pro file:
    Qt Code:
    1. CONFIG += c++11
    To copy to clipboard, switch view to plain text mode 

    Here is the MainWindow.h:
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. namespace Ui {
    7. class MainWindow;
    8. }
    9.  
    10.  
    11. class MainWindow : public QMainWindow
    12. {
    13. Q_OBJECT
    14.  
    15. public:
    16. explicit MainWindow(QWidget *parent = 0);
    17. ~MainWindow();
    18.  
    19. private: // methods
    20. void connectTheme(QPushButton* button);
    21. void theme(const QString& data);
    22.  
    23. private:
    24. Ui::MainWindow *ui;
    25. };
    26.  
    27. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Observe that this does not contain the declaration of a slot. The private method theme(data) should get called when clicking on a button with the data associated to that button (currently the button text). So the buttons need to get connected to method theme(). To simplify the connection process, I have introduced the function connectTheme(QPushButton*). Here is the MainWindow.cpp containing the implementation of these functions:
    Qt Code:
    1. #include "MainWindow.h"
    2. #include "ui_MainWindow.h"
    3.  
    4. #include <iostream>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. connectTheme(ui->pushButton0);
    13. connectTheme(ui->pushButton1);
    14. connectTheme(ui->pushButton2);
    15. }
    16.  
    17. MainWindow::~MainWindow()
    18. {
    19. delete ui;
    20. }
    21.  
    22. void MainWindow::connectTheme(QPushButton* button)
    23. {
    24. /*
    25.   * Store the data associated with the button into a local variable.
    26.   * In this example, the data are just the button text.
    27.   */
    28. auto themeData = button->text();
    29.  
    30. /*
    31.   * Here we connect the clicked(bool) signal of button to a lambda capturing
    32.   * the themeData by value. This lambda calls MainWindow::theme() method passing
    33.   * it the captured data.
    34.   */
    35. connect(button, &QPushButton::clicked, [this, themeData](){theme(themeData);});
    36. }
    37.  
    38. void MainWindow::theme(const QString& data)
    39. {
    40. /*
    41.   * Just process the data passed in.
    42.   */
    43. std::cout << data.toStdString() << std::endl;
    44. }
    To copy to clipboard, switch view to plain text mode 
    Some explanation: In the constructor I connect the 3 buttons after ui initialization. The connectTheme(button) function stores the button text in a local variable and then connects the clicked signal of QPushButton button to a lambda function
    Qt Code:
    1. [this, themeData](){theme(themeData);}
    To copy to clipboard, switch view to plain text mode 
    The lambda function captures the this pointer (the pointer to the current MainWindow object) and a copy of the local variable themeData. In the lambda body
    Qt Code:
    1. theme(themeData);
    To copy to clipboard, switch view to plain text mode 
    method theme() of MainWindow is called with the data associated with the button. This lambda function replaces the QSignalMapper. The theme(data) method just processes the data passed in (in this case just print them to cout). Note that there is no need to declare theme() as a slot.

    With porting my application to Qt5 and using C++11 I now prefer the procedure explained above over using QSignalMapper.

    Best regards
    ars
    Last edited by ars; 23rd January 2016 at 14:54. Reason: Add hint for compiling with c++11

  8. The following user says thank you to ars for this useful post:

    d_stranz (24th January 2016)

  9. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: What's wrong with this signalMapper

    With porting my application to Qt5 and using C++11 I now prefer the procedure explained above over using QSignalMapper.
    Where's the smiley-face emoji that means "My brain just exploded"?

Similar Threads

  1. What am i doing wrong?
    By ayanda83 in forum Newbie
    Replies: 3
    Last Post: 2nd January 2013, 09:58
  2. What am I doing wrong??
    By Splatify in forum Newbie
    Replies: 3
    Last Post: 7th February 2011, 16:47
  3. i don't know what i do wrong..
    By Hardstyle in forum Newbie
    Replies: 2
    Last Post: 27th June 2010, 17:33
  4. What's wrong??
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 25th June 2008, 08:07
  5. Help please - what am I doing wrong?
    By Jimmy2775 in forum Qt Programming
    Replies: 6
    Last Post: 6th March 2006, 22:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.