C++ QT开发 学习笔记(1)
考试系统
创建项目
新建Qt桌面应用程序,项目名:ExamSys。
类信息:类名LoginDialog继承自QDialog
(1) ExamSys.pro
工程文件,包含当前工程的相关信息。
QDialog 是 Qt 框架中用于创建对话框的基类。对话框是一种特殊类型的窗口,通常用于短期的交互和信息交换,比如用户输入、设置选项、文件选择等。QDialog
提供了许多专为这种用途设计的特性和功能。
ExamSys.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = ExamSys
TEMPLATE = app
logindialog.h
#ifndef LOGINDIALOG_H
#define LOGINDIALOG_H
#include <QDialog>
namespace Ui {
class LoginDialog; //Ui_LoginDialog son class, use for describe the login window detail message.
}
class LoginDialog : public QDialog
{
Q_OBJECT //Support signal and slots
public:
explicit LoginDialog(QWidget *parent = 0);
~LoginDialog();
private:
Ui::LoginDialog *ui;
};
#endif // LOGINDIALOG_H
logindialog.cpp
#include "logindialog.h"
#include "ui_logindialog.h"
LoginDialog::LoginDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::LoginDialog)
{
ui->setupUi(this);
}
LoginDialog::~LoginDialog()
{
delete ui;
}
main.cpp
#include "logindialog.h"
#include <QApplication>
#include <examdialog.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //Define the apps object
LoginDialog w; //Define the window object
w.show(); //Display window
return a.exec(); //Enter APP execeute loop
}
logindialog.ui
登录窗口界面文件。
登录界面
设计模式下添加账号、密码标签,账号、密码输入框,登录、取消按钮,并修改对象名,添加资源文件imgage.qrc,给imgLabel添加图片资源做背景。
logindialog.cpp构造函数中设置窗体标题、风格
LoginDialog::LoginDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::LoginDialog)
{
ui->setupUi(this);
this->resize(600,400);
setFixedSize(width(),height());
this->setWindowTitle("Cat Exam");
this->setWindowFlags(Qt::Dialog| Qt::WindowCloseButtonHint);
this->setWindowFlags(Qt::Window| Qt::WindowFullscreenButtonHint);
}
验证邮箱地址
给登录按钮添加响应点击信号的槽方法:
设计模式下,右键单击登录按钮,选择“转到槽”,选择点击信号clicked().
给槽方法void LoginDialog::on_loginBtn_clicked()添加如下代码:
void LoginDialog::on_login_Button_clicked()
{
//QMessageBox::information(this,"Hint","Slot method used");
//Verify the email address username@address ex:123980@qq.com
//Symbol decalration: ^string start &string end
//+match times>=1 *match any times(include 0 times) {n,m}match times at least n times, at most m times.
QRegExp rx("^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z0-9]{2,6}$");
bool res = rx.exactMatch(ui->account_edit->text());
if(!res)// If failed match
{
QMessageBox::information(this,"Hint","Invalid email address, please reenter the email address");
}
else
{
QMessageBox::information(this,"Hint","Welcome to the CAT exam!");
}
注意:需要加上头文件 include <QMessageBox>
验证账号密码 (通过账号密码保存在文档的方式)
在logindialog.cpp添加头文件
#include <QFile>
#include <QTextStream>
这两个头文件的引入有特定的目的,主要涉及到文件处理和文本数据的流式读写
编辑 void LoginDialog::on_loginBtn_clicked()
方法
void LoginDialog::on_login_Button_clicked()
{
//QMessageBox::information(this,"Hint","Slot method used");
//Verify the email address username@address ex:123980@qq.com
//Symbol decalration: ^string start &string end
//+match times>=1 *match any times(include 0 times) {n,m}match times at least n times, at most m times.
QRegExp rx("^[A-Za-z0-9]+([_\.][A-Za-z0-9]+)*@([A-Za-z0-9\-]+\.)+[A-Za-z0-9]{2,6}$");
bool res = rx.exactMatch(ui->account_edit->text());
if(!res)// If failed match
{
QMessageBox::information(this,"Hint","Invalid email address, please reenter the email address");
ui->account_edit->clear();
ui->code_edit->clear();
ui->account_edit->setFocus();
return;
}
else
{
//QMessageBox::information(this,"Hint","Welcome to the cat EXAM!");
QString filename; //Account & password data file
QString strAccInput; //User input account
QString strCode; //User input password
QString strLine; //Every line read data
QStringList strList; //Use for seperate the line from strLine for account and password
filename = "../account.txt"; //According to the debug folder. so need to go previous folder get account.txt
strAccInput = ui->account_edit->text();
strCode = ui->code_edit->text();
QFile file(filename);
QTextStream stream(&file);
if (file.open(QIODevice::ReadOnly | QIODevice::Text)