重构标题栏框架构建函数

This commit is contained in:
JackLee_CN 2024-11-07 11:23:40 +08:00
parent 25b4c9146c
commit 8678fb6978
9 changed files with 95 additions and 133 deletions

View File

@ -4,7 +4,7 @@ QWidget#m_widget{
color:#F0F0F0F0; color:#F0F0F0F0;
} }
QPushButton#ico_button{ QPushButton#btn_opt{
image:url(:/res/img/xsteam.png); image:url(:/res/img/xsteam.png);
border:none; border:none;
} }
@ -82,7 +82,7 @@ QWidget#sui_title{
margin-bottom:1px; margin-bottom:1px;
margin-left:1px; margin-left:1px;
} }
QPushButton#min_button,QPushButton#max_button,QPushButton#close_button{ QPushButton#btn_min,QPushButton#btn_max,QPushButton#btn_close{
margin-right:3px; margin-right:3px;
margin-bottom:0px; margin-bottom:0px;
border-radius:8px; border-radius:8px;
@ -90,31 +90,31 @@ QPushButton#min_button,QPushButton#max_button,QPushButton#close_button{
font-weight: bold; font-weight: bold;
font: bold 12px; font: bold 12px;
} }
QPushButton#min_button{ QPushButton#btn_min{
image:url(":/res/img/btn/btn_min.png"); image:url(":/res/img/btn/btn_min.png");
border:none; border:none;
width:20px; width:20px;
height:20px; height:20px;
} }
QPushButton#max_button{ QPushButton#btn_max{
image:url(":/res/img/btn/btn_max.png"); image:url(":/res/img/btn/btn_max.png");
border:none; border:none;
width:20px; width:20px;
height:20px; height:20px;
} }
QPushButton#close_button{ QPushButton#btn_close{
image:url(":/res/img/btn/btn_close.png"); image:url(":/res/img/btn/btn_close.png");
border:none; border:none;
width:20px; width:20px;
height:20px; height:20px;
} }
QPushButton#min_button:hover,QPushButton#max_button:hover,QPushButton#close_button:hover{ QPushButton#btn_min:hover,QPushButton#btn_max:hover,QPushButton#btn_close:hover{
padding-left:1px; padding-left:1px;
padding-top:1px; padding-top:1px;
} }
QPushButton#min_button:pressed,QPushButton#max_button:pressed,QPushButton#close_button:pressed{ QPushButton#btn_min:pressed,QPushButton#btn_max:pressed,QPushButton#btn_close:pressed{
padding-left:1px; padding-left:1px;
padding-top:1px; padding-top:1px;
} }

View File

@ -4,7 +4,7 @@ QWidget#m_widget{
color:#2F4F4F; color:#2F4F4F;
} }
QPushButton#ico_button{ QPushButton#btn_opt{
image:url(:/res/img/xsteam.png); image:url(:/res/img/xsteam.png);
border:none; border:none;
} }
@ -58,37 +58,37 @@ QWidget#sui_title{
margin-bottom:1px; margin-bottom:1px;
margin-left:1px; margin-left:1px;
} }
QPushButton#min_button,QPushButton#max_button,QPushButton#close_button{ QPushButton#btn_min,QPushButton#btn_max,QPushButton#btn_close{
margin-right:3px; margin-right:3px;
margin-bottom:0px; margin-bottom:0px;
border-radius:8px; border-radius:8px;
} }
QPushButton#min_button{ QPushButton#btn_min{
image:url(":/res/img/btn/btn_min.png"); image:url(":/res/img/btn/btn_min.png");
border:none; border:none;
width:25px; width:25px;
height:25px; height:25px;
} }
QPushButton#max_button{ QPushButton#btn_max{
image:url(":/res/img/btn/btn_max.png"); image:url(":/res/img/btn/btn_max.png");
border:none; border:none;
width:25px; width:25px;
height:25px; height:25px;
} }
QPushButton#close_button{ QPushButton#btn_close{
image:url(":/res/img/btn/btn_close.png"); image:url(":/res/img/btn/btn_close.png");
border:none; border:none;
width:25px; width:25px;
height:25px; height:25px;
} }
QPushButton#min_button:hover,QPushButton#max_button:hover,QPushButton#close_button:hover{ QPushButton#btn_min:hover,QPushButton#btn_max:hover,QPushButton#btn_close:hover{
padding-left:1px; padding-left:1px;
padding-top:1px; padding-top:1px;
} }
QPushButton#min_button:pressed,QPushButton#max_button:pressed,QPushButton#close_button:pressed{ QPushButton#btn_min:pressed,QPushButton#btn_max:pressed,QPushButton#btn_close:pressed{
padding-left:1px; padding-left:1px;
padding-top:1px; padding-top:1px;
} }

View File

@ -1,22 +1,12 @@
#include "sui.h" #include "sui.h"
sui::sui(QWidget *parent) sui::sui(QWidget *parent)
: QWidget(parent), : QWidget(parent),
m_widget(new QDialog),
m_layout(new QVBoxLayout), m_layout(new QVBoxLayout),
m_widget(new QWidget),
m_press(false), m_press(false),
m_border_width(5) m_border_width(5)
{
init_layout();
}
sui::~sui()
{
}
void sui::init_layout()
{ {
m_title=new sui_title(this); m_title=new sui_title(this);
m_title->set_title(tr("XSteam"));
setObjectName(tr("m_widget")); setObjectName(tr("m_widget"));
m_layout->addWidget(m_title); m_layout->addWidget(m_title);
m_title->setMouseTracking(true); m_title->setMouseTracking(true);
@ -28,10 +18,11 @@ void sui::init_layout()
setBaseSize(800, 600); setBaseSize(800, 600);
resize(800, 600); resize(800, 600);
setLayout(m_layout); setLayout(m_layout);
connect(m_title, SIGNAL(signals_close()), this, SLOT(slots_close())); connect(m_title, SIGNAL(signals_close()), this, SLOT(close()));
} }
void sui::slots_close(){ sui::~sui()
this->close(); {
} }
void sui::showEvent(QShowEvent *event) void sui::showEvent(QShowEvent *event)
{ {

View File

@ -16,17 +16,13 @@ public:
explicit sui(QWidget *parent = 0); explicit sui(QWidget *parent = 0);
~sui(); ~sui();
sui_title* title() { return m_title; } sui_title* title() { return m_title; }
private:
void init_layout();
private: private:
QVBoxLayout *m_layout; QVBoxLayout *m_layout;
QWidget *m_widget; QWidget *m_widget;
QPoint m_point; QPoint m_point;
bool m_press; bool m_press;
int m_border_width; int m_border_width;
sui_title* m_title; sui_title *m_title;
public slots:
void slots_close();
protected: protected:
void paintEvent(QPaintEvent *event); void paintEvent(QPaintEvent *event);
void showEvent(QShowEvent *event); void showEvent(QShowEvent *event);

View File

@ -7,39 +7,17 @@ sui_title::sui_title(QWidget *parent)
m_type(QD_TYPE::QD_EXIT) m_type(QD_TYPE::QD_EXIT)
{ {
m_parent = parent; m_parent = parent;
// 给成员变量申请内存
m_icon_button = new QPushButton(/*this*/);
m_min_button = new QPushButton(this);
m_max_button = new QPushButton(this);
m_close_button = new QPushButton(this);
// 初始化图标Label
m_icon_button->setFixedSize(35, 35);
m_icon_button->setAutoFillBackground(true);
m_icon_button->setFlat(true);
// 设置按钮的固定大小、图片、取消边框
m_min_button->setIconSize(QSize(25, 25));
m_min_button->setAutoFillBackground(true);
m_min_button->setFlat(true);
//--
m_max_button->setIconSize(QSize(25, 25));
m_max_button->setAutoFillBackground(true);
m_max_button->setFlat(true);
//--
m_close_button->setIconSize(QSize(25, 25));
m_close_button->setAutoFillBackground(true);
m_close_button->setFlat(true);
// 设置窗口部件的名称
setObjectName(tr("sui_title")); setObjectName(tr("sui_title"));
m_icon_button->setObjectName(tr("ico_button")); for (int i = 0; i <= m_btn_obj_str.count() - 1; i++)
m_min_button->setObjectName(tr("min_button")); {
m_max_button->setObjectName(tr("max_button")); QPushButton *btn = new QPushButton(this);
m_close_button->setObjectName(tr("close_button")); btn->setFixedSize(35, 35);
// 给按钮设置静态tooltip当鼠标移上去时显示tooltip btn->setAutoFillBackground(true);
m_min_button->setToolTip(tr("最小化")); btn->setFlat(true);
m_max_button->setToolTip(tr("最大化")); btn->setObjectName(m_btn_obj_str[i]);
m_close_button->setToolTip(tr("关闭")); btn->setToolTip(m_btn_tips_str[i]);
m_btn_vectors.push_back(btn);
}
// 标题栏布局 // 标题栏布局
m_title_label = new QLabel(this); m_title_label = new QLabel(this);
m_title_label->setAlignment(Qt::AlignCenter); m_title_label->setAlignment(Qt::AlignCenter);
@ -49,13 +27,13 @@ sui_title::sui_title(QWidget *parent)
m_title_label->setFont(font); m_title_label->setFont(font);
m_layout = new QHBoxLayout(this); m_layout = new QHBoxLayout(this);
m_layout->setAlignment(Qt::AlignCenter); m_layout->setAlignment(Qt::AlignCenter);
m_layout->addWidget(m_icon_button); m_layout->addWidget(m_btn_vectors[0]);
m_layout->addItem(sparcer_item); m_layout->addItem(sparcer_item);
m_layout->addWidget(m_title_label); m_layout->addWidget(m_title_label);
m_layout->addItem(sparcer_item); m_layout->addItem(sparcer_item);
m_layout->addWidget(m_min_button); m_layout->addWidget(m_btn_vectors[1]);
m_layout->addWidget(m_max_button); m_layout->addWidget(m_btn_vectors[2]);
m_layout->addWidget(m_close_button); m_layout->addWidget(m_btn_vectors[3]);
m_layout->setSpacing(5); m_layout->setSpacing(5);
m_layout->setContentsMargins(5, 0, 5, 0); m_layout->setContentsMargins(5, 0, 5, 0);
setFixedHeight(40); setFixedHeight(40);
@ -64,7 +42,8 @@ sui_title::sui_title(QWidget *parent)
} }
sui_title::~sui_title() sui_title::~sui_title()
{ {
if(m_thread){ if (m_thread)
{
m_thread->quit(); m_thread->quit();
m_thread->exit(); m_thread->exit();
} }
@ -72,13 +51,12 @@ sui_title::~sui_title()
void sui_title::connect_slots() void sui_title::connect_slots()
{ {
// 连接三个按钮的信号槽3 for (auto btn : m_btn_vectors)
connect(m_icon_button, SIGNAL(clicked(bool)), this, SLOT(slots_ico_menu())); {
connect(m_min_button, SIGNAL(clicked(bool)), this, SLOT(on_clicked())); connect(btn, SIGNAL(clicked(bool)), this, SLOT(on_clicked()));
connect(m_max_button, SIGNAL(clicked(bool)), this, SLOT(on_clicked())); }
connect(m_close_button, SIGNAL(clicked(bool)), this, SLOT(on_clicked()));
} }
void sui_title::slots_ico_menu() void sui_title::btn_ico_menu()
{ {
m_menu = new QMenu(); m_menu = new QMenu();
// 外观 // 外观
@ -165,16 +143,16 @@ void sui_title::slots_style_system()
{ {
if (sender() != nullptr) if (sender() != nullptr)
{ {
if (_data.x_config.style_system!=0) if (_data.x_config.style_system != 0)
{ {
_data.x_config.style_system = 0; _data.x_config.style_system = 0;
m_style->state=_data.x_config.style_system; m_style->state = _data.x_config.style_system;
((QAction *)sender())->setChecked(false); ((QAction *)sender())->setChecked(false);
} }
else else
{ {
_data.x_config.style_system = 1; _data.x_config.style_system = 1;
m_style->state=_data.x_config.style_system; m_style->state = _data.x_config.style_system;
((QAction *)sender())->setChecked(true); ((QAction *)sender())->setChecked(true);
} }
} }
@ -245,46 +223,43 @@ void sui_title::change_style(int index)
QString _styleSheet = QLatin1String(qss.readAll()); QString _styleSheet = QLatin1String(qss.readAll());
m_parent->setStyleSheet(""); m_parent->setStyleSheet("");
m_parent->setStyleSheet(_styleSheet); m_parent->setStyleSheet(_styleSheet);
//QApplication::setStyle("Fusion"); // QApplication::setStyle("Fusion");
} }
qss.close(); qss.close();
//m_parent->update(); // m_parent->update();
} }
// 双击标题栏进行界面的最大化/还原 // 双击标题栏进行界面的最大化/还原
void sui_title::mouseDoubleClickEvent(QMouseEvent *event) void sui_title::mouseDoubleClickEvent(QMouseEvent *event)
{ {
Q_UNUSED(event); // 没有实质性的作用只是用来允许event可以不使用用来避免编译器警告 Q_UNUSED(event); // 没有实质性的作用只是用来允许event可以不使用用来避免编译器警告
emit m_max_button->clicked(); emit m_btn_vectors[2]->clicked();
} }
// 进行最小化、最大化/还原、关闭操作 // 进行最小化、最大化/还原、关闭操作
void sui_title::on_clicked() void sui_title::on_clicked()
{ {
// QObject::Sender()返回发送信号的对象的指针返回类型为QObject * if (sender()->objectName() == "btn_opt")
QPushButton *pButton = qobject_cast<QPushButton *>(sender());
QWidget *pWindow = this->window(); // 获得标题栏所在的窗口
if (pWindow->isWindow())
{ {
// 判断发送信号的对象使哪个按钮 btn_ico_menu();
if (pButton == m_min_button) }
else if (sender()->objectName() == "btn_min")
{
window()->showMinimized();
}
else if (sender()->objectName() == "btn_max")
{
window()->isFullScreen() ? window()->showNormal() : window()->showFullScreen();
}
else if (sender()->objectName() == "btn_close")
{
if (m_type == QD_TYPE::QD_CLOSE)
{ {
pWindow->showMinimized(); // 窗口最小化显示 emit signals_close();
} }
else if (pButton == m_max_button) else if (m_type == QD_TYPE::QD_EXIT)
{ {
pWindow->isFullScreen() ? pWindow->showNormal() : pWindow->showFullScreen(); xsteam_save_data_write();
} QApplication::quit();
else if (pButton == m_close_button)
{
if (m_type == QD_TYPE::QD_CLOSE)
{
emit signals_close();
}
else if (m_type == QD_TYPE::QD_EXIT)
{
xsteam_save_data_write();
QApplication::quit();
}
} }
} }
} }
@ -298,18 +273,18 @@ void sui_title::up_maximize()
if (bMaximize) if (bMaximize)
{ {
// 目前窗口是最大化状态,则最大化/还原的toolTip设置为"Restore" // 目前窗口是最大化状态,则最大化/还原的toolTip设置为"Restore"
m_max_button->setToolTip(tr("Restore")); m_btn_vectors[2]->setToolTip(tr("Restore"));
// 设置按钮的属性名为"maximizeProperty" // 设置按钮的属性名为"maximizeProperty"
m_max_button->setProperty("maximizeProperty", tr("restore")); m_btn_vectors[2]->setProperty("maximizeProperty", tr("restore"));
} }
else else
{ {
// 目前窗口是还原状态,则最大化/还原的toolTip设置为"Maximize" // 目前窗口是还原状态,则最大化/还原的toolTip设置为"Maximize"
m_max_button->setToolTip(tr("Maximize")); m_btn_vectors[2]->setToolTip(tr("Maximize"));
// 设置按钮的属性名为"maximizeProperty" // 设置按钮的属性名为"maximizeProperty"
m_max_button->setProperty("maximizeProperty", tr("maximize")); m_btn_vectors[2]->setProperty("maximizeProperty", tr("maximize"));
} }
m_max_button->setStyle(QApplication::style()); m_btn_vectors[2]->setStyle(QApplication::style());
} }
} }
void sui_title::set_type(QD_TYPE _type) void sui_title::set_type(QD_TYPE _type)
@ -317,18 +292,18 @@ void sui_title::set_type(QD_TYPE _type)
m_type = _type; m_type = _type;
if (m_type) if (m_type)
{ {
//启动系统主题监听线程 // 启动系统主题监听线程
m_style = new sui_sys_style(); m_style = new sui_sys_style();
m_thread=new QThread(); m_thread = new QThread();
m_style->moveToThread(m_thread); m_style->moveToThread(m_thread);
m_style->state=_data.x_config.style_system; m_style->state = _data.x_config.style_system;
connect(m_style, SIGNAL(signals_sys_style_change(int)), this, SLOT(change_style(int))); connect(m_style, SIGNAL(signals_sys_style_change(int)), this, SLOT(change_style(int)));
connect(m_thread, SIGNAL(started()), m_style, SLOT(slots_sys_style_thread())); connect(m_thread, SIGNAL(started()), m_style, SLOT(slots_sys_style_thread()));
m_thread->start(); m_thread->start();
} }
else else
{ {
disconnect(m_icon_button, SIGNAL(clicked(bool)), this, SLOT(slots_ico_menu())); disconnect(m_btn_vectors[0], SIGNAL(clicked(bool)), this, SLOT(btn_ico_menu()));
} }
} }
void sui_title::set_title(QString str) void sui_title::set_title(QString str)

View File

@ -47,23 +47,22 @@ private:
//最大化/还原 //最大化/还原
void up_maximize(); void up_maximize();
void connect_slots(); void connect_slots();
void btn_ico_menu();
private: private:
QWidget *m_parent; QWidget *m_parent;
QHBoxLayout *m_layout; QHBoxLayout *m_layout;
QPushButton *m_icon_button; //标题栏图标 QLabel *m_title_label;
QLabel *m_title_label; //标题栏标题 std::vector<QPushButton*> m_btn_vectors;
QPushButton *m_min_button; //最小化按钮 QSpacerItem *sparcer_item = new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Fixed);
QPushButton *m_max_button; //最大化/还原按钮 QMenu *m_menu;
QPushButton *m_close_button; //关闭按钮 QMenu *s_menu;
QSpacerItem *sparcer_item = new QSpacerItem(0,0,QSizePolicy::Expanding,QSizePolicy::Fixed); QMenu *f_menu;
QMenu* m_menu; QD_TYPE m_type;
QMenu* s_menu; QThread* m_thread=nullptr;
QMenu* f_menu;
QD_TYPE m_type;
QThread* m_thread=nullptr;
sui_sys_style* m_style=nullptr; sui_sys_style* m_style=nullptr;
public slots: QStringList m_btn_obj_str={"btn_opt","btn_min","btn_max","btn_close"};
void slots_ico_menu(); QStringList m_btn_tips_str={"功能","最小化","最大化","关闭"};
public slots:
void slots_set_style_change(); void slots_set_style_change();
void slots_set_font_change(); void slots_set_font_change();
void slots_style_system(); void slots_style_system();

View File

@ -559,7 +559,7 @@ void xsteam_run_steam_steamtools(RUN_MODE _mode)
} }
else if (_mode == RUN_MODE::STEAMTOOLS) else if (_mode == RUN_MODE::STEAMTOOLS)
{ {
run_steam_steamtools_path = QString::fromStdString(_data.x_config.steamtools_dir) + "Steamtools.exe"; run_steam_steamtools_path = QString::fromStdString(_data.x_config.steamtools_dir) + "/Steamtools.exe";
_tips = "未检测到SteamTools安装,请先配置SteamTools路径"; _tips = "未检测到SteamTools安装,请先配置SteamTools路径";
} }
if (QFile(run_steam_steamtools_path).exists()) if (QFile(run_steam_steamtools_path).exists())

View File

@ -29,7 +29,7 @@ public:
_instance = new xsteam_set_ui(); _instance = new xsteam_set_ui();
_instance->title()->set_type(QD_CLOSE); _instance->title()->set_type(QD_CLOSE);
_instance->title()->set_title(tr("XSteam设置")); _instance->title()->set_title(tr("XSteam设置"));
_instance->setWindowTitle(tr("XSteam设置")); _instance->setWindowTitle(tr("XSteam设置"));
} }
mutex.unlock(); mutex.unlock();
} }

View File

@ -46,6 +46,7 @@ void xsteam_ui::xsteam_init_info_layout()
} }
void xsteam_ui::xsteam_init_mind_layout() void xsteam_ui::xsteam_init_mind_layout()
{ {
title()->set_title(tr("XSteam"));
mind_widget = new QWidget(); mind_widget = new QWidget();
mind_layout = new QVBoxLayout(); mind_layout = new QVBoxLayout();
server_layout = new QHBoxLayout(); server_layout = new QHBoxLayout();