[C++ QT项目实战]----C++ QT系统登陆界面设计

前言

        在C++ QT项目开发过程中,设计系统登录界面可以使用QT框架来实现。以下是一个简单的系统登录界面设计示例:

  1. 创建登录界面UI:可以使用QT Designer来设计登录界面的UI,包括用户名输入框、密码输入框、登录按钮等。在QT Designer中,可以直观地拖放控件来设计界面布局。

  2. 实现登录功能:在C++中,可以通过连接信号与槽的方式实现登录功能。当用户点击登录按钮时,可以连接按钮的clicked信号与相应的槽函数,用于验证用户名密码是否正确。

  3. 用户验证:在槽函数中,可以获取用户名密码输入框中的文本内容,并与预先设置的用户名密码进行比对。如果用户名密码正确,可以跳转到系统主界面;如果不正确,可以提示用户重新输入。

  4. 界面美化:可以使用QT的样式表来美化界面,包括设置背景颜色、字体样式、按钮样式等。

正文

01-功能演示

        首先在QT中设计界面,如下图所示:字体,样式,颜色,和名称均可以按照意愿进行设计

         运行之后,登录界面如下图所示,可以进行用户名和密码输入登录主界面操作

        登录之后,主界面如下图所示,这是自己设计的界面,可以根据需要设计 

02-功能实现

        分三个文件进行分析,包括dialog_login.h,dialog_login.cpp,main主函数

        下方是dialog_login.cpp文件:其中path_deploy_cmBox为选择生产现场还是程序调试的控件,需要与槽函数path_deploy_cmBox_switch()连接,槽函数实现在下方代码中。函数on_login_ok_Btn_clicked()为上方设计中确定按钮对应的函数,当用户名和密码输入正确,点击确定之后,登陆界面被隐藏,主界面显示。

#include "dialog_login.h"
#include "ui_dialog_login.h"
#include <QComboBox>
#include <QMessageBox>
#include <QDebug>
#include "cglobal.h"
#include "mainwindow.h"
#include"ui_mainwindow.h"
Dialog_login::Dialog_login(QWidget *parent) :
	QDialog(parent),
	ui(new Ui::Dialog_login)
{
	ui->setupUi(this);
	ui->pass_QLE->setEchoMode(QLineEdit::Password);
	setWindowTitle(QString::fromLocal8Bit("主系统"));
	connect(ui->path_deploy_cmBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(path_deploy_cmBox_switch()));

}

Dialog_login::~Dialog_login()
{
	delete ui;
}

void Dialog_login::on_login_ok_Btn_clicked()
{
	QString str_user,str_pass;
	str_user = ui->user_QLE->text();
	str_pass = ui->pass_QLE->text();
	if (str_user=="beijing" &&str_pass=="beijing")
	{
		this->hide();
		MainWindow *pMainWindow = new MainWindow();
		pMainWindow->show();
		
	}
	else
	{
		ui->user_QLE->setText("");
		ui->pass_QLE->setText("");
		QMessageBox box(QMessageBox::Warning, "login", "login account  incorrect!");
		box.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
		box.setButtonText(QMessageBox::Ok, QString("yes"));
		box.setButtonText(QMessageBox::Cancel, QString("cancel"));
		box.exec();
		this->show();

		}	

}

void Dialog_login::path_deploy_cmBox_switch()
{
	if (ui->path_deploy_cmBox->currentText() == QString::fromLocal8Bit("生产现场"))
	{
		path_deploy = "beijing";

	}

	else if (ui->path_deploy_cmBox->currentText() == QString::fromLocal8Bit("程序调试"))
	{
		path_deploy = "local";
	}
}


        下方是dialog_login​​​​​​​.h文件:这里主要做了槽函数path_deploy_cmBox_switch()和函数on_login_ok_Btn_clicked()的声明。

#ifndef DIALOG_LOGIN_H
#define DIALOG_LOGIN_H
#include "ui_dialog_login.h"

#include <QDialog>
#include <QAbstractButton>
#include <QPushButton>
#include "mainwindow.h"
#include"ui_mainwindow.h"

namespace Ui {
	class Dialog_login;

}

class Dialog_login : public QDialog
{
	Q_OBJECT

public:
	explicit Dialog_login(QWidget *parent = 0);
	~Dialog_login();

private slots:
	void on_login_ok_Btn_clicked();
	void path_deploy_cmBox_switch();
	signals:
	/**
	* 信号必须要signals关键字来声明
	* 信号没有返回值, 但可以有参数
	* 信号就是函数的声明, 无需定义
	* 使用: emit mySignal();
	* 信号可以被重载
	*/
	//void showmain();

private:
	Ui::Dialog_login *ui;


};


#endif // DIALOG_LOGIN_H

        下方是main函数文件:这里用于给登陆界面命名,实现登陆界面显示功能等。

#include "mainwindow.h"
#include"ui_mainwindow.h"
#include <QApplication>
#include <QDesktopWidget>
#include <QDesktopWidget>
#include "ui_dialog_login.h"
#include "dialog_login.h"
#include<string>
#include "spdlog/fmt/ostr.h"
#include"dialog_con.h"

#include"ui_Dialog_con.h"

#pragma comment(lib , "DbgHelp.lib")

#include <Windows.h>
#include <DbgHelp.h>
#include <QString>
#include <QMessageBox>
#include <QDateTime>

//程式异常捕获
LONG CreateCrashHandler(EXCEPTION_POINTERS *pException) {
	//创建 Dump 文件

	QDateTime CurDTime = QDateTime::currentDateTime();
	QString current_date = CurDTime.toString("yyyy_MM_dd_hh_mm_ss");
	//dmp文件的命名
	QString dumpText = "Dump_" + current_date + ".dmp";
	HANDLE hDumpFile = CreateFile((LPCWSTR)dumpText.utf16(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hDumpFile != INVALID_HANDLE_VALUE) {
		//Dump信息
		MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
		dumpInfo.ExceptionPointers = pException;
		dumpInfo.ThreadId = GetCurrentThreadId();
		dumpInfo.ClientPointers = TRUE;
		//写入Dump文件内容
		MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);
	}
	//这里弹出一个错误对话框并退出程序
	EXCEPTION_RECORD* record = pException->ExceptionRecord;
	QString errCode(QString::number(record->ExceptionCode, 16)), errAdr(QString::number((uint)record->ExceptionAddress, 16)), errMod;
	QMessageBox::critical(NULL, QString::fromLocal8Bit("程序退出错误"), QString::fromLocal8Bit("<FONT size=4><div><b>对于发生的程序退出错误,表示诚挚的歉意,请根据错误信息和跟踪dump文件分析退出原因</b><br/></div>") +
		QString::fromLocal8Bit("<div>错误代码:%1</div><div>错误地址:%2</div></FONT>").arg(errCode).arg(errAdr),
		QMessageBox::Ok);
	return EXCEPTION_EXECUTE_HANDLER;
}


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

	//获取系统编码
	QTextCodec *codec = QTextCodec::codecForLocale();
	QTextCodec::setCodecForLocale(QTextCodec::codecForLocale());
	//注冊异常捕获函数
	SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)CreateCrashHandler);
	
	Dialog_login f0;
	f0.setWindowTitle(QString::fromLocal8Bit("主系统"));
	f0.show();

    return a.exec();
}

总结

        经过上述过程,便完成了登陆界面设计。在C++中设计系统登录界面通过QT框架来实现,通过信号与槽机制实现用户交互功能,同时可以利用QT的丰富功能来实现界面美化和多语言支持。

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
C++写的一个简单的界面演示系统 void CMiniDrawDoc::AddFigure (CFigure *PFigure) { m_FigArray.Add (PFigure); SetModifiedFlag (); } CFigure *CMiniDrawDoc::GetFigure (int Index) { if (Index m_FigArray.GetUpperBound ()) return 0; return (CFigure *)m_FigArray.GetAt (Index); } int CMiniDrawDoc::GetNumFigs () { return m_FigArray.GetSize (); } void CMiniDrawDoc::DeleteContents() { // TODO: Add your specialized code here and/or call the base class int Index = m_FigArray.GetSize (); while (Index--) delete m_FigArray.GetAt (Index); m_FigArray.RemoveAll (); CDocument::DeleteContents(); } void CMiniDrawDoc::OnEditClearAll() { // TODO: Add your command handler code here DeleteContents (); UpdateAllViews (0); SetModifiedFlag (); } void CMiniDrawDoc::OnUpdateEditClearAll(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->Enable (m_FigArray.GetSize ()); } void CMiniDrawDoc::OnEditUndo() { // TODO: Add your command handler code here int Index = m_FigArray.GetUpperBound (); if (Index > -1) { delete m_FigArray.GetAt (Index); m_FigArray.RemoveAt (Index); } UpdateAllViews (0); SetModifiedFlag (); } void CMiniDrawDoc::OnUpdateEditUndo(CCmdUI* pCmdUI) { // TODO: Add your command update UI handler code here pCmdUI->Enable (m_FigArray.GetSize ()); } // implementation of figure classes: IMPLEMENT_SERIAL (CFigure, CObject, 3) CRect CFigure::GetDimRect () { return CRect (min (m_X1, m_X2), min (m_Y1, m_Y2), max (m_X1, m_X2) + 1, max (m_Y1, m_Y2) + 1); } void CFigure::Serialize (CArchive& ar) { if (ar.IsStoring ()) ar << m_X1 << m_Y1 << m_X2 << m_Y2 <> m_X1 >> m_Y1 >> m_X2 >> m_Y2 >> m_Color; } IMPLEMENT_SERIAL (CLine, CFigure, 3) CLine::CLine (int X1, int Y1, int X2, int Y2, COLORREF Color, int Thickness) { m_X1 = X1; m_Y1 = Y1; m_X2 = X2; m_Y2 = Y2; m_Color = Color; m_Thickness = Thickness; } void CLine::Serialize (CArchive& ar) { CFigure::Serialize (ar); if (ar.IsStoring ()) ar <> m_Thickness; } void CLine::Draw (CDC *PDC) { CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_SOLID, m_Thickness, m_Color); POldPen = PDC->SelectObject (&Pen); // draw figure: PDC->MoveTo (m_X1, m_Y1); PDC->LineTo (m_X2, m_Y2); // remove pen/brush: PDC->SelectObject (POldPen); } IMPLEMENT_SERIAL (CRectangle, CFigure, 3) CRectangle::CRectangle (int X1, int Y1, int X2, int Y2, COLORREF Color, int Thickness) { m_X1 = X1; m_Y1 = Y1; m_X2 = X2; m_Y2 = Y2; m_Color = Color; m_Thickness = Thickness; } void CRectangle::Serialize (CArchive& ar) { CFigure::Serialize (ar); if (ar.IsStoring ()) ar <> m_Thickness; } void CRectangle::Draw (CDC *PDC) { CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_INSIDEFRAME, m_Thickness, m_Color); POldPen = PDC->SelectObject (&Pen); PDC->SelectStockObject (NULL_BRUSH); // draw figure: PDC->Rectangle (m_X1, m_Y1, m_X2, m_Y2); // remove pen/brush: PDC->SelectObject (POldPen); } IMPLEMENT_SERIAL (CRectFill, CFigure, 3) CRectFill::CRectFill (int X1, int Y1, int X2, int Y2, COLORREF Color) { m_X1 = min (X1, X2); m_Y1 = min (Y1, Y2); m_X2 = max (X1, X2); m_Y2 = max (Y1, Y2); m_Color = Color; } void CRectFill::Draw (CDC *PDC) { CBrush Brush, *POldBrush; CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_INSIDEFRAME, 1, m_Color); POldPen = PDC->SelectObject (&Pen); Brush.CreateSolidBrush (m_Color); POldBrush = PDC->SelectObject (&Brush); // draw figure: PDC->Rectangle (m_X1, m_Y1, m_X2, m_Y2); // remove pen/brush: PDC->SelectObject (POldPen); PDC->SelectObject (POldBrush); } IMPLEMENT_SERIAL (CRectRound, CFigure, 3) CRectRound::CRectRound (int X1, int Y1, int X2, int Y2, COLORREF Color, int Thickness) { m_X1 = min (X1, X2); m_Y1 = min (Y1, Y2); m_X2 = max (X1, X2); m_Y2 = max (Y1, Y2); m_Color = Color; m_Thickness = Thickness; } void CRectRound::Serialize (CArchive& ar) { CFigure::Serialize (ar); if (ar.IsStoring ()) ar <> m_Thickness; } void CRectRound::Draw (CDC *PDC) { CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_INSIDEFRAME, m_Thickness, m_Color); POldPen = PDC->SelectObject (&Pen); PDC->SelectStockObject (NULL_BRUSH); // draw figure: int SizeRound = (m_X2 - m_X1 + m_Y2 - m_Y1) / 6; PDC->RoundRect (m_X1, m_Y1, m_X2, m_Y2, SizeRound, SizeRound); // remove pen/brush: PDC->SelectObject (POldPen); } IMPLEMENT_SERIAL (CRectRoundFill, CFigure, 3) CRectRoundFill::CRectRoundFill (int X1, int Y1, int X2, int Y2, COLORREF Color) { m_X1 = min (X1, X2); m_Y1 = min (Y1, Y2); m_X2 = max (X1, X2); m_Y2 = max (Y1, Y2); m_Color = Color; } void CRectRoundFill::Draw (CDC *PDC) { CBrush Brush, *POldBrush; CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_INSIDEFRAME, 1, m_Color); POldPen = PDC->SelectObject (&Pen); Brush.CreateSolidBrush (m_Color); POldBrush = PDC->SelectObject (&Brush); // draw figure: int SizeRound = (m_X2 - m_X1 + m_Y2 - m_Y1) / 6; PDC->RoundRect (m_X1, m_Y1, m_X2, m_Y2, SizeRound, SizeRound); // remove pen/brush: PDC->SelectObject (POldPen); PDC->SelectObject (POldBrush); } IMPLEMENT_SERIAL (CCircle, CFigure, 3) CCircle::CCircle (int X1, int Y1, int X2, int Y2, COLORREF Color, int Thickness) { m_X1 = min (X1, X2); m_Y1 = min (Y1, Y2); m_X2 = max (X1, X2); m_Y2 = max (Y1, Y2); m_Color = Color; m_Thickness = Thickness; } void CCircle::Serialize (CArchive& ar) { CFigure::Serialize (ar); if (ar.IsStoring ()) ar <> m_Thickness; } void CCircle::Draw (CDC *PDC) { CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_INSIDEFRAME, m_Thickness, m_Color); POldPen = PDC->SelectObject (&Pen); PDC->SelectStockObject (NULL_BRUSH); // draw figure: PDC->Ellipse (m_X1, m_Y1, m_X2, m_Y2); // remove pen/brush: PDC->SelectObject (POldPen); } IMPLEMENT_SERIAL (CCircleFill, CFigure, 3) CCircleFill::CCircleFill (int X1, int Y1, int X2, int Y2, COLORREF Color) { m_X1 = min (X1, X2); m_Y1 = min (Y1, Y2); m_X2 = max (X1, X2); m_Y2 = max (Y1, Y2); m_Color = Color; } void CCircleFill::Draw (CDC *PDC) { CBrush Brush, *POldBrush; CPen Pen, *POldPen; // select pen/brush: Pen.CreatePen (PS_INSIDEFRAME, 1, m_Color); POldPen = PDC->SelectObject (&Pen); Brush.CreateSolidBrush (m_Color); POldBrush = PDC->SelectObject (&Brush); // draw figure: PDC->Ellipse (m_X1, m_Y1, m_X2, m_Y2); // remove pen/brush: PDC->SelectObject (POldPen); PDC->SelectObject (POldBrush); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一伦明悦

感谢,您的支持是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值