1.头文件
#ifndef LOGINWINDOW_H
#define LOGINWINDOW_H#include <QWidget>
#include <QPushButton>
#include <QLineEdit>
#include <QVBoxLayout>class LoginWindow : public QWidget {Q_OBJECTpublic:// 构造函数LoginWindow(QWidget *parent = nullptr);private:// 用户名输入框QLineEdit *usernameEdit;// 密码输入框QLineEdit *passwordEdit;// 登录按钮QPushButton *loginButton;
};#endif
2.源文件
#include "loginwindow.h"// 构造函数实现
LoginWindow::LoginWindow(QWidget *parent) : QWidget(parent) {// 创建布局和控件QVBoxLayout *layout = new QVBoxLayout(this);usernameEdit = new QLineEdit(this);usernameEdit->setPlaceholderText("account"); // 设置用户名占位符passwordEdit = new QLineEdit(this);passwordEdit->setEchoMode(QLineEdit::Password); // 设置为密码模式passwordEdit->setPlaceholderText("password"); // 设置密码占位符loginButton = new QPushButton("login", this);// 添加控件到布局layout->addWidget(usernameEdit);layout->addWidget(passwordEdit);layout->addWidget(loginButton);
}
3.main函数
#include "loginwindow.h"
#include <QApplication>int main(int argc, char *argv[]) {QApplication app(argc, argv);LoginWindow window; // 创建窗口实例window.resize(300, 150);window.setWindowTitle("login");window.show();return app.exec();
}