PDA

View Full Version : How to use a slot in Qt like a function in C++



franky
20th October 2016, 15:20
I have a small exercise purely in C++ with a slot this way:

Calculator.h:


#ifndef CALCULATOR_H
#define CALCULATOR_H

#include <QDialog>

class QLineEdit;
class QPushButton;

class Calculator : public QDialog
{
Q_OBJECT

public:
explicit Calculator(QWidget *parent = 0);
private slots:
void myslot(QString);
private:
QLineEdit* lineEdit;
QPushButton* oneButton;
QPushButton* twoButton;
QPushButton* quitButton;
};

#endif // CALCULATOR_H


Calculator.cpp is this:


#include "calculator.h"
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>

Calculator::Calculator(QWidget *parent)
: QDialog(parent)
{
lineEdit = new QLineEdit;
oneButton = new QPushButton("1");
twoButton = new QPushButton("2");
quitButton = new QPushButton("Quit");

QVBoxLayout* vbox = new QVBoxLayout;
vbox -> addWidget (lineEdit);
vbox -> addWidget (oneButton);
vbox -> addWidget (twoButton);
vbox -> addWidget (quitButton);

setLayout(vbox);

QString st1 = "1";
QString st2 = "2";
connect(oneButton, SIGNAL(clicked()), this, SLOT(myslot(st1)));
connect(twoButton, SIGNAL(clicked()), this, SLOT(myslot(st2)));
connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
}

void Calculator::myslot(QString str)
{
lineEdit -> setText(str);
}



And main.cpp is this way:


#include <QApplication>
#include "calculator.h"

int main(int argc, char* argv[])
{
QApplication app(argc, argv);
Calculator calculator;
calculator.show();
return app.exec();
}


The app runs without errors but nothing will be shown in the lineEdit when clicking oneButton and twoButton! And also there are warning messages in the Application Output saying:


QObject::connect: No such slot Calculator::myslot(st1) in ..\Calculator\calculator.cpp:24
QObject::connect: No such slot Calculator::myslot(st2) in ..\Calculator\calculator.cpp:25

What is the reason and how to use a slot in Qt like a function in C++ please?

Lesiok
20th October 2016, 15:53
You can't simply connect signal without parameters to slot with parameters. Read about QSignalMapper.

anda_skoa
20th October 2016, 17:22
QObject::connect: No such slot Calculator::myslot(st1) in ..\Calculator\calculator.cpp:24
QObject::connect: No such slot Calculator::myslot(st2) in ..\Calculator\calculator.cpp:25

What is the reason and how to use a slot in Qt like a function in C++ please?

The reason is, as the output already said, no slot in Calculator that has the name "myslot" and takes an argument of type "st1".

Calculator has a slot named "myslot" but it takes a QString, not a st1.

To call a slot like function you do exactly that: call it like a function



myslot("foo");


Cheers,
_

franky
20th October 2016, 22:51
Thank you for the replies.

But both st1 and st2 are of type Qstring.
And I like to call that slot when I click on the buttons. So what do I need to do for that purpose please?

Lesiok
21st October 2016, 06:48
Thank you for the replies.

But both st1 and st2 are of type Qstring.
And I like to call that slot when I click on the buttons. So what do I need to do for that purpose please?
Read about QSignalMapper. Example in documentation is exactly your problem.

anda_skoa
21st October 2016, 18:17
But both st1 and st2 are of type Qstring.

You put "st1" as the type argument of the slot signature, so the connect was looking for a slot with an argument of type "st1"



And I like to call that slot when I click on the buttons.
Since the clicked() signal as no argument (well, it has a bool), you connect it to a slot matching its signature.

Cheers,
_