PDA

View Full Version : How to use QSignalMapper for a list of QAction items



Sai Kamat
3rd March 2014, 06:04
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.
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,

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.
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:

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

#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();


private:
Ui::MainWindow *ui;
void createActions();

private slots:
void interval();
void help();
int setAreaThreshold();

};

#endif // MAINWINDOW_H

mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"
//#include "intervaldialog.h"
//#include "help.h"

int i =25;
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()));

QSignalMapper *pSignalMapper = new QSignalMapper(this);
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


#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

ChrisW67
3rd March 2014, 10:24
Something like:


QSignalMapper *pSignalMapper = new QSignalMapper(this);
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) {

}

anda_skoa
3rd March 2014, 15:49
Or setting the value as the action's custom data and retrieving it in the slot



ui->action25->setData(25);
.....
connect(ui->menuArea_Threshold, SIGNAL(triggered(QAction*)), this, SLOT(setAreaThreshold(QAction*)));



setAreaThreshold(QAction *action)
{
int value = action->data().toInt();
}


Cheers,
_

ChrisW67
4th March 2014, 11:45
Thanks anda_skoa, must remember that variation.