手把手教你實現(xiàn)漂亮的Qt?登錄界面
前言
最近在使用Qt5,Qt Creator做一個管理系統(tǒng)類的項目,自然需要用到登錄界面,故記錄一下登錄界面的制作。其中一些功能的實現(xiàn)也得益于之前Qt5基礎(chǔ)視頻不規(guī)則窗口部分的學(xué)習(xí)。
手把手教你實現(xiàn)漂亮的登錄界面
首先看一下成品。

第一步、新建一個Qwidget項目
沒必要用qmainwindow,不需要那些菜單,一般用qwidget就可以,注意勾選ui。

第二步、添加界面組件
1、添加容器

調(diào)整容器為合適大小,同時調(diào)整整個畫布為合適大小。

2、添加按鈕,標(biāo)簽,文字組件
構(gòu)思:
賬號密碼部分使用Input Widgets:Line Edit
Logo和忘記密碼使用兩個Display Widgets:TextLabel
是否記住我選擇一個Buttons:CheckBox
登錄按鈕使用Buttons:PushButton

3、修改組件名稱
首先修改Line Edit默認(rèn)文本屬性,分別點(diǎn)擊兩個LineEdit修改文本屬性為Username和Password。

然后其他的按鈕文本標(biāo)簽直接雙擊修改名稱即可。

以上兩步之后,可以得到這個樣子(這里統(tǒng)一在Wighets右邊的菜單里修改過字體,一會可以通過樣式表統(tǒng)一去改)。

4、添加樣式表
類似于css,Qt具有Qss,最為關(guān)鍵的一步就是添加樣式表。在Frame容器外 畫布內(nèi) 右鍵改變樣式表,輸入以下代碼。
*{
background:rgb(255, 255, 255);
font-size:15px;
font-style:MingLiU-ExtB;
}
QFrame{
border:sold 10px rgba(0,0,0);
background-image:url(H:/GUI_design/day04/image/Login_Blue5);//背景
}
QLineEdit{
color:#8d98a1;
background-color:#405361;
font-size:16px;
border-style:outset;
border-radius:10px;
font-style:MingLiU-ExtB;
}
QPushButton{
background:#ced1d8;
border-style:outset;
border-radius:10px;
font-style:MingLiU-ExtB;
}
QPushButton:pressed{
background-color:rgb(224,0,0);
border-style:inset;
font-style:MingLiU-ExtB;
}
QCheckBox{
background:rgba(85,170,255,0);
color:white;
font-style:MingLiU-ExtB;
}
QLabel{
background:rgba(85,170,255,0);
color:white;
font-style:MingLiU-ExtB;
font-size:14px;
}
背景部分找“度娘”就可以。

應(yīng)用樣式表后展示。

第三步、實現(xiàn)最小化窗口和關(guān)閉窗口功能
1、添加最小化和關(guān)閉窗口按鈕
按鈕選擇Buttons:Tool Button,最小化采用-,關(guān)閉窗口采用x。

2、按鈕轉(zhuǎn)到槽
將兩個按鈕都轉(zhuǎn)到槽。

3、代碼示例
轉(zhuǎn)到槽之后,只需要在相應(yīng)的函數(shù)里添加close()和showMinimized()功能即可。具體的代碼如下。
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private slots:
void on_toolButton_clicked();
void on_toolButton_2_clicked();
private:
Ui::Widget *ui;
};
#endif // WIDGET_H
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.cpp
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
//核心代碼就這幾行
void Widget::on_toolButton_clicked()
{
close();
}
void Widget::on_toolButton_2_clicked()
{
showMinimized();
}
調(diào)整畫布到合適大小

展示如下:

這個時候我們還需要把Widget自帶的上邊框去掉,并且去掉背景。
第四步、隱藏widget窗口邊框和背景
widget.cpp文件中添加如下兩句代碼即可。
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//去窗口邊框
setWindowFlags(Qt::FramelessWindowHint | windowFlags());
//把窗口背景設(shè)置為透明;
setAttribute(Qt::WA_TranslucentBackground);
}
第五步、實現(xiàn)界面可移動
需要添加一個鼠標(biāo)移動和鼠標(biāo)按下事件。以*e來獲取鼠標(biāo)移動或按下。
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
protected:
void mouseMoveEvent(QMouseEvent *e);//鼠標(biāo)移動
void mousePressEvent(QMouseEvent *e);//鼠標(biāo)按下移動
private slots:
void on_close_clicked();
void on_minimized_clicked();
private:
Ui::Widget *ui;
QPoint p;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QPainter>
#include <QMouseEvent>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
//去窗口邊框
setWindowFlags(Qt::FramelessWindowHint | windowFlags());
//把窗口背景設(shè)置為透明;
setAttribute(Qt::WA_TranslucentBackground);
}
Widget::~Widget()
{
delete ui;
}
void Widget::mousePressEvent(QMouseEvent *e)
{
if(e->button() == Qt::LeftButton)
{
//求坐標(biāo)差值
//當(dāng)前點(diǎn)擊坐標(biāo)-窗口左上角坐標(biāo)
p = e->globalPos() - this->frameGeometry().topLeft();
}
}
void Widget::mouseMoveEvent(QMouseEvent *e)
{
if(e->buttons() & Qt::LeftButton)
{
//移到左上角
move(e->globalPos() - p);
}
}
void Widget::on_close_clicked()
{
close();
}
void Widget::on_minimized_clicked()
{
showMinimized();
}
參考:
https://www.bilibili.com/video/BV1gb411W7WE?p=2
到此這篇關(guān)于手把手教你實現(xiàn)漂亮的Qt 登錄界面的文章就介紹到這了,更多相關(guān)Qt 登錄界面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
c++實現(xiàn)跳躍表(Skip List)的方法示例
跳表(skiplist)是一個非常優(yōu)秀的數(shù)據(jù)結(jié)構(gòu),實現(xiàn)簡單,插入、刪除、查找的復(fù)雜度均為O(logN),下面這篇文章主要介紹了c++實現(xiàn)跳躍表(Skip List)的相關(guān)資料,需要的朋友可以參考借鑒,下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-09-09
Visual Studio 2019配置qt開發(fā)環(huán)境的搭建過程
這篇文章主要介紹了Visual Studio 2019配置qt開發(fā)環(huán)境的搭建過程,本文圖文并茂給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03

