我要用qt做一个选课系统,请先教我做登录界面,该登陆界面有注册和登录功能,且分学生端和管理员端,选择不同的端会进入不同的选课页面
时间: 2024-02-25 20:54:21 浏览: 19
好的,以下是一个简单的登录界面示例,包括注册和登录功能,分学生端和管理员端。
首先,你需要在Qt Creator中创建一个新的Qt Widgets应用程序项目。然后在mainwindow.cpp文件中编写以下代码:
```
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 设置默认为学生端登录
ui->comboBox->setCurrentIndex(0);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
// 获取用户名和密码
QString username = ui->lineEdit_username->text();
QString password = ui->lineEdit_password->text();
// 判断用户名和密码是否为空
if (username.isEmpty() || password.isEmpty()) {
QMessageBox::warning(this, "Warning", "Username or password cannot be empty!");
return;
}
// 判断选择的是学生端还是管理员端
if (ui->comboBox->currentIndex() == 0) { // 学生端
// TODO: 学生端登录处理
// 进入学生端选课页面
QMessageBox::information(this, "Information", "Enter student course selection page!");
} else { // 管理员端
// 判断管理员用户名和密码是否正确
if (username == "admin" && password == "admin") {
// TODO: 管理员端登录处理
// 进入管理员端选课页面
QMessageBox::information(this, "Information", "Enter administrator course selection page!");
} else {
QMessageBox::warning(this, "Warning", "Username or password incorrect!");
}
}
}
void MainWindow::on_pushButton_register_clicked()
{
// 获取用户名和密码
QString username = ui->lineEdit_username->text();
QString password = ui->lineEdit_password->text();
// 判断用户名和密码是否为空
if (username.isEmpty() || password.isEmpty()) {
QMessageBox::warning(this, "Warning", "Username or password cannot be empty!");
return;
}
// 判断选择的是学生端还是管理员端
if (ui->comboBox->currentIndex() == 0) { // 学生端
// TODO: 学生端注册处理
QMessageBox::information(this, "Information", "Student registered successfully!");
} else { // 管理员端
// TODO: 管理员端注册处理
QMessageBox::information(this, "Information", "Administrator registered successfully!");
}
}
```
然后在mainwindow.h文件中声明以下公共槽函数:
```
private slots:
void on_pushButton_clicked();
void on_pushButton_register_clicked();
```
接着,在mainwindow.ui文件中设计登录界面。可以使用QLineEdit控件获取用户名和密码,使用QComboBox控件选择学生端或管理员端。将登录和注册按钮分别连接到on_pushButton_clicked()和on_pushButton_register_clicked()槽函数。
最后,在main.cpp文件中启动应用程序:
```
#include "mainwindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
```
这样就完成了一个简单的登录界面,根据选择的端口进入不同的选课页面。