I
I
Igor Mitrakov2018-02-06 21:15:40
C++ / C#
Igor Mitrakov, 2018-02-06 21:15:40

Slots in Qt C++?

Hello.
Started learning C++ QT. Difficulties arose during the study of signals and slots.
Please tell me what's wrong?
error: no matching function for call to 'QObject::connect(QPushButton*&, const char*, myClass&, const char*)'
QObject::connect(myObj.entr_butt, SIGNAL(clicked(bool)), myObj, SLOT( mySlot();
myclass.h:

#include <QWidget>
#include <QPushButton>
#include <QTextEdit>
#include <QVBoxLayout>


class myClass : public QObject
{
    Q_OBJECT
public:
    QPushButton *close_butt = new QPushButton("Закрыть");
    QPushButton *entr_butt = new QPushButton("Ввод");
    QPushButton *clr_butt = new QPushButton("Очистить");
    QTextEdit *text_Edit = new QTextEdit;
    QVBoxLayout *lay = new QVBoxLayout();
    QWidget *window = new QWidget();
    void func()
    {
        lay->addWidget(text_Edit);
        lay->addWidget(entr_butt);
        lay->addWidget(clr_butt);
        lay->addWidget(close_butt);
        window->setLayout(lay);
        window->show();
    }

public slots:
    mySlot(){
        text_Edit->setText("Слот вызван");
    }

};

main.cpp:
#include <QApplication>
#include <myclass.h>
#include <QObject>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    myClass myObj;
    myObj.func();
    QObject::connect(myObj.entr_butt, SIGNAL(clicked(bool)), myObj, SLOT(mySlot()));
    QObject::connect(myObj.close_butt, SIGNAL(clicked(bool)), myObj.window;, SLOT(close());
    return a.exec();
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2018-02-06
@igormitrakov

Need an & before the first myObj.
Otherwise, I will not comment on the code, because. a lot and for a long time.

A
Alexander Taratin, 2018-02-09
@Taraflex

Starting with version 5 with support for c++11 enabled, slots are not needed at all, since it has become possible to pass a reference to any class function as a slot and not only. Performance will be higher + more compile-time checks for signal and slot compatibility
https://wiki.qt.io/New_Signal_Slot_Syntax/en
When creating descendant classes from QObject, it is better to always pass the parent so that there is no need to write delete in the destructor.
doc.qt.io/qt-5/objecttrees.html
doc.qt.io/qt-5/qobject.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question