【发布时间】:2021-04-12 22:50:45
【问题描述】:
我想将QPushButton 连接到同一个类中的一个函数。该类只是通过代码制作ui 的QGridLayout。我不知道该怎么办。实际上我不知道在QObject::connect 中为reciever 参数输入什么。
我不想使用一堆 h 和 cpp 文件,以便代码尽可能简单易读。
这是我的main.cpp 文件:
#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();
}
这里是.pro 文件:
QT += core gui widgets
SOURCES += \
main.cpp \
HEADERS += \
任何仅涉及这两个文件的帮助将不胜感激。
【问题讨论】:
-
您已经将按钮连接到函数(尽管使用旧语法)。您应该编辑问题以更好地解决问题。
标签: c++ qt5 qpushbutton qobject