Qt - “没有这样的槽”,虽然有 [英] Qt - "No such slot", although there is

查看:140
本文介绍了Qt - “没有这样的槽”,虽然有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将一个按钮连接到一个函数,所以当我按下按钮,该函数被调用具体的参数。我有

  class FieldGridWidget:public QWidget 
{
Q_OBJECT

public :
FieldGridWidget(QWidget * parent = 0);
〜FieldGridWidget();

public slots:
void resizeGrid(int n);

private:
QGridLayout * _gridLayout;
QVector< QPushButton *> _buttonGrid;

};

然后按钮

  _button3 = new QPushButton(3x3,this); 

我试图连接它,所以如果点击, resizeGrid connect(_button3,SIGNAL)。$ {code} (clicked()),_fieldGrid,SLOT(resizeGrid(3))); //这是第21行

但我得到运行时错误
QObject :: connect:没有这样的插槽FieldGridWidget :: resizeGrid(3)in ../ filename.cpp:21



做错了?或者我怎么能这样,如果我按下按钮, resizeGrid(3)被调用?
谢谢!

解决方案

您不能将值直接传递到Qt中的插槽,如 SLOT(resizeGrid(3)) SLOT 的参数应该只是您的方法的签名( resizeGrid())。



有两种方法可以将参数添加到插槽。关于如何将参数传递到广告位的问题显示了一些解决方案,此处重复。 (Go upvote them!)



现代的方式:Qt5和C ++ 11



插槽,连接到函子,如 Kuba Ober 此回答

  connect(action1,& QAction :: triggered, this,[this] {onStepIncreased(1);}); 
connect(action5,& QAction :: triggered,this,[this] {onStepIncreased(5);});
connect(action10,& QAction :: triggered,this,[this] {onStepIncreased(10);});
connect(action25,& QAction :: triggered,this,[this] {onStepIncreased(25);});
connect(action50,& QAction :: triggered,this,[this] {onStepIncreased(50);});



旧版:QSignalMapper



使用 QSignalMapper 按照 TonyK 这个回答

  QSignalMapper * signalMapper = new QSignalMapper 
connect(action1,SIGNAL(triggered()),signalMapper,SLOT(map()));
connect(action5,SIGNAL(triggered()),signalMapper,SLOT(map()));
connect(action10,SIGNAL(triggered()),signalMapper,SLOT(map()));
connect(action25,SIGNAL(triggered()),signalMapper,SLOT(map()));
connect(action50,SIGNAL(triggered()),signalMapper,SLOT(map()));

signalMapper - > setMapping(action1,1);
signalMapper - > setMapping(actions5,5);
signalMapper - > setMapping(action10,10);
signalMapper - > setMapping(action25,25);
signalMapper - > setMapping(action50,50);

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


I'm trying to connect a button to a function, so when I push the button, the function gets called with a specific parameter. I have

class FieldGridWidget : public QWidget
{
    Q_OBJECT

public:
    FieldGridWidget(QWidget *parent=0);
    ~FieldGridWidget();

public slots:
    void resizeGrid(int n);

private:
    QGridLayout* _gridLayout;
    QVector<QPushButton*> _buttonGrid;

};

then the button

_button3 = new QPushButton("3x3", this);

and I'm trying to connect it so if clicked, the resizeGrid function gets called with the parameter 3. For this, I'm trying

connect(_button3, SIGNAL(clicked()), _fieldGrid, SLOT(resizeGrid(3))); //this is line 21

but I get the runtime error QObject::connect: No such slot FieldGridWidget::resizeGrid(3) in ../filename.cpp:21

What am I doing wrong? Or how can I make it so if I press the button, resizeGrid(3) gets called? Thank you!

解决方案

You cannot pass a value to a slot in Qt directly, as in SLOT(resizeGrid(3)). The argument to SLOT should be just the signature of your method (resizeGrid()).

There are two ways how to add an argument to a slot. This question about how to pass arguments to a slot shows some solutions, repeated here. (Go upvote them!)

The modern way : Qt5 and C++11

Instead of connecting a slot, connect to functor, as described by Kuba Ober in this answer:

connect(action1,  &QAction::triggered, this, [this]{ onStepIncreased(1); });
connect(action5,  &QAction::triggered, this, [this]{ onStepIncreased(5); });
connect(action10, &QAction::triggered, this, [this]{ onStepIncreased(10); });
connect(action25, &QAction::triggered, this, [this]{ onStepIncreased(25); });
connect(action50, &QAction::triggered, this, [this]{ onStepIncreased(50); });

Old fashioned : QSignalMapper

You can use a QSignalMapper to perform what you want, as described by TonyK in this answer:

QSignalMapper* signalMapper = new QSignalMapper (this) ;
connect (action1, SIGNAL(triggered()), signalMapper, SLOT(map())) ;
connect (action5, SIGNAL(triggered()), signalMapper, SLOT(map())) ;
connect (action10, SIGNAL(triggered()), signalMapper, SLOT(map())) ;
connect (action25, SIGNAL(triggered()), signalMapper, SLOT(map())) ;
connect (action50, SIGNAL(triggered()), signalMapper, SLOT(map())) ;

signalMapper -> setMapping (action1, 1) ;
signalMapper -> setMapping (action5, 5) ;
signalMapper -> setMapping (action10, 10) ;
signalMapper -> setMapping (action25, 25) ;
signalMapper -> setMapping (action50, 50) ;

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

这篇关于Qt - “没有这样的槽”,虽然有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆