Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem in using the Connect



  • Hi
    The problem in using the Connect function in the class after the call, the error:
    QObject :: connect: Incompatible sender / receiver arguments
    MyClass :: finished () -> MyClass :: login (QString, QString)
    My Code :

    //Qml Call run Function    
    MyButton
        {
            width: 250
            height: 45
            x: rect.width / 2 - 33
            y: 200
    
            onClicked: {
                //MyClass.login(txtUserName.text,txtPass.text)
                MyClass.run2()
            }
        }
    
    //MyClass.h
    #ifndef MYCLASS_H
    #define MYCLASS_H
    
    #include <QObject>
    #include <QQuickItem>
    #include <QtSql>
    #include <QThread>
    
    class MyClass : public QObject
    {
        Q_OBJECT
    
    public :
        MyClass();
        ~MyClass();
    public slots :
        void run2();
        void login(QString usr,QString psw);
        void connecttodb();
    signals :
        void started();
        void finished();
    
    };
    
    #endif // MYCLASS_H
    
    //MyClass.cpp
    #include "myclass.h"
    #include <qdebug.h>
    #include <QtSql>
    #include <QTimer>
    
    MyClass::MyClass()
    {
    
    }
    
    MyClass::~MyClass()
    {
    
    }
    
    void MyClass::run2()
    {
        MyClass *w = new MyClass();
        connect(w,SIGNAL(finished()),w,SLOT(login(QString,QString)));
    
    }
    
    void MyClass::login(QString usr,QString psw)
    {
    
    ....
    
        emit finished();
    
    
    }
    }
    

  • Qt Champions 2019

    The error already explains what the problem is: connect(w,SIGNAL(finished()),w,SLOT(login(QString,QString)));
    your slot expects to get two parameters, but the signal you're trying to connect does not have any parameters. So, the signal cannot pass parameters the slot expects.



  • @jsulm
    Thanks very much for guidance, I run function connect() with what to do?


  • Qt Champions 2019

    Somehow your code is really weird: in MyClass::run2 you create a new instance of the same class, then in login() slot you emit the signal which you want to connect to login() slot! Even if you would be able to connect finished() to login() you would get an infinite loop: call login() -> emit finished() -> login() is called -> emit finished() -> login() is called -> ...
    What do you want to do?


  • Qt Champions 2019

    @MrErfan I don't understand your question.
    You cannot connect a signal without parameter to a slot with parameter.
    Did you read http://doc.qt.io/qt-5.5/signalsandslots.html ?
    Either remove parameter from login() slot or add same parameter to finished() signal.


  • Qt Champions 2019

    @MrErfan
    I don't know what you want to do when login() is finished, but do not connect finished() to login()!

    MyClass::MyClass()
    {
        connect(this,SIGNAL(finished()),this,SLOT(onFinished()));
    }
    
    MyClass::~MyClass()
    {
    }
    
    void MyClass::run2()
    {
        login("...", "...");
    }
    
    void MyClass::login(QString usr,QString psw)
    {
    ....
        emit finished();
    }
    
    void MyClass::onFinished()
    {
        // No idea what you want to do here
    }
    

Log in to reply