首页 > 解决方案 > 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吗?有办法吗?

标签: qtsignals-slots

解决方案


您可以将新的连接语法与 labmda 表达式一起使用。

Qt 有一篇关于它的好文章。https://wiki.qt.io/New_Signal_Slot_Syntax

最终代码将如下所示:

connect(commandBox, &CommandBox::returnPressed,
        this, [=] () {commandBox->SubmitCommand();});

推荐阅读