bJing
bJing

Reputation: 1

Connecting class function to a button.(QT C++)

I am curious how to connect a function in a class to a button in QT. I am trying to use this one:

connect(ui->m_but,SIGNAL(clicked()),&Downloader,SLOT(DoDownload()));

I have a class Downloader. A button name m_but. Function DoDownload to call.

Also tried to make a slot function in my class which calls DoDownload. But no result.

Upvotes: 0

Views: 77

Answers (1)

dtech
dtech

Reputation: 49309

I have a class Downloader.

But do you have an instance of it? You do need to instantiate the class in order to connect to it.

You should have something like this in your class body:

Downloader downloader;

Then connect(ui->m_but, SIGNAL(clicked()), &downloader, SLOT(DoDownload())); should work.

In the case Downloader is a "static class", that is it has no non-static members, then it can be used without creating an instance of it. In that case, you'd have to use the new connection syntax, available in Qt 5:

connect(ui->m_but, &QPushButton::clicked, Downloader::DoDownload);

Upvotes: 1

Related Questions