2 Attachment(s)
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.
Attachment 10094
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,
Code:
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.
Attachment 10095
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:
Code:
http://stackoverflow.com/questions/14151443/how-to-pass-a-qstring-to-a-qt-slot-from-a-qmenu-via-qsignalmapper-or-otherwise/14157471#14157471
Here's the code which requires 4-5 steps, which I am not getting, to be added for making it work:
mainwindow.h
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QDebug>
#include <QSignalMapper>
namespace Ui {
class MainWindow;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
void createActions();
private slots:
void interval();
void help();
int setAreaThreshold();
};
#endif // MAINWINDOW_H
mainwindow.cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "intervaldialog.h"
//#include "help.h"
int i =25;
MainWindow
::MainWindow(QWidget *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()));
connect(ui
->menuArea_Threshold,
SIGNAL(triggered
(QAction*)),
this,
SLOT(setAreaThreshold
()));
ui->menuHelp->setStatusTip(tr("help "));
}
void MainWindow::interval()
{
qDebug()<<"inside interval qdialog";
}
int MainWindow::setAreaThreshold()
{
qDebug()<<"setting area threshold";
// qDebug()<<str;
// int i = str.toInt();
// qDebug()<<i;
// QString areaThresholdString;
// areaThresholdString = ui->action25_sec->text();
// switch (areaThresholdString) {
// case 25:
// qDebug()<<"25";
// break;
// default:
// qDebug()<<"default";
// break;
// }
return 0;
}
void MainWindow::help()
{
Help help;
help.exec();
}
main.cpp
Code:
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
Re: How to use QSignalMapper for a list of QAction items
Something like:
Code:
connect(pSignalMapper, SIGNAL(mapped(int)), SLOT(setAreaThreshold(int)));
connect(ui->action25, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
connect(ui->action50, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
connect(ui->action100, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
connect(ui->action150, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
connect(ui->action200, SIGNAL(triggered()), pSignalMapper, SLOT(map()));
pSignalMapper->setMapping(ui->action25, 25);
pSignalMapper->setMapping(ui->action50, 50);
pSignalMapper->setMapping(ui->action100, 100);
pSignalMapper->setMapping(ui->action150, 150);
pSignalMapper->setMapping(ui->action200, 200);
// and a slot
void setAreaThreshold(int value) {
}
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
Code:
ui->action25->setData(25);
.....
connect(ui
->menuArea_Threshold,
SIGNAL(triggered
(QAction*)),
this,
SLOT(setAreaThreshold
(QAction*)));
Code:
{
int value = action->data().toInt();
}
Cheers,
_
Re: How to use QSignalMapper for a list of QAction items
Thanks anda_skoa, must remember that variation.