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,
http://qt-project.org/doc/qt-4.8/qsignalmapper.html
http://doc.qt.digia.com/qq/qq10-signalmapper.html#thesignalmapperapproach
http://qt-project.org/doc/qt-4.8/qsignalmapper.html
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:
http://stackoverflow.com/questions/14151443/how-to-pass-a-qstring-to-a-qt-slot-from-a-qmenu-via-qsignalmapper-or-otherwise/14157471#14157471
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
#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
#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
To copy to clipboard, switch view to plain text mode
mainwindow.cpp
#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();
}
#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();
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
MainWindow w;
w.show();
return a.exec();
}
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks