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.
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,
http://qt-project.org/doc/qt-4.8/qsignalmapper.html
http://doc.qt.digia.com/qq/qq10-signalmapper.html#thesignalmapperapproach
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.
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:
https://stackoverflow.com/questions/14151443/how-to-pass-a-qstring-to-a-qt-slot-from-a-qmenu-via-qsignalmapper-or-otherwise/14157471#14157471
***********************************UPDATED SECTION(as of Mar 3, '14)************************************************************************
My problem is quite similar to following question: How to add a list of QActions to a QMenu and handle them with a single slot?
I too, tried using setData in helpAction(QAction *pAction)
, but I am still mistaken since the only value that I get when I click on the action items in the area threshold sub menu is
0
So my updated question is, how to use the setData(const QVariant &userData)
to assign indices to my action items. Please advise.
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QSignalMapper>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void subMenuForThresholds();
private:
Ui::MainWindow *ui;
void createActions();
QSignalMapper *pSignalMapper;
private slots:
void interval();
void help();
void helpAction(QAction *pAction);
void setAreathreshold(int value);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "intervaldialog.h"
#include "help.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
createActions();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::createActions()
{
ui->actionInterval->setStatusTip(tr("Set the interval for capturing delta & reference images"));
connect(ui->actionInterval, SIGNAL(triggered()), this, SLOT(interval()));
subMenuForThresholds();
ui->menuHelp->setStatusTip(tr("help "));
// connect(ui->menuHelp, SIGNAL(triggered(QAction*)), this, SLOT(help()));
}
void MainWindow::subMenuForThresholds()
{
pSignalMapper = new QSignalMapper(this);
// connect(pSignalMapper, SIGNAL(mapped(int)), this, SIGNAL(/*not sure what to place here?*/);
connect(ui->menuArea_Threshold, SIGNAL(triggered(QAction*)), this, SLOT(helpAction(QAction*)));
}
void MainWindow::setAreaThreshold()
{
qDebug()<<value;
}
void MainWindow::interval()
{
qDebug()<<"inside interval qdialog";
Help help;
help.exec();
}
void MainWindow::help()
{
qDebug()<<"inside help qdialog";
Help help;
help.exec();
}
void MainWindow::helpAction(QAction *pAction)
{
ui->action25_sec->setData(10);
int value = pAction->data().toInt();
qDebug()<<value;
}
This helped me out.
QSignalMapper *pSignalMapper = new QSignalMapper(this);
connect(pSignalMapper, SIGNAL(mapped(int)), SLOT(setAreaThreshold(int)));
connect(ui->action25_s, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
connect(ui->action50_sec, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
connect(ui->action100_sec, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
connect(ui->action150_sec, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
connect(ui->action200_sec, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
pSignalMapper->setMapping(ui->action25_s, 25);
pSignalMapper->setMapping(ui->action50_sec, 50);
pSignalMapper->setMapping(ui->action100_sec, 100);
pSignalMapper->setMapping(ui->action150_sec, 150);
pSignalMapper->setMapping(ui->action200_sec, 200);
// and a slot
void setAreaThreshold(int value) {
qDebug()<<value;
}