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