Results 1 to 4 of 4

Thread: How to use QSignalMapper for a list of QAction items

  1. #1
    Join Date
    Feb 2014
    Posts
    11
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default How to use QSignalMapper for a list of QAction items

    I am developing a code snippet to perform similar, not identical actions when I click on the sub menu item list. Please check the attached snapshot. QSignalMapper is the best solution to connect multiple signals to the same slot.
    snap1.jpg

    But I am not able to exactly place, which signal is to be called for which slot. I have read a lot of theory about QSignalMapper,
    Qt Code:
    1. http://qt-project.org/doc/qt-4.8/qsignalmapper.html
    2. http://doc.qt.digia.com/qq/qq10-signalmapper.html#thesignalmapperapproach
    To copy to clipboard, switch view to plain text mode 

    even implemented their codes. Unlike the mentioned sample programs, my QAction objects apparently cannot be defined like we define elements inside an array, coz their names were auto-generated by the design window.
    snap2.jpg

    I am not able to understand, what should I place as SIGNAL here, and when should I use the setMapping function? If I use setMapping function, which parameters should I implement? I am simply not getting the concept thorough, not knowing what to do, whom to ask and making the mistake in my code here. Can you please advise me what I am doing wrong? I checked this for reference coz he had similar issue:
    Qt Code:
    1. http://stackoverflow.com/questions/14151443/how-to-pass-a-qstring-to-a-qt-slot-from-a-qmenu-via-qsignalmapper-or-otherwise/14157471#14157471
    To copy to clipboard, switch view to plain text mode 

    Here's the code which requires 4-5 steps, which I am not getting, to be added for making it work:

    mainwindow.h
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QDebug>
    6. #include <QSignalMapper>
    7.  
    8. namespace Ui {
    9. class MainWindow;
    10. }
    11.  
    12. class MainWindow : public QMainWindow
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19.  
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23. void createActions();
    24.  
    25. private slots:
    26. void interval();
    27. void help();
    28. int setAreaThreshold();
    29.  
    30. };
    31.  
    32. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. //#include "intervaldialog.h"
    4. //#include "help.h"
    5.  
    6. int i =25;
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. createActions();
    13. }
    14.  
    15. MainWindow::~MainWindow()
    16. {
    17. delete ui;
    18. }
    19.  
    20. void MainWindow::createActions()
    21. {
    22. ui->actionInterval->setStatusTip(tr("Set the interval for capturing delta & reference images"));
    23. connect(ui->actionInterval, SIGNAL(triggered()), this, SLOT(interval()));
    24.  
    25. QSignalMapper *pSignalMapper = new QSignalMapper(this);
    26. connect(ui->menuArea_Threshold, SIGNAL(triggered(QAction*)), this, SLOT(setAreaThreshold()));
    27.  
    28.  
    29. ui->menuHelp->setStatusTip(tr("help "));
    30. }
    31.  
    32. void MainWindow::interval()
    33. {
    34. qDebug()<<"inside interval qdialog";
    35. }
    36.  
    37. int MainWindow::setAreaThreshold()
    38. {
    39. qDebug()<<"setting area threshold";
    40.  
    41. // qDebug()<<str;
    42. // int i = str.toInt();
    43. // qDebug()<<i;
    44. // QString areaThresholdString;
    45. // areaThresholdString = ui->action25_sec->text();
    46. // switch (areaThresholdString) {
    47. // case 25:
    48. // qDebug()<<"25";
    49. // break;
    50. // default:
    51. // qDebug()<<"default";
    52. // break;
    53. // }
    54.  
    55. return 0;
    56. }
    57.  
    58. void MainWindow::help()
    59. {
    60. Help help;
    61. help.exec();
    62. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include <QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to use QSignalMapper for a list of QAction items

    Something like:
    Qt Code:
    1. QSignalMapper *pSignalMapper = new QSignalMapper(this);
    2. connect(pSignalMapper, SIGNAL(mapped(int)), SLOT(setAreaThreshold(int)));
    3. connect(ui->action25, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
    4. connect(ui->action50, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
    5. connect(ui->action100, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
    6. connect(ui->action150, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
    7. connect(ui->action200, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
    8. pSignalMapper->setMapping(ui->action25, 25);
    9. pSignalMapper->setMapping(ui->action50, 50);
    10. pSignalMapper->setMapping(ui->action100, 100);
    11. pSignalMapper->setMapping(ui->action150, 150);
    12. pSignalMapper->setMapping(ui->action200, 200);
    13.  
    14. // and a slot
    15. void setAreaThreshold(int value) {
    16.  
    17. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to ChrisW67 for this useful post:

    Sai Kamat (4th March 2014)

  4. #3
    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: How to use QSignalMapper for a list of QAction items

    Or setting the value as the action's custom data and retrieving it in the slot

    Qt Code:
    1. ui->action25->setData(25);
    2. .....
    3. connect(ui->menuArea_Threshold, SIGNAL(triggered(QAction*)), this, SLOT(setAreaThreshold(QAction*)));
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. setAreaThreshold(QAction *action)
    2. {
    3. int value = action->data().toInt();
    4. }
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  5. The following user says thank you to anda_skoa for this useful post:

    ChrisW67 (4th March 2014)

  6. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to use QSignalMapper for a list of QAction items

    Thanks anda_skoa, must remember that variation.

Similar Threads

  1. How to use list of items?
    By Squall in forum Qt Programming
    Replies: 1
    Last Post: 19th March 2012, 01:22
  2. Replies: 1
    Last Post: 23rd April 2011, 18:33
  3. How to align list items to the center?
    By zgulser in forum Qt Tools
    Replies: 4
    Last Post: 9th February 2009, 10:52
  4. Comparing Items In List Box
    By kenny_isles in forum Qt Programming
    Replies: 9
    Last Post: 21st February 2007, 14:06
  5. delete items from list box
    By vvbkumar in forum Qt Programming
    Replies: 4
    Last Post: 23rd June 2006, 20:08

Tags for this Thread

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.