Results 1 to 5 of 5

Thread: QSignalMapper question

  1. #1
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSignalMapper question

    Hello,

    I am trying to understand QSignalMapper, I found this piece of code, see below.
    It compiles correct, but its not working, change 'signal' to 'slot' was the answer.
    Bu I can't get it working.

    Regards,
    Arend (absolute beginner)

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
    2. {
    3. ui->setupUi(this);
    4.  
    5. theMapper = new QSignalMapper(this);
    6. QGridLayout *theLayout = new QGridLayout();
    7.  
    8. int count = 0;
    9. for (int t = 0; t < 10 ; t++)
    10. {
    11. count++;
    12. QString buttonName;
    13. buttonName.setNum(count);
    14. buttonName.prepend("Button ");
    15. QPushButton *theButton = new QPushButton(buttonName);
    16. theLayout->addWidget(theButton,count,1,1,1);
    17. connect(theButton, SIGNAL(clicked()), theMapper, SLOT(map()));
    18. theMapper->setMapping(theButton,buttonName);
    19. }
    20.  
    21. centralWidget()->setLayout(theLayout);
    22. connect(theMapper,SIGNAL(mapped(const QString &)),this,SIGNAL(clicked(const QString &)));
    23.  
    24. }
    25.  
    26. void clicked(const QString &text)
    27. {
    28.  
    29. qDebug() << text;
    30. }
    31.  
    32. MainWindow::~MainWindow()
    33. {
    34. delete ui;
    35. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by high_flyer; 26th November 2012 at 11:57. Reason: code tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSignalMapper question

    1. Please use code tags in the future.
    2. What is the question?
    3. Define "working".
    4. What is it that you are trying to achieve?
    5. If I understand your code correctly the use of a signal mapper is not needed.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSignalMapper question

    showing the header would be useful - is void clicked(const QString &text) defined as a slot?
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  4. #4
    Join Date
    Nov 2012
    Posts
    24
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSignalMapper question

    No void clicked(const QString &text) is defined as a signal

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QtGui>
    6. #include <QGridLayout>
    7. #include <QSignalMapper>
    8.  
    9. namespace Ui {
    10. class MainWindow;
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20.  
    21. signals:
    22. void clicked(const QString &text);
    23.  
    24. protected:
    25. void changeEvent(QEvent *e);
    26.  
    27. private:
    28. Ui::MainWindow *ui;
    29. QPushButton *theButton;
    30. QGridLayout *theLayout;
    31. QSignalMapper *theMapper;
    32. };
    33.  
    34. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 26th November 2012 at 15:05.

  5. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSignalMapper question

    didn't we already mention code tags??

    If clicked is a signal, then why do you have this
    Qt Code:
    1. void clicked(const QString &text)
    2. {
    3.  
    4. qDebug() << text;
    5. }
    To copy to clipboard, switch view to plain text mode 
    ??

    You can't have code for a signal.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. QSignalMapper
    By Ali Reza in forum Newbie
    Replies: 35
    Last Post: 30th November 2012, 09:12
  2. QSignalMapper
    By axisdj in forum Newbie
    Replies: 6
    Last Post: 16th September 2010, 01:52
  3. help with QSignalMapper and actions
    By andreime in forum Newbie
    Replies: 1
    Last Post: 9th August 2009, 18:24
  4. ? about QSignalMapper
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2008, 21:21
  5. QSignalMapper question: SIGNAL 2 int's
    By vonCZ in forum Newbie
    Replies: 5
    Last Post: 20th July 2007, 10:02

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.