feat: v1.1 使用qApp->setStyle("xxx")显示系统其他风格

This commit is contained in:
touwoyimuli 2019-12-03 08:47:10 +08:00
parent 488485ee49
commit e4872abf51
6 changed files with 148 additions and 4 deletions

View File

@ -0,0 +1,27 @@
/*
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
*
* Author: touwoyimuli <touwoyimuli@gmai.com>
*
* github: https://github.com/touwoyimuli
* blogs: https://touwoyimuli.github.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://touwoyimuli.github.io/>.
*/
#include "ExMyStyle.h"
ExMyStyle::ExMyStyle()
{
}

View File

@ -0,0 +1,34 @@
/*
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
*
* Author: touwoyimuli <touwoyimuli@gmai.com>
*
* github: https://github.com/touwoyimuli
* blogs: https://touwoyimuli.github.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://touwoyimuli.github.io/>.
*/
#ifndef EXMYSTYLE_H
#define EXMYSTYLE_H
#include <QCommonStyle>
class ExMyStyle : public QCommonStyle
{
public:
ExMyStyle();
};
#endif // EXMYSTYLE_H

View File

@ -1,11 +1,69 @@
/*
* Copyright (C) 2019 ~ 2019 touwoyimuli. All rights reserved.
*
* Author: touwoyimuli <touwoyimuli@gmai.com>
*
* github: https://github.com/touwoyimuli
* blogs: https://touwoyimuli.github.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://touwoyimuli.github.io/>.
*/
#include "Example.h"
Example::Example(QWidget *parent)
: QWidget(parent)
{
}
Example::~Example()
{
}
void Example::init()
{
QTableWidget *table = new QTableWidget(10, 10, this);
QProgressBar *progressH = new QProgressBar(this);
QProgressBar *progressV = new QProgressBar(this);
table->move(10, 10);
progressH->move(300, 50);
progressH->setRange(0, 100);
progressH->setValue(34);
progressH->resize(380, 40);
progressV->move(50, 250);
progressV->setRange(0, 100);
progressV->setValue(67);
progressV->resize(50, 380);
progressV->setOrientation(Qt::Vertical);
int i = 0;
QStringList listStyle = QStyleFactory::keys();
foreach(QString val, listStyle) { //打印当前系统支持的系统风格
QPushButton *btn = new QPushButton(val, this);
btn->move(this->rect().right() - 100, this->rect().top() + i++ * 40);
connect(btn, &QPushButton::clicked, this, [=](){
qApp->setStyle(val);
});
}
QPushButton *btn = new QPushButton("ExMyStyle", this);
btn->move(this->rect().right() - 100, this->rect().top() + i++ * 40);
connect(btn, &QPushButton::clicked, this, [=](){
qApp->setStyle(QStyleFactory::create("ExMyStyle"));
});
}

View File

@ -22,7 +22,15 @@
#ifndef EXAMPLE_H
#define EXAMPLE_H
#include <QStyleFactory>
#include <QDebug>
#include <QWidget>
#include <QTableWidget>
#include <QProgressBar>
#include <QPushButton>
#include <QApplication>
#include "ExMyStyle.h"
class Example : public QWidget
{
@ -31,6 +39,8 @@ class Example : public QWidget
public:
Example(QWidget *parent = 0);
~Example();
void init();
};
#endif // EXAMPLE_H

View File

@ -26,10 +26,13 @@ CONFIG += c++11
SOURCES += \
main.cpp \
Example.cpp
Example.cpp \
ExMyStyle.cpp
HEADERS += \
Example.h
Example.h \
ExMyStyle.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin

View File

@ -21,12 +21,24 @@
*/
#include "Example.h"
#include <QApplication>
#include <QStyleFactory>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Example w;
w.show();
//若为系统自带的QStyle则qApp->setStyle("系统自带风格");
//若为自定义新的QStyle则qApp->setStyle(QStyleFactory::create("自定义风格")
QStringList listStyle = QStyleFactory::keys();
foreach(QString val, listStyle) //打印当前系统支持的系统风格
qDebug()<<val<<" ";
qApp->setStyle("chameleon"); //Windows/Fusion/...
Example *w = new Example();
w->resize(1000, 700);
w->init();
w->show();
return a.exec();
}