QT 使用另一个 class 的 public 插槽
QT using public slot of another class
我有一个 class ArrayToolBar
,它有一个 public 成员 commandBox
和一个 public 函数 createArray()
.
class ArrayToolBar : public QToolBar
{
Q_OBJECT
public:
explicit ArrayToolBar(const QString &title, QWidget *parent);
CommandBox* commandBox = new CommandBox();
void createArray();
这里是 createArray()
的定义方式
void ArrayToolBar::createArray(){
commandBox->setFocus();
connect(commandBox, SIGNAL(returnPressed()), this, SLOT(commandBox->SubmitCommand()));
}
SubmitCommand() is a public slot in CommandBox
class.
我的问题是出现错误:不存在这样的插槽。
这是因为我在ArrayToolBar
中使用了其他class的插槽吗?有办法解决吗?
您可以对 lambda 表达式使用新的连接语法。
Qt 有一篇关于它的好文章。 https://wiki.qt.io/New_Signal_Slot_Syntax
最终代码如下所示:
connect(commandBox, &CommandBox::returnPressed,
this, [=] () {commandBox->SubmitCommand();});
您可以像已经提到的那样使用 lambda 表达式。
但是这应该在没有 lambda 的情况下做你想做的事:
connect(commandBox, SIGNAL(returnPressed()), commandBox, SLOT(SubmitCommand()))
我有一个 class ArrayToolBar
,它有一个 public 成员 commandBox
和一个 public 函数 createArray()
.
class ArrayToolBar : public QToolBar
{
Q_OBJECT
public:
explicit ArrayToolBar(const QString &title, QWidget *parent);
CommandBox* commandBox = new CommandBox();
void createArray();
这里是 createArray()
的定义方式
void ArrayToolBar::createArray(){
commandBox->setFocus();
connect(commandBox, SIGNAL(returnPressed()), this, SLOT(commandBox->SubmitCommand()));
}
SubmitCommand() is a public slot in
CommandBox
class.
我的问题是出现错误:不存在这样的插槽。
这是因为我在ArrayToolBar
中使用了其他class的插槽吗?有办法解决吗?
您可以对 lambda 表达式使用新的连接语法。
Qt 有一篇关于它的好文章。 https://wiki.qt.io/New_Signal_Slot_Syntax
最终代码如下所示:
connect(commandBox, &CommandBox::returnPressed,
this, [=] () {commandBox->SubmitCommand();});
您可以像已经提到的那样使用 lambda 表达式。
但是这应该在没有 lambda 的情况下做你想做的事:
connect(commandBox, SIGNAL(returnPressed()), commandBox, SLOT(SubmitCommand()))