Object::connect: No such slot QApplication::MYSLOT()



  • Well, I've searched this many times in Google, and still I can't solve it, so I ask for some help.

    I've seen the documentation, and I think this is the way to declare a custom slot:

    My H file:
    @
    #ifndef ACTIVIDADESTIC_H
    #define ACTIVIDADESTIC_H

    #include <QMainWindow>
    #include <QMessageBox>

    namespace Ui {
    class ActividadesTIC;

    }

    class ActividadesTIC : public QMainWindow
    {
    Q_OBJECT

    public:
    ActividadesTIC(): manera() {}
    explicit ActividadesTIC(QWidget *parent = 0);
    ~ActividadesTIC();

    public slots:
    void easy();

    private:
    int manera;
    Ui::ActividadesTIC *ui;
    };

    #endif // ACTIVIDADESTIC_H

    @

    My main.cpp
    @

    void ActividadesTIC::easy() {
    ++manera;
    if (manera>0) {
    this->setWindowTitle("This is the easy way");
    }
    }

    class MyWidget : public QWidget
    {
    public:
    MyWidget(QWidget *parent = 0);
    };

    MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
    {
    setFixedSize(900, 700);

    QRadioButton *easyWay = new QRadioButton (tr("Easy Way"), this);
        easyWay->setFont(QFont("Arial", 11));
        easyWay->setGeometry(10, 40, 150, 50);
    
       connect(easyWay, SIGNAL(clicked()), this, SLOT(easy()));
    

    }

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

    QDir setPath("Documents");
    QApplication app(argc, argv);
        MyWidget widget;
        widget.show();
        return app.exec&#40;&#41;;
    

    }

    @

    Well, what i do in the void easy() doesn't really matters, its just a test to see if it works, but I keep getting Object::connect: No such slot QApplication::easy() in ..\ActividadesTIC\main.cpp

    So please, any help is welcome.



  • The problem is at this line:
    @connect(easyWay, SIGNAL(clicked()), qApp, SLOT(easy())); //the slot is in your class not in QApplication
    @
    So the correct 3rd argument of connect should be this (the instance of your class***):
    @connect(easyWay, SIGNAL(clicked()), this, SLOT(easy()));@

    LE:
    ***More correctly expressed: pointer to the instance



  • The point is that easy() is not a slot of QApplication but of your own class ActividadesTIC. Thus you should write

    @connect ( easyWay, SIGNAL(clicked()), this, SLOT(easy()) );@

    Edit: I shouldn't let myself get distracted by something else while writing a post... Took 6 minutes to long :D



  • Ok, so I've updated the program (& the post, you can check the "new code above"), but now I get this error: @Object::connect: No such slot QWidget::easy() in ..\ActividadesTIC\main.cpp:38@

    So now, i understand where's the problem. But how to fix it?

    NOTE: I tried: @connect ( easyWay, SIGNAL(clicked()), ActividadesTIC, SLOT(easy()) );@
    But it tells me: @expected primary expression before ',' token@



  • FINALLY!!!

    Got it working guys.

    Do you expect me to post the working code? I'm doing so anyway: (NOTE: some of it is in spanish)

    O!! BTW: THANKS!!

    MY H FILE

    @
    #ifndef ACTIVIDADESTIC_H
    #define ACTIVIDADESTIC_H

    #include <QMainWindow>
    #include <QMessageBox>

    namespace Ui {
    class ActividadesTIC;

    }

    class MyWidget : public QWidget
    {
    Q_OBJECT

    public:
    MyWidget(QWidget *parent = 0);

    public slots:
    void easy();

    private:
    int manera;
    };

    class ActividadesTIC : public QMainWindow
    {
    // Q_OBJECT

    public:

    explicit ActividadesTIC(QWidget *parent = 0);
    ~ActividadesTIC();
    

    private:
    Ui::ActividadesTIC *ui;
    };

    #endif // ACTIVIDADESTIC_H

    @

    My main.cpp:
    @
    #ifndef ACTIVIDADESTIC_H
    #define ACTIVIDADESTIC_H

    #include <QMainWindow>
    #include <QMessageBox>

    namespace Ui {
    class ActividadesTIC;

    }

    class MyWidget : public QWidget
    {
    Q_OBJECT

    public:
    MyWidget(QWidget *parent = 0);

    public slots:
    void easy();

    private:
    int manera;
    };

    class ActividadesTIC : public QMainWindow
    {
    // Q_OBJECT

    public:

    explicit ActividadesTIC(QWidget *parent = 0);
    ~ActividadesTIC();
    

    private:
    Ui::ActividadesTIC *ui;
    };

    #endif // ACTIVIDADESTIC_H

    @



  • Please do yourself a favor and include the Q_OBJECT macro in all QObject derived classes (QWidgets and subclasses count in!). You will save you a big bunch of problems, even if you do not need the functionality in the first place. So, please remove the commenting // in the second class.



  • hahah, ok Thanks, And I'll do that from now and on



  • When you posted your solution, you pasted the header file in the cpp section. I am having the same problem and am unable to resolve. I would appreciate seeing your working cpp code, if you still have it on hand.



  • Please open a new thread and post your problem description with some code snippets there.



  • Error :----QObject::connect: No such slot QDialog::mydata()

    My HeaderFile
    #ifndef DIALOG_H
    #define DIALOG_H
    #include <QDialog>
    #include<QLineEdit>
    #include<QPushButton>

    class Dialog : public QDialog
    {

    public:
    Dialog(QLineEdit *);
    QPushButton *apply;
    QLineEdit *line2;
    QLineEdit *oldLineEdit;
    private slots:
    void mydata();

    };

    #endif // DIALOG_H

    cpp file

    #include "Dialog.h"
    #include <QVBoxLayout>
    #include<stdio.h>
    Dialog::Dialog(QLineEdit *ledit)
    {
    apply = new QPushButton("Apply");
    line2 = new QLineEdit;
    oldLineEdit=ledit;
    QVBoxLayout *b = new QVBoxLayout;
    b->addWidget(line2);
    b->addWidget(apply);
    setLayout(b);
    QObject::connect(apply,SIGNAL(clicked()),this,SLOT(mydata()));
    }

    void Dialog::mydata(){
    printf("Enter the slot");
    QString str = line2->text();
    oldLineEdit->setText(str);
    }



  • hii Sushant......
    It is inadequate --
    QObject::connect(apply,SIGNAL),this,SLOT)); ?



  • hii sumit,
    QObject::connect(apply,SIGNAL(clicked()),this,SLOT(mydata()));



  • Hi sushant,

    1. You forget add Q_OBJECT in your custom class.
    2. Please wrap your code between <code>@ and @<code>

    BTW,
    You can start a new thread for your question next time.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.