用QT 写一个界面包含登录注册
时间: 2024-05-30 16:15:51 浏览: 92
QT写的登录界面
以下是一个简单的用QT写的登录注册界面:
mainwindow.h
```
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_loginButton_clicked();
void on_registerButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
```
mainwindow.cpp
```
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_loginButton_clicked()
{
QString username = ui->usernameLineEdit->text();
QString password = ui->passwordLineEdit->text();
// 进行登录验证,这里只是简单地判断用户名和密码是否为空
if (username.isEmpty() || password.isEmpty()) {
QMessageBox::warning(this, "Error", "Username or password can't be empty!");
} else {
QMessageBox::information(this, "Success", "Login succeeded!");
}
}
void MainWindow::on_registerButton_clicked()
{
QString username = ui->usernameLineEdit->text();
QString password = ui->passwordLineEdit->text();
// 进行注册,这里只是简单地判断用户名和密码是否为空
if (username.isEmpty() || password.isEmpty()) {
QMessageBox::warning(this, "Error", "Username or password can't be empty!");
} else {
QMessageBox::information(this, "Success", "Register succeeded!");
}
}
```
mainwindow.ui
使用QT Designer生成的UI文件,包含一个窗口、两个标签、两个文本框、两个按钮和一个水平布局。
```
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Login/Register</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
<string>Login/Register</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QLabel" name="usernameLabel">
<property name="text">
<string>Username:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="usernameLineEdit"/>
</item>
<item>
<widget class="QLabel" name="passwordLabel">
<property name="text">
<string>Password:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="passwordLineEdit" form="EchoModePassword"/>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="loginButton">
<property name="text">
<string>Login</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="registerButton">
<property name="text">
<string>Register</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
```
效果如下图所示:
![login_register.png](https://i.loli.net/2021/08/02/7z8gWpSjKd1YHJZ.png)
阅读全文