Sunit Gautam
Sunit Gautam

Reputation: 6045

QT using public slot of another class

I have a class ArrayToolBar which has a public member commandBox and a public function createArray().

class ArrayToolBar : public QToolBar
{
    Q_OBJECT

public:
    explicit ArrayToolBar(const QString &title, QWidget *parent);
    CommandBox* commandBox = new CommandBox(); 
    void createArray();

Here is how createArray() is defined

void ArrayToolBar::createArray(){
    commandBox->setFocus();
    connect(commandBox, SIGNAL(returnPressed()), this, SLOT(commandBox->SubmitCommand()));
}

SubmitCommand() is a public slot in CommandBox class.

My problem is that I am getting an error : No such slot exists. Is this because I have used a slot of some other class in ArrayToolBar? Is there a way around?

Upvotes: 0

Views: 61

Answers (2)

Captain GouLash
Captain GouLash

Reputation: 1287

You can use lambda expressions like already mentioned.

But this should do what you want without lambda:

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

Upvotes: 0

Andrey Semenov
Andrey Semenov

Reputation: 971

You can use new connection syntax with labmda expressions.

Qt has a good aricle about it. https://wiki.qt.io/New_Signal_Slot_Syntax

And final code will looks like this:

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

Upvotes: 1

Related Questions