SLOT DOUBT



  • Hello,
    I am new in QT.
    Help me to solve my doubt.
    I don't get it why slot function is not called.

    Main.CPP file
    -------------------
    
    #include <QApplication>
    #include "pushbutton.h"
    
    int main(int argc, char *argv[])
    {
        QApplication app(argc, argv);
        pushbutton1 push;
        push.show();
    //    push.window.show();
        return app.exec();
    }
    
    pushbutton.h file
    -------------------------
    #ifndef PUSHBUTTON_H
    #define PUSHBUTTON_H
    
    #include <QWidget>
    #include <QLabel>
    #include <QPushButton>
    #include <QLineEdit>
    #include <QMessageBox>
    #include <QMainWindow>
    
    class pushbutton1 : public QWidget
    {
    
    public:
        explicit pushbutton1();
        ~pushbutton1();
    //    QWidget window;
        QMessageBox box;
    
    private slots:
        void popup();
    
    private:
    
    QLabel *Label;
    QPushButton *ok;
    QPushButton *cancel;
    QLineEdit line;
    
    };
    
    #endif // PUSHBUTTON_H
    
    pubutton.cpp file
    -------------------------
    #include <iostream>
    #include <QDebug>
    #include "pushbutton.h"
    #include "main.cpp"
    
    void pushbutton1 :: popup()
    {
        qDebug() << "Hello How are you?";
    //    cout << "Hello" << endl;
    //    QMessageBox::information(this, "Line MSG", line.text());
        box.setText(line.text());
        box.show();
    }
    
    pushbutton1::pushbutton1()
    {
        //class Rushin r1;
    
        Label = new QLabel("Enter Text:",this);
        Label->resize(90,30);
        Label->setStyleSheet("background-color:white;border-style: outset;\
                            border-width: 1px; border-color: black;");
    
    //    line = new QLine("0",&window);
        //line("",&window);
        line.setParent(this);
        line.resize(100,30);
        line.move(95,0);
        line.setStyleSheet("border-style: outset;\
                            border-width: 1px; border-color: black;");
    
        ok = new QPushButton("OK",this);
        ok->move(50,35);
        ok->resize(50,30);
    
        cancel = new QPushButton("Cancel",this);
        cancel->move(110,35);
        cancel->resize(50,30);
    
    //    box.setParent(&window);
        box.resize(100,100);
        box.setWindowTitle("Display");
        box.setText(line.text());
    
        connect(cancel, SIGNAL(clicked()), this, SLOT(close()));
        connect(&line, SIGNAL(returnPressed()), SLOT(popup()));
        connect(ok, SIGNAL(clicked()), SLOT(popup()));
    
    //    window.show();
    
    }
    
    pushbutton1::~pushbutton1()
    {
    
    }```
    //your code here
    

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You're missing the Q_OBJECT macro in your class declaration.

    On a side note, it's Qt, QT stands for Apple QuickTime which you might also be using ;)


  • Qt Champions 2016

    hi
    You need Q_OBJECT in your class for signals to work
    like
    class pushbutton1 : public QWidget
    {
    Q_OBJECT

    After adding this, please run qmake. Normal compile is not enough.

    Also as trick.
    use
    qDebug() to see if connect is true. so u know if it is ok

    qDebug() << "result: " << connect(ok, SIGNAL(clicked()), SLOT(popup()));

    (#include <QDebug>)



  • This post is deleted!


  • @mrjj :
    Hello,
    As per your explanation, I have changed in my code but now It shows compilation error
    like below.
    undefined reference to 'vtable for pushbutton1' and Id returned 1 exit status.
    Please solve it.


  • Lifetime Qt Champion

    Do as @mrjj suggested: re-run qmake before building or do a full rebuild.


Log in to reply
 

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