feat: 创建系统自定义样式风格ExCustomStyle

搭建一个框架,但是继承的类QCommonStyle里面的对应的虚函数还有没有重写,和体验感受win和linux系统自带多种风格样式
This commit is contained in:
touwoyimuli
2019-08-29 23:26:30 +08:00
parent 4342e0091c
commit 03b0948c86
10 changed files with 236 additions and 137 deletions

View File

@@ -1,6 +1,10 @@
#include "ExCustomStyle.h"
CUSTOMSTYLE_BEDGIN_NAMESPACE
ExCustomStyle::ExCustomStyle()
{
}
CUSTOMSTYLE_END_NAMESPACE

View File

@@ -3,6 +3,10 @@
#include <QCommonStyle>
#include "ExDefineGlobal.h"
CUSTOMSTYLE_BEDGIN_NAMESPACE
class ExCustomStyle : public QCommonStyle
{
Q_OBJECT
@@ -11,4 +15,6 @@ public:
ExCustomStyle();
};
CUSTOMSTYLE_END_NAMESPACE
#endif // EXCUSTOMSTYLE_H

View File

@@ -0,0 +1,13 @@
#ifndef EXDEFINEGLOBAL_H
#define EXDEFINEGLOBAL_H
//用来定义一些宏的使用: 尝试写大项目的架构
//定义命名空间的宏(在定义 class ExCustomStyle: 的时候用到了 eg: ExCustomStyle.h ExCustomStyle.cpp里面)
#define CUSTOMSTYLE_BEDGIN_NAMESPACE namespace touwoyimuliStyle {
#define CUSTOMSTYLE_END_NAMESPACE }
//定义使用命名空间((在使用 class ExCustomStyle的定义内容时候用到了 eg:Examples.cpp里面))
#define CUSTOMSTYLE_USE_NAMESPACE using namespace touwoyimuliStyle;
#endif // EXDEFINEGLOBAL_H

View File

@@ -1,15 +1,53 @@
#include "Examples.h"
#include "ui_Examples.h"
CUSTOMSTYLE_USE_NAMESPACE
Examples::Examples(QWidget *parent) :
QWidget(parent),
ui(new Ui::Examples)
{
ui->setupUi(this);
setWindowTitle(QObject::tr("创建自定义的CustomStyle风格"));
init();
}
Examples::~Examples()
{
delete ui;
}
//获取当前系统支持的默认系统风格
void Examples::init()
{
//当前系统支持的系统风格,放入QcomboBox的item里面,且打印出来
QStringList listStyle = QStyleFactory::keys();
foreach(QString val, listStyle) {
ui->comboBox->addItem(val);
qDebug()<<val;
}
//设置一个默认的风格
ui->comboBox->addItem("ExCustomStyle");
qApp->setStyle(QStyleFactory::create("Fusion"));
}
void Examples::on_comboBox_currentIndexChanged(const QString &style)
{
// qDebug()<<"当前选中的风格:"<<style;
//当前选中item项为系统预支持的风格
QStringList listStyle = QStyleFactory::keys();
foreach(QString val, listStyle) {
if (style == val) {
qApp->setStyle(QStyleFactory::create(style));
return;
}
}
//不属于系统风格,则使用自己的风格
ExCustomStyle* customStyle = new ExCustomStyle;
qApp->setStyle(customStyle);
}

View File

@@ -2,6 +2,10 @@
#define EXAMPLES_H
#include <QWidget>
#include <QStyleFactory>
#include <QDebug>
#include "ExCustomStyle.h"
namespace Ui {
class Examples;
@@ -15,6 +19,10 @@ public:
explicit Examples(QWidget *parent = nullptr);
~Examples();
void init(); //获取当前系统支持的默认系统风格
private slots:
void on_comboBox_currentIndexChanged(const QString &style);
private:
Ui::Examples *ui;
};

View File

@@ -26,141 +26,149 @@
<attribute name="title">
<string>滚动条</string>
</attribute>
<widget class="QScrollBar" name="scrollBarHor">
<widget class="QWidget" name="gridLayoutWidget">
<property name="geometry">
<rect>
<x>30</x>
<y>280</y>
<width>531</width>
<height>31</height>
<x>10</x>
<y>10</y>
<width>441</width>
<height>251</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QScrollBar" name="scrollBarVer">
<property name="geometry">
<rect>
<x>540</x>
<y>40</y>
<width>31</width>
<height>191</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
<widget class="QTableWidget" name="tableWidget">
<property name="geometry">
<rect>
<x>40</x>
<y>60</y>
<width>431</width>
<height>161</height>
</rect>
</property>
<row>
<property name="text">
<string>a</string>
</property>
</row>
<row>
<property name="text">
<string>b</string>
</property>
</row>
<row>
<property name="text">
<string>c</string>
</property>
</row>
<row>
<property name="text">
<string>d</string>
</property>
</row>
<row>
<property name="text">
<string>e</string>
</property>
</row>
<row>
<property name="text">
<string>f</string>
</property>
</row>
<column>
<property name="text">
<string>1</string>
</property>
</column>
<column>
<property name="text">
<string>2</string>
</property>
</column>
<column>
<property name="text">
<string>3</string>
</property>
</column>
<column>
<property name="text">
<string>4</string>
</property>
</column>
<column>
<property name="text">
<string>5</string>
</property>
</column>
<column>
<property name="text">
<string>6</string>
</property>
</column>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QTableWidget" name="tableWidget">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximumSize">
<size>
<width>400</width>
<height>200</height>
</size>
</property>
<row>
<property name="text">
<string>a</string>
</property>
</row>
<row>
<property name="text">
<string>b</string>
</property>
</row>
<row>
<property name="text">
<string>c</string>
</property>
</row>
<row>
<property name="text">
<string>d</string>
</property>
</row>
<row>
<property name="text">
<string>e</string>
</property>
</row>
<row>
<property name="text">
<string>f</string>
</property>
</row>
<column>
<property name="text">
<string>1</string>
</property>
</column>
<column>
<property name="text">
<string>2</string>
</property>
</column>
<column>
<property name="text">
<string>3</string>
</property>
</column>
<column>
<property name="text">
<string>4</string>
</property>
</column>
<column>
<property name="text">
<string>5</string>
</property>
</column>
<column>
<property name="text">
<string>6</string>
</property>
</column>
</widget>
</item>
<item row="0" column="1">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Fixed</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="0" colspan="2">
<widget class="QScrollBar" name="scrollBarHor">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="0" column="2" rowspan="2">
<widget class="QScrollBar" name="scrollBarVer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QComboBox" name="comboBox">
<property name="geometry">
<rect>
<x>650</x>
<y>40</y>
<width>131</width>
<height>22</height>
<x>470</x>
<y>10</y>
<width>135</width>
<height>31</height>
</rect>
</property>
<item>
<property name="text">
<string>Windows</string>
</property>
</item>
<item>
<property name="text">
<string>New Item</string>
</property>
</item>
<item>
<property name="text">
<string>WindowsXP</string>
</property>
</item>
<item>
<property name="text">
<string>WindowsVista</string>
</property>
</item>
<item>
<property name="text">
<string>Fusion</string>
</property>
</item>
<item>
<property name="text">
<string>ExCustomStyle</string>
</property>
</item>
</widget>
</widget>
<widget class="QWidget" name="tab_2">

View File

@@ -31,12 +31,13 @@ SOURCES += \
HEADERS += \
Examples.h \
ExCustomStyle.h
ExCustomStyle.h \
ExDefineGlobal.h
FORMS += \
Examples.ui
RC_ICONS += QT_win_32x32.ico
RC_ICONS += qt.ico
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin

View File

@@ -9,7 +9,7 @@ int main(int argc, char *argv[])
{
QApplication a(argc, argv);
//使用系统或者自定义的风格
// //使用系统或者自定义的风格
// QStringList listStyle = QStyleFactory::keys();
// foreach(QString val, listStyle)
// qDebug()<<val<<" ";

View File

Before

Width:  |  Height:  |  Size: 4.2 KiB

After

Width:  |  Height:  |  Size: 4.2 KiB