Results 1 to 7 of 7

Thread: QSignalMapper

  1. #1
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default QSignalMapper

    here is what i have, just doing some test to get familiar with QT Creator. This compiles but when I click one of the buttons the clicked signal does not trigger. Any Ideas?

    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 

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QDebug>
    4.  
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11.  
    12. theMapper = new QSignalMapper(this);
    13. QGridLayout *theLayout = new QGridLayout();
    14.  
    15. int count = 0;
    16. for (int t = 0; t < 10 ; t++)
    17. {
    18. count++;
    19. QString buttonName;
    20. buttonName.setNum(count);
    21. buttonName.prepend("Sequence");
    22. QPushButton *theButton = new QPushButton(buttonName);
    23. theLayout->addWidget(theButton,count,1,1,1);
    24. connect(theButton, SIGNAL(clicked()), theMapper, SLOT(map()));
    25. theMapper->setMapping(theButton,buttonName);
    26. }
    27.  
    28. centralWidget()->setLayout(theLayout);
    29. connect(theMapper,SIGNAL(mapped(const QString &)),this,SIGNAL(clicked(const QString &)));
    30.  
    31. }
    32.  
    33. void clicked(const QString &text)
    34. {
    35.  
    36. qDebug() << text;
    37. }
    38.  
    39. MainWindow::~MainWindow()
    40. {
    41. delete ui;
    42. }
    43.  
    44. void MainWindow::changeEvent(QEvent *e)
    45. {
    46. QMainWindow::changeEvent(e);
    47. switch (e->type()) {
    48. case QEvent::LanguageChange:
    49. ui->retranslateUi(this);
    50. break;
    51. default:
    52. break;
    53. }
    54. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSignalMapper

    Make your "clicked" a slot, not a signal.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSignalMapper

    Thanks you so much, the QT documentation(QSignalMapper) I used, stated to use signal... changed to slot everything works now. Much appreciated

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSignalMapper

    Quote Originally Posted by axisdj View Post
    Thanks you so much, the QT documentation(QSignalMapper) I used, stated to use signal...
    No, it didn't, read again.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSignalMapper

    I must be missing something, I am a complete newbie, but here is the sample code from the QSignalMapper

    Qt Code:
    1. class ButtonWidget : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. ButtonWidget(QStringList texts, QWidget *parent = 0);
    7.  
    8. signals:
    9. void clicked(const QString &text); ///Uses Signals???
    10.  
    11. private:
    12. QSignalMapper *signalMapper;
    13. };
    14. The only function that we need to implement is the constructor:
    15.  
    16. ButtonWidget::ButtonWidget(QStringList texts, QWidget *parent)
    17. : QWidget(parent)
    18. {
    19. signalMapper = new QSignalMapper(this);
    20.  
    21. QGridLayout *gridLayout = new QGridLayout;
    22. for (int i = 0; i < texts.size(); ++i) {
    23. QPushButton *button = new QPushButton(texts[i]);
    24. connect(button, SIGNAL(clicked()), signalMapper, SLOT(map()));
    25. signalMapper->setMapping(button, texts[i]);
    26. gridLayout->addWidget(button, i / 3, i % 3);
    27. }
    28.  
    29. connect(signalMapper, SIGNAL(mapped(const QString &)),
    30. this, SIGNAL(clicked(const QString &))); ////Notice it says SIGNAL????
    31.  
    32. setLayout(gridLayout);
    33. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSignalMapper

    This is a completely different situation. Notice that in the docs the "clicked" method is not implemented. It is a signal which is then connected to some other slot (which is not part of the example). Since you implemented "clicked" this can't be a signal (and the compiler should refuse to build such a program). It would be ok to use a signal if you moved the body of "clicked" to another method (slot), most probably in a different object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Apr 2010
    Posts
    13
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSignalMapper

    Thanks again for taking the time to explain, i really appreciate it.

Similar Threads

  1. QSignalMapper and argument problems
    By harmodrew in forum Newbie
    Replies: 14
    Last Post: 7th August 2010, 20:20
  2. double QSignalMapper with QTcpServer/QTcpSocket
    By radeberger in forum Qt Programming
    Replies: 5
    Last Post: 5th May 2010, 19:46
  3. help with QSignalMapper and actions
    By andreime in forum Newbie
    Replies: 1
    Last Post: 9th August 2009, 19:24
  4. ? about QSignalMapper
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2008, 22:21
  5. QSignalMapper question: SIGNAL 2 int's
    By vonCZ in forum Newbie
    Replies: 5
    Last Post: 20th July 2007, 11: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.