上面的代码实现部分放入QMainWindow.cpp中
时间: 2024-02-15 12:07:28 浏览: 24
好的,如果要把实现部分放入 QMainWindow.cpp 中,可以按照以下步骤进行修改:
1. 在 QMainWindow.h 文件中添加需要使用的 QT 控件的头文件和成员变量:
```cpp
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRegExpValidator>
class QMainWindow : public QWidget
{
Q_OBJECT
public:
QMainWindow(QWidget *parent = nullptr);
~QMainWindow();
private:
QLabel *accountLabel;
QLineEdit *accountLineEdit;
QLabel *passwordLabel;
QLineEdit *passwordLineEdit;
QPushButton *loginButton;
QRegExp regExp;
QRegExpValidator *validator;
};
```
2. 在 QMainWindow.cpp 文件中实现构造函数和析构函数:
```cpp
QMainWindow::QMainWindow(QWidget *parent)
: QWidget(parent)
{
setWindowTitle("QQ 登录");
accountLabel = new QLabel("账号:", this);
accountLabel->move(70, 80);
accountLineEdit = new QLineEdit(this);
accountLineEdit->move(120, 80);
passwordLabel = new QLabel("密码:", this);
passwordLabel->move(70, 120);
passwordLineEdit = new QLineEdit(this);
passwordLineEdit->setEchoMode(QLineEdit::Password);
passwordLineEdit->move(120, 120);
// 使用正则表达式校验密码
regExp = QRegExp("[a-zA-Z0-9]{6,18}");
validator = new QRegExpValidator(regExp, passwordLineEdit);
passwordLineEdit->setValidator(validator);
loginButton = new QPushButton("登录", this);
loginButton->move(150, 160);
resize(400, 300);
show();
}
QMainWindow::~QMainWindow()
{
delete accountLabel;
delete accountLineEdit;
delete passwordLabel;
delete passwordLineEdit;
delete loginButton;
delete validator;
}
```
3. 在 main.cpp 文件中创建 QMainWindow 对象并启动应用程序:
```cpp
#include "QMainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
return a.exec();
}
```
这样就可以把实现部分放入 QMainWindow.cpp 中了。