如何使用QSignalMapper获取QAction项列表

时间:2014-03-01 10:29:46

标签: qt5 signals-slots qt-signals qmenu qaction

当我点击子菜单项列表时,我正在开发一个代码片段来执行类似的,不相同的操作。请检查附加的快照。 QSignalMapper是将多个信号连接到同一插槽的最佳解决方案。

I want to perform similar actions whenever the values in the Area Threshold sub menu are clicked

但我无法准确放置,哪个信号要调用哪个插槽。我已经阅读了很多关于QSignalMapper的理论,

http://qt-project.org/doc/qt-4.8/qsignalmapper.html
http://doc.qt.digia.com/qq/qq10-signalmapper.html#thesignalmapperapproach

甚至实施了他们的代码。 与上面提到的示例程序不同,我的QAction对象显然无法定义,就像我们在数组中定义元素一样,因为它们的名称是由设计窗口自动生成的。 Names of qAction objects cannot be placed in for loop as they aren't sequential per se

我无法理解,我应该在这里放置什么信号,什么时候应该使用setMapping函数?如果我使用setMapping函数,我应该实现哪些参数? 我根本没有让这个概念彻底,不知道该怎么做,在这里我的代码中要求和犯错误。你能告诉我我做错了什么吗? 我检查了这个以供参考,因为他有类似的问题:

https://stackoverflow.com/questions/14151443/how-to-pass-a-qstring-to-a-qt-slot-from-a-qmenu-via-qsignalmapper-or-otherwise/14157471#14157471

***************************************************************************************************************************************************************************************************** ************************************************** *********************

我的问题与以下问题非常相似:     How to add a list of QActions to a QMenu and handle them with a single slot?

我也尝试在helpAction(QAction *pAction)中使用setData,但我仍然错了,因为当我点击区域阈值子菜单中的操作项时,我得到的唯一值是

0

所以我更新的问题是,如何使用setData(const QVariant &userData)为我的操作项指定索引。请指教。

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

1 个答案:

答案 0 :(得分:1)

这帮助了我。

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