首页 文章

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

提问于
浏览
0

我有一个 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 中使用了一些其他类的插槽吗?有办法吗?

2 回答

  • 0

    您可以使用已经提到的lambda表达式 .

    但这应该做你想要的没有lambda:

    connect(commandBox, SIGNAL(returnPressed()), commandBox, SLOT(SubmitCommand()))
    
  • 1

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

    Qt有一个很好的论点 . https://wiki.qt.io/New_Signal_Slot_Syntax

    最终代码如下所示:

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

相关问题