简体   繁体   中英

connect QPushButton to a function inside a the same class

I want to connect QPushButton to a function inside the same class. the class is just a QGridLayout that making ui via code. I don't know what to do. actually I don't know what to put in for reciever argument in QObject::connect .

I don't want to use bunch of h and cpp files so that the code be as simple and readable as possible.

here is my main.cpp file:

#include <QApplication>
#include <QtWidgets>
#include <iostream>
using namespace  std;

class ConnectPage
{
public:
    void login_clicked(){
        cout<<"login pressed"<<endl;
    }
    QGridLayout *main_layout = new QGridLayout;
    // user interface Labels
    QLabel *username_label = new QLabel("username");
    QLabel *password_label = new QLabel("password");
    QLabel *host_label = new QLabel("host");
    QLabel *port_label = new QLabel("port");
    QLabel *status_bar = new QLabel;
    // user interface lineedits
    QLineEdit *username_lineedit = new QLineEdit;
    QLineEdit *password_lineedit = new QLineEdit;
    QLineEdit *host_lineedit = new QLineEdit;
    QLineEdit *port_lineedit = new QLineEdit;
    // user interface buttons
    QPushButton *login = new QPushButton("Connect");
    QPushButton *reset = new QPushButton("Clear form");
    ConnectPage()
    {
        QObject::connect(login, SIGNAL(clicked()), &app, SLOT(login_clicked()));
        main_layout->addWidget(username_label,0,0,1,1);
        main_layout->addWidget(username_lineedit,0,1,1,1);
        main_layout->addWidget(password_label,1,0,1,1);
        main_layout->addWidget(password_lineedit,1,1,1,1);
        main_layout->addWidget(host_label,2,0,1,1);
        main_layout->addWidget(host_lineedit,2,1,1,1);
        main_layout->addWidget(port_label,3,0,1,1);
        main_layout->addWidget(port_lineedit,3,1,1,1);
        main_layout->addWidget(reset,4,0,1,1);
        main_layout->addWidget(login,4,1,1,1);
        main_layout->addWidget(status_bar,5,0,1,2);
    }
};

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
    QWidget *window = new QWidget;
    ConnectPage start_connecting;
    QGridLayout *main_layout = start_connecting.main_layout;
    window->setLayout( main_layout );
    window->setWindowTitle("Login Page");
    window->show();
    return app.exec();
}

and here is .pro file:

QT += core gui widgets

SOURCES += \
    main.cpp \

HEADERS += \

any help that regards only these two files would be appreciated.

If you are using Qt <= 15.2, i think you should start at reading Qt resources with signals and slots. link

Simple answer is that:

  1. your class should inherit from Qobject,and you should put QObject makro in top and private section of this class.
class example : public QObject
{
    Q_OBJECT

public: ...
public slots: ...
private: ...

};
  1. Function that is slot, should be in public slots: section of a class. Like:
public slots:
void login_clicked(){
        cout<<"login pressed"<<endl;
    }
  1. You could capture this function in class constructor like this:
connect(login, &QPushButton::pressed, this, &ConnectPage::login_clicked);

And you may use a lambda expression in connect .

connect(login, &QPushButton::pressed, [&](){
    cout<<"login pressed"<<endl;
});

And for last word. If you want to use syntax that you have now, you need to remember about function signatures. From documentation: "The rule about whether to include arguments or not in the SIGNAL() and SLOT() macros, if the arguments have default values, is that the signature passed to the SIGNAL() macro must not have fewer arguments than the signature passed to the SLOT() macro."

So this syntax should work:

connect(login, SIGNAL( clicked(bool)), this, SLOT(login_clicked()));

But I don't like this syntax.

Best regards!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM