开发者

Can't identify source of signalmap error

开发者 https://www.devze.com 2023-03-13 23:22 出处:网络
I\'ve got the following code: QSignalMapper* signalMapper = new QSignalMapper (this) ; ttAct = new QAction(tr(\"Ttime\"), this);

I've got the following code:

QSignalMapper* signalMapper = new QSignalMapper (this) ;

ttAct = new QAction(tr("Ttime"), this);
ttAct->setCheckable(true);
ttAct->setChecked(true);
connect(ttAct, SIGNAL(triggered()), signalMapper, SLOT( map() ));

plAct = new QAction(tr("Length"), this);
plAct->setCheckable(true);
plAct->setChecked(true);
connect(plAct, SIGNAL(triggered()), signalMapper, SLOT( map() ));

signalMapper->setMapping(ttAct,     0);
signalMapper->setMapping(plAct,     1);


connect (signalMapper, SIGNAL( mapped(int) ), this, SLOT(enableM(int))) ;

where enableM is

void MainWindow::enableM(int i){
    qDebug() << i;
}

is a private slot, just printing. The code compiles fine, but when I try to run it, it fails.

As far as i can tell from qDebug statements, it fails on the line

mMenu->addAction(ttAct);

(mMenu-->addAction(plAct); is the next line after that)

Any ideas on what I'm missing?

Many thanks.

/***********************************************/

/***********************************************/

/***********************************************/

Edit: If you open up the 开发者_如何学运维example code in Qt, Main Windows -> Menus, and add teh following code, you should get to the same situation:

To mainwindow.h: add

void enableM(int);

to private slots

add

QMenu *metricMenu;
QAction *ttAct;
QAction *plAct;

to private.

In mainwindow.cpp, add

#include <QSignalMapper>

at the top, and then add

QSignalMapper* signalMapper = new QSignalMapper (this) ;

ttAct = new QAction(tr("Ttime"), this);
ttAct->setCheckable(true);
ttAct->setChecked(true);
connect(ttAct, SIGNAL(triggered()), signalMapper, SLOT( map() ));

plAct = new QAction(tr("Length"), this);
plAct->setCheckable(true);
plAct->setChecked(true);
connect(plAct, SIGNAL(triggered()), signalMapper, SLOT( map() ));


signalMapper->setMapping(ttAct,     0);
signalMapper->setMapping(plAct,     1);

connect (signalMapper, SIGNAL( mapped(int) ), this, SLOT(enableM(int))) ;

to the createActions() method.

Add the following

fileMenu = menuBar()->addMenu(tr("&Metrics"));
metricMenu->addAction(ttAct);
metricMenu->addAction(plAct);

to the createMenus() method.

Finally add the following function anywhere

void MainWindow::enableM(int i){
    infoLabel->setText(tr("I'm alive! " + i));
}

then you should get the same error I get.


make some breakpoints and run in debug mode. bug show itself


Well, managed to find the answer:

fileMenu = menuBar()->addMenu(tr("&Metrics"));
metricMenu->addAction(ttAct);
metricMenu->addAction(plAct);

is only almost correct. Shoulda been

metricMenu = menuBar()->addMenu(tr("&Metrics"));
metricMenu->addAction(ttAct);
metricMenu->addAction(plAct);

instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消