QT正在使用另一个类的公共插槽



我有一个类ArrayToolBar,它有一个公共成员commandBox和一个公共函数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((是CommandBox类中的一个公共槽。

我的问题是我得到了一个错误:不存在这样的插槽。这是因为我在ArrayToolBar中使用了其他类的插槽吗?有办法绕过吗?

您可以将新的连接语法用于labmda表达式。

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()))

最新更新