新增超级曲线图表

This commit is contained in:
feiyangqingyun
2021-12-10 20:34:18 +08:00
parent 1a2302c05d
commit e215d3989b
143 changed files with 120241 additions and 5 deletions

View File

@@ -0,0 +1,13 @@
FORMS += \
$$PWD/frmmain.ui \
$$PWD/frmmultiaxes.ui
HEADERS += \
$$PWD/frmmain.h \
$$PWD/frmmultiaxes.h \
$$PWD/iconhelper.h
SOURCES += \
$$PWD/frmmain.cpp \
$$PWD/frmmultiaxes.cpp \
$$PWD/iconhelper.cpp

View File

@@ -0,0 +1,149 @@
#pragma execution_character_set("utf-8")
#include "frmmain.h"
#include "ui_frmmain.h"
#include "iconhelper.h"
#include "qdebug.h"
#include "frmquadratic.h"
#include "frmsimple.h"
#include "frmsincscatter.h"
#include "frmscatterstyle.h"
#include "frmscatterpixmap.h"
#include "frmlinestyle.h"
#include "frmdate.h"
#include "frmtexturebrush.h"
#include "frmmultiaxis.h"
#include "frmlogarithmic.h"
#include "frmrealtimedata.h"
#include "frmparametriccurve.h"
#include "frmbarchart.h"
#include "frmstatistical.h"
#include "frmsimpleitem.h"
#include "frmitem.h"
#include "frmstyled.h"
#include "frmadvancedaxes.h"
#include "frmcolormap.h"
#include "frmfinancial.h"
#include "frmpolarplot.h"
#include "frmaxistag.h"
#include "frminteraction.h"
#include "frmscrollbar.h"
#include "frmmultiaxes.h"
frmMain::frmMain(QWidget *parent) : QWidget(parent), ui(new Ui::frmMain)
{
ui->setupUi(this);
this->initForm();
this->initWidget();
this->initNav();
this->initIcon();
}
frmMain::~frmMain()
{
delete ui;
}
void frmMain::showEvent(QShowEvent *)
{
//滚动到底部
QScrollBar *bar = ui->scrollArea->verticalScrollBar();
bar->setValue(bar->maximum());
}
void frmMain::initForm()
{
ui->scrollArea->setFixedWidth(170);
ui->widgetLeft->setProperty("flag", "left");
}
void frmMain::initWidget()
{
//ui->customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
ui->stackedWidget->addWidget(new frmQuadratic);
ui->stackedWidget->addWidget(new frmSimple);
ui->stackedWidget->addWidget(new frmSincScatter);
ui->stackedWidget->addWidget(new frmScatterStyle);
ui->stackedWidget->addWidget(new frmScatterPixmap);
ui->stackedWidget->addWidget(new frmLineStyle);
ui->stackedWidget->addWidget(new frmDate);
ui->stackedWidget->addWidget(new frmTextureBrush);
ui->stackedWidget->addWidget(new frmMultiAxis);
ui->stackedWidget->addWidget(new frmLogarithmic);
ui->stackedWidget->addWidget(new frmRealtimeData);
ui->stackedWidget->addWidget(new frmParametricCurve);
ui->stackedWidget->addWidget(new frmBarChart);
ui->stackedWidget->addWidget(new frmStatistical);
ui->stackedWidget->addWidget(new frmSimpleItem);
ui->stackedWidget->addWidget(new frmItem);
ui->stackedWidget->addWidget(new frmStyled);
ui->stackedWidget->addWidget(new frmAdvancedAxes);
ui->stackedWidget->addWidget(new frmColorMap);
ui->stackedWidget->addWidget(new frmFinancial);
ui->stackedWidget->addWidget(new frmPolarPlot);
ui->stackedWidget->addWidget(new frmAxisTag);
ui->stackedWidget->addWidget(new frmInterAction);
ui->stackedWidget->addWidget(new frmScrollBar);
ui->stackedWidget->addWidget(new frmMultiAxes);
}
void frmMain::initNav()
{
//按钮文字集合
QStringList names;
names << "平方二次元" << "简单曲线图" << "正弦散点图" << "数据点样式" << "数据点图片"
<< "曲线条样式" << "日期数据图" << "纹理画刷图" << "双坐标曲线" << "对数曲线图"
<< "动态正弦图" << "环形曲线图" << "垂直柱状图" << "箱形盒须图" << "静态指示线"
<< "动态指示线" << "曲线样式图" << "高级坐标轴" << "颜色热力图" << "金融曲线图"
<< "南丁格尔图" << "坐标轴指示" << "相互作用图" << "滚动条曲线" << "多坐标轴图";
//自动生成按钮
for (int i = 0; i < names.count(); i++) {
QToolButton *btn = new QToolButton;
//设置按钮固定高度
btn->setFixedHeight(35);
//设置按钮的文字
btn->setText(QString("%1. %2").arg(i + 1, 2, 10, QChar('0')).arg(names.at(i)));
//设置按钮可选中按下类似复选框的功能
btn->setCheckable(true);
//设置按钮图标在左侧文字在右侧
btn->setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
//设置按钮拉伸策略为横向填充
btn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Preferred);
//关联按钮单击事件
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClicked()));
//将按钮加入到布局
ui->widgetLeft->layout()->addWidget(btn);
//可以指定不同的图标
icons << 0xf061;
btns << btn;
}
//底部加个弹簧
QSpacerItem *verticalSpacer = new QSpacerItem(20, 40, QSizePolicy::Minimum, QSizePolicy::Expanding);
ui->widgetLeft->layout()->addItem(verticalSpacer);
btns.at(names.count() - 1)->click();
}
void frmMain::initIcon()
{
//左侧导航样式,可以设置各种牛逼的参数,超级棒
IconHelper::StyleColor styleColor;
styleColor.defaultBorder = true;
IconHelper::setStyle(ui->widgetLeft, btns, icons, styleColor);
}
void frmMain::buttonClicked()
{
QAbstractButton *b = (QAbstractButton *)sender();
int count = btns.count();
int index = btns.indexOf(b);
ui->stackedWidget->setCurrentIndex(index);
for (int i = 0; i < count; i++) {
QAbstractButton *btn = btns.at(i);
btn->setChecked(btn == b);
}
}

View File

@@ -0,0 +1,37 @@
#ifndef FRMMAIN_H
#define FRMMAIN_H
#include <QWidget>
class QAbstractButton;
namespace Ui {
class frmMain;
}
class frmMain : public QWidget
{
Q_OBJECT
public:
explicit frmMain(QWidget *parent = 0);
~frmMain();
protected:
void showEvent(QShowEvent *);
private:
Ui::frmMain *ui;
//左侧导航栏图标+按钮集合
QList<int> icons;
QList<QAbstractButton *> btns;
private slots:
void initForm(); //初始化界面数据
void initWidget(); //初始化子窗体
void initNav(); //初始化导航按钮
void initIcon(); //初始化导航按钮图标
void buttonClicked(); //导航按钮单击事件
};
#endif // FRMMAIN_H

View File

@@ -0,0 +1,85 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmMain</class>
<widget class="QWidget" name="frmMain">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>635</width>
<height>429</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QScrollArea" name="scrollArea">
<property name="minimumSize">
<size>
<width>180</width>
<height>0</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>180</width>
<height>16777215</height>
</size>
</property>
<property name="widgetResizable">
<bool>true</bool>
</property>
<widget class="QWidget" name="widgetLeft">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>178</width>
<height>427</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<property name="spacing">
<number>0</number>
</property>
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
</layout>
</widget>
</widget>
</item>
<item>
<widget class="QStackedWidget" name="stackedWidget"/>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>

View File

@@ -0,0 +1,186 @@
#include "frmmultiaxes.h"
#include "ui_frmmultiaxes.h"
#include "qfile.h"
#include "qdebug.h"
frmMultiAxes::frmMultiAxes(QWidget *parent) : QWidget(parent), ui(new Ui::frmMultiAxes)
{
ui->setupUi(this);
this->initForm();
}
frmMultiAxes::~frmMultiAxes()
{
delete ui;
}
void frmMultiAxes::initForm()
{
//Y坐标轴的数量
count = 4;
this->initPlot(ui->customPlot1, QCPAxis::atLeft, count);
this->initPlot(ui->customPlot2, QCPAxis::atRight, count);
this->loadPlot(ui->customPlot1, QCPAxis::atLeft, count);
this->loadPlot(ui->customPlot2, QCPAxis::atRight, count);
for (int i = 0; i < count; ++i) {
this->loadData(ui->customPlot1, i);
this->loadData(ui->customPlot2, i);
}
}
void frmMultiAxes::selectionChangedByUser()
{
//联动处理 坐标轴、刻度尺、画布曲线 选中一样则选中所有
QCustomPlot *customPlot = (QCustomPlot *)sender();
QCPAxis::AxisType type;
if (customPlot == ui->customPlot1) {
type = QCPAxis::atLeft;
} else if (customPlot == ui->customPlot2) {
type = QCPAxis::atRight;
}
int selecteIndex = -1;
for (int i = 0; i < count; ++i) {
//获取对应的坐标轴
QCPAxis *axis = customPlot->axisRect()->axis(type, i);
//获取对应的画布曲线
QCPGraph *graph = customPlot->graph(i);
//判断 坐标轴、刻度尺、画布曲线 是否选中了一个
if (axis->selectedParts().testFlag(QCPAxis::spAxis) || axis->selectedParts().testFlag(QCPAxis::spTickLabels) || graph->selected()) {
//设置坐标轴、刻度尺同时选中
axis->setSelectedParts(QCPAxis::spAxis | QCPAxis::spTickLabels);
//设置画布曲线选中
graph->setSelection(QCPDataSelection(graph->data()->dataRange()));
selecteIndex = i;
break;
}
}
//存在选中则设置当前选中的可拖动和缩放
//不存在则设置所有的可拖动和缩放
QCPAxisRect *axisRect = customPlot->axisRect();
if (selecteIndex >= 0) {
QCPAxis *axis = axisRect->axis(type, selecteIndex);
axisRect->setRangeDragAxes(customPlot->xAxis, axis);
axisRect->setRangeZoomAxes(customPlot->xAxis, axis);
} else {
#ifndef qcustomplot_v1_3
axisRect->setRangeDragAxes(axisRect->axes());
axisRect->setRangeZoomAxes(axisRect->axes());
#endif
}
}
void frmMultiAxes::initPlot(QCustomPlot *customPlot, const QCPAxis::AxisType &type, int count)
{
//选中对应的坐标轴或者画布等
connect(customPlot, SIGNAL(selectionChangedByUser()), this, SLOT(selectionChangedByUser()));
//connect(customPlot->yAxis2, SIGNAL(rangeChanged(QCPRange)), customPlot->yAxis, SLOT(setRange(QCPRange)));
if (type == QCPAxis::atLeft) {
customPlot->yAxis->setVisible(true);
customPlot->yAxis2->setVisible(false);
} else if (type == QCPAxis::atRight) {
customPlot->yAxis->setVisible(false);
customPlot->yAxis2->setVisible(true);
}
//添加多个坐标轴
for (int i = 0; i < count - 1; ++i) {
customPlot->axisRect()->addAxis(type);
}
//设置边距、范围值、网格可见、刻度数量等
for (int i = 0; i < count; ++i) {
QCPAxis *axis = customPlot->axisRect()->axis(type, i);
axis->setPadding(10);
axis->setRange(0, 1);
axis->grid()->setVisible(true);
//axis->grid()->setSubGridVisible(true);
axis->ticker()->setTickCount(10);
}
//设置初始范围值
customPlot->xAxis->setRange(0.5, 10.5);
//设置刻度数量
customPlot->xAxis->ticker()->setTickCount(10);
//设置可拖动和缩放选中等
customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectAxes | QCP::iSelectPlottables);
}
void frmMultiAxes::loadPlot(QCustomPlot *customPlot, const QCPAxis::AxisType &type, int count)
{
//颜色集合
QList<QColor> colors;
colors << QColor(211, 78, 78) << QColor(29, 185, 242) << QColor(170, 162, 119) << QColor(255, 192, 1);
colors << QColor(0, 176, 180) << QColor(0, 113, 193) << QColor(255, 192, 0);
colors << QColor(72, 103, 149) << QColor(185, 87, 86) << QColor(0, 177, 125);
colors << QColor(214, 77, 84) << QColor(71, 164, 233) << QColor(34, 163, 169);
colors << QColor(59, 123, 156) << QColor(162, 121, 197) << QColor(72, 202, 245);
colors << QColor(0, 150, 121) << QColor(111, 9, 176) << QColor(250, 170, 20);
for (int i = 0; i < count; ++i) {
//分配一种颜色
QColor color;
if (i < colors.count()) {
color = colors.at(i);
}
QPen pen(color);
QCPAxis *axis = customPlot->axisRect()->axis(type, i);
//刻度文本颜色
axis->setTickLabelColor(color);
//刻度线条颜色
axis->setBasePen(pen);
//大刻度尺颜色
axis->setTickPen(pen);
//小刻度尺颜色
axis->setSubTickPen(pen);
//画布线条颜色
QCPGraph *graph = customPlot->addGraph(customPlot->xAxis, axis);
graph->setPen(pen);
}
}
void frmMultiAxes::loadData(QCustomPlot *customPlot, int index)
{
//下面演示从文本文件读取数据,具体格式可以自行打开文件查看
//真实场景自己准备数据调用 setData 即可
int flag = index > 2 ? 0 : index;
QString fileName = QString(":/data/data%1.txt").arg(flag);
QFile file(fileName);
if (file.open(QFile::ReadOnly | QIODevice::Text)) {
QString value = file.readAll();
file.close();
QVector<double> keys, values;
QStringList list = value.split(" ");
int len = list.length();
//如果不是2的倍数则长度要减去1
if (len % 2 != 0) {
len = (len - 1);
}
for (int i = 0; i < len / 2; i++) {
keys << i;
}
//每两个16进制数字组合成一个数字
for (int i = 0; i < len; i = i + 2) {
QString strHex = QString("%1%2").arg(list.at(i)).arg(list.at(i + 1));
qint16 value = strHex.toUShort(NULL, 16);
values << value;
}
//获取对应画布
QCPGraph *graph = customPlot->graph(index);
//设置数据到画布
graph->setData(keys, values);
//自适应坐标轴
graph->rescaleAxes();
}
}

View File

@@ -0,0 +1,36 @@
#ifndef FRMMULTIAXES_H
#define FRMMULTIAXES_H
#include <QWidget>
#include "qcustomplot.h"
namespace Ui {
class frmMultiAxes;
}
class frmMultiAxes : public QWidget
{
Q_OBJECT
public:
explicit frmMultiAxes(QWidget *parent = 0);
~frmMultiAxes();
private:
Ui::frmMultiAxes *ui;
int count;
private slots:
//初始化界面数据
void initForm();
//用户按下区域改变
void selectionChangedByUser();
//初始化图表控件
void initPlot(QCustomPlot *customPlot, const QCPAxis::AxisType &type, int count);
//加载图表控件
void loadPlot(QCustomPlot *customPlot, const QCPAxis::AxisType &type, int count);
//加载数据到图表控件
void loadData(QCustomPlot *customPlot, int index);
};
#endif // FRMMULTIAXES_H

View File

@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>frmMultiAxes</class>
<widget class="QWidget" name="frmMultiAxes">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>400</width>
<height>300</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QCustomPlot" name="customPlot1" native="true"/>
</item>
<item>
<widget class="QCustomPlot" name="customPlot2" native="true"/>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>QCustomPlot</class>
<extends>QWidget</extends>
<header location="global">qcustomplot.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@@ -0,0 +1,359 @@
#include "iconhelper.h"
IconHelper *IconHelper::iconFontAliBaBa = 0;
IconHelper *IconHelper::iconFontAwesome = 0;
IconHelper *IconHelper::iconFontWeather = 0;
int IconHelper::iconFontIndex = -1;
void IconHelper::initFont()
{
static bool isInit = false;
if (!isInit) {
isInit = true;
if (iconFontAliBaBa == 0) {
iconFontAliBaBa = new IconHelper(":/font/iconfont.ttf", "iconfont");
}
if (iconFontAwesome == 0) {
iconFontAwesome = new IconHelper(":/font/fontawesome-webfont.ttf", "FontAwesome");
}
if (iconFontWeather == 0) {
iconFontWeather = new IconHelper(":/font/pe-icon-set-weather.ttf", "pe-icon-set-weather");
}
}
}
QFont IconHelper::getIconFontAliBaBa()
{
initFont();
return iconFontAliBaBa->getIconFont();
}
QFont IconHelper::getIconFontAwesome()
{
initFont();
return iconFontAwesome->getIconFont();
}
QFont IconHelper::getIconFontWeather()
{
initFont();
return iconFontWeather->getIconFont();
}
IconHelper *IconHelper::getIconHelper(int icon)
{
initFont();
//指定了字体索引则取对应索引的字体类
//没指定则自动根据不同的字体的值选择对应的类
//由于部分值范围冲突所以可以指定索引来取
//fontawesome 0xf000-0xf2e0
//iconfont 0xe501-0xe793 0xe8d5-0xea5d
//weather 0xe900-0xe9cf
IconHelper *iconHelper = iconFontAwesome;
if (iconFontIndex < 0) {
if ((icon > 0xe501 && icon < 0xe793) || (icon > 0xe8d5 && icon < 0xea5d)) {
iconHelper = iconFontAliBaBa;
}
} else if (iconFontIndex == 0) {
iconHelper = iconFontAliBaBa;
} else if (iconFontIndex == 1) {
iconHelper = iconFontAwesome;
} else if (iconFontIndex == 2) {
iconHelper = iconFontWeather;
}
return iconHelper;
}
void IconHelper::setIcon(QLabel *lab, int icon, quint32 size)
{
getIconHelper(icon)->setIcon1(lab, icon, size);
}
void IconHelper::setIcon(QAbstractButton *btn, int icon, quint32 size)
{
getIconHelper(icon)->setIcon1(btn, icon, size);
}
void IconHelper::setPixmap(QAbstractButton *btn, const QColor &color, int icon, quint32 size,
quint32 width, quint32 height, int flags)
{
getIconHelper(icon)->setPixmap1(btn, color, icon, size, width, height, flags);
}
QPixmap IconHelper::getPixmap(const QColor &color, int icon, quint32 size,
quint32 width, quint32 height, int flags)
{
return getIconHelper(icon)->getPixmap1(color, icon, size, width, height, flags);
}
void IconHelper::setStyle(QWidget *widget, QList<QPushButton *> btns,
QList<int> icons, const IconHelper::StyleColor &styleColor)
{
int icon = icons.first();
getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
}
void IconHelper::setStyle(QWidget *widget, QList<QToolButton *> btns,
QList<int> icons, const IconHelper::StyleColor &styleColor)
{
int icon = icons.first();
getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
}
void IconHelper::setStyle(QWidget *widget, QList<QAbstractButton *> btns,
QList<int> icons, const IconHelper::StyleColor &styleColor)
{
int icon = icons.first();
getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
}
IconHelper::IconHelper(const QString &fontFile, const QString &fontName, QObject *parent) : QObject(parent)
{
//判断图形字体是否存在,不存在则加入
QFontDatabase fontDb;
if (!fontDb.families().contains(fontName)) {
int fontId = fontDb.addApplicationFont(fontFile);
QStringList listName = fontDb.applicationFontFamilies(fontId);
if (listName.count() == 0) {
qDebug() << QString("load %1 error").arg(fontName);
}
}
//再次判断是否包含字体名称防止加载失败
if (fontDb.families().contains(fontName)) {
iconFont = QFont(fontName);
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
iconFont.setHintingPreference(QFont::PreferNoHinting);
#endif
}
}
bool IconHelper::eventFilter(QObject *watched, QEvent *event)
{
//根据不同的
if (watched->inherits("QAbstractButton")) {
QAbstractButton *btn = (QAbstractButton *)watched;
int index = btns.indexOf(btn);
if (index >= 0) {
//不同的事件设置不同的图标,同时区分选中的和没有选中的
if (btn->isChecked()) {
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = (QMouseEvent *)event;
if (mouseEvent->button() == Qt::LeftButton) {
btn->setIcon(QIcon(pixChecked.at(index)));
}
} else if (event->type() == QEvent::Enter) {
btn->setIcon(QIcon(pixChecked.at(index)));
} else if (event->type() == QEvent::Leave) {
btn->setIcon(QIcon(pixChecked.at(index)));
}
} else {
if (event->type() == QEvent::MouseButtonPress) {
QMouseEvent *mouseEvent = (QMouseEvent *)event;
if (mouseEvent->button() == Qt::LeftButton) {
btn->setIcon(QIcon(pixPressed.at(index)));
}
} else if (event->type() == QEvent::Enter) {
btn->setIcon(QIcon(pixHover.at(index)));
} else if (event->type() == QEvent::Leave) {
btn->setIcon(QIcon(pixNormal.at(index)));
}
}
}
}
return QObject::eventFilter(watched, event);
}
void IconHelper::toggled(bool checked)
{
//选中和不选中设置不同的图标
QAbstractButton *btn = (QAbstractButton *)sender();
int index = btns.indexOf(btn);
if (checked) {
btn->setIcon(QIcon(pixChecked.at(index)));
} else {
btn->setIcon(QIcon(pixNormal.at(index)));
}
}
QFont IconHelper::getIconFont()
{
return this->iconFont;
}
void IconHelper::setIcon1(QLabel *lab, int icon, quint32 size)
{
iconFont.setPixelSize(size);
lab->setFont(iconFont);
lab->setText((QChar)icon);
}
void IconHelper::setIcon1(QAbstractButton *btn, int icon, quint32 size)
{
iconFont.setPixelSize(size);
btn->setFont(iconFont);
btn->setText((QChar)icon);
}
void IconHelper::setPixmap1(QAbstractButton *btn, const QColor &color, int icon, quint32 size,
quint32 width, quint32 height, int flags)
{
btn->setIcon(getPixmap1(color, icon, size, width, height, flags));
}
QPixmap IconHelper::getPixmap1(const QColor &color, int icon, quint32 size,
quint32 width, quint32 height, int flags)
{
//主动绘制图形字体到图片
QPixmap pix(width, height);
pix.fill(Qt::transparent);
QPainter painter;
painter.begin(&pix);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.setPen(color);
iconFont.setPixelSize(size);
painter.setFont(iconFont);
painter.drawText(pix.rect(), flags, (QChar)icon);
painter.end();
return pix;
}
void IconHelper::setStyle1(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
{
QList<QAbstractButton *> list;
foreach (QPushButton *btn, btns) {
list << btn;
}
setStyle(widget, list, icons, styleColor);
}
void IconHelper::setStyle1(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
{
QList<QAbstractButton *> list;
foreach (QToolButton *btn, btns) {
list << btn;
}
setStyle(widget, list, icons, styleColor);
}
void IconHelper::setStyle1(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const IconHelper::StyleColor &styleColor)
{
int btnCount = btns.count();
int iconCount = icons.count();
if (btnCount <= 0 || iconCount <= 0 || btnCount != iconCount) {
return;
}
QString position = styleColor.position;
quint32 iconSize = styleColor.iconSize;
quint32 iconWidth = styleColor.iconWidth;
quint32 iconHeight = styleColor.iconHeight;
quint32 borderWidth = styleColor.borderWidth;
//根据不同的位置计算边框
QString strBorder;
if (position == "top") {
strBorder = QString("border-width:%1px 0px 0px 0px;padding-top:%1px;padding-bottom:%2px;")
.arg(borderWidth).arg(borderWidth * 2);
} else if (position == "right") {
strBorder = QString("border-width:0px %1px 0px 0px;padding-right:%1px;padding-left:%2px;")
.arg(borderWidth).arg(borderWidth * 2);
} else if (position == "bottom") {
strBorder = QString("border-width:0px 0px %1px 0px;padding-bottom:%1px;padding-top:%2px;")
.arg(borderWidth).arg(borderWidth * 2);
} else if (position == "left") {
strBorder = QString("border-width:0px 0px 0px %1px;padding-left:%1px;padding-right:%2px;")
.arg(borderWidth).arg(borderWidth * 2);
}
//如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色
//如果图标在文字上面而设置的边框是 top bottom 也需要启用加深边框
QStringList qss;
if (styleColor.defaultBorder) {
qss << QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:solid;border-radius:0px;%2border-color:%3;color:%4;background:%5;}")
.arg(position).arg(strBorder).arg(styleColor.normalBgColor).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor);
} else {
qss << QString("QWidget[flag=\"%1\"] QAbstractButton{border-style:none;border-radius:0px;padding:5px;color:%2;background:%3;}")
.arg(position).arg(styleColor.normalTextColor).arg(styleColor.normalBgColor);
}
//悬停+按下+选中
qss << QString("QWidget[flag=\"%1\"] QAbstractButton:hover{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
.arg(position).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.hoverTextColor).arg(styleColor.hoverBgColor);
qss << QString("QWidget[flag=\"%1\"] QAbstractButton:pressed{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
.arg(position).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.pressedTextColor).arg(styleColor.pressedBgColor);
qss << QString("QWidget[flag=\"%1\"] QAbstractButton:checked{border-style:solid;%2border-color:%3;color:%4;background:%5;}")
.arg(position).arg(strBorder).arg(styleColor.borderColor).arg(styleColor.checkedTextColor).arg(styleColor.checkedBgColor);
//窗体背景颜色+按钮背景颜色
qss << QString("QWidget#%1{background:%2;}")
.arg(widget->objectName()).arg(styleColor.normalBgColor);
qss << QString("QWidget>QAbstractButton{border-width:0px;background-color:%1;color:%2;}")
.arg(styleColor.normalBgColor).arg(styleColor.normalTextColor);
qss << QString("QWidget>QAbstractButton:hover{background-color:%1;color:%2;}")
.arg(styleColor.hoverBgColor).arg(styleColor.hoverTextColor);
qss << QString("QWidget>QAbstractButton:pressed{background-color:%1;color:%2;}")
.arg(styleColor.pressedBgColor).arg(styleColor.pressedTextColor);
qss << QString("QWidget>QAbstractButton:checked{background-color:%1;color:%2;}")
.arg(styleColor.checkedBgColor).arg(styleColor.checkedTextColor);
//设置样式表
widget->setStyleSheet(qss.join(""));
//可能会重复调用设置所以先要移除上一次的
for (int i = 0; i < btnCount; i++) {
for (int j = 0; j < this->btns.count(); j++) {
if (this->btns.at(j) == btns.at(i)) {
disconnect(btns.at(i), SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
this->btns.at(j)->removeEventFilter(this);
this->btns.removeAt(j);
this->pixNormal.removeAt(j);
this->pixHover.removeAt(j);
this->pixPressed.removeAt(j);
this->pixChecked.removeAt(j);
break;
}
}
}
//存储对应按钮对象,方便鼠标移上去的时候切换图片
int checkedIndex = -1;
for (int i = 0; i < btnCount; i++) {
int icon = icons.at(i);
QPixmap pixNormal = getPixmap1(styleColor.normalTextColor, icon, iconSize, iconWidth, iconHeight);
QPixmap pixHover = getPixmap1(styleColor.hoverTextColor, icon, iconSize, iconWidth, iconHeight);
QPixmap pixPressed = getPixmap1(styleColor.pressedTextColor, icon, iconSize, iconWidth, iconHeight);
QPixmap pixChecked = getPixmap1(styleColor.checkedTextColor, icon, iconSize, iconWidth, iconHeight);
//记住最后选中的按钮
QAbstractButton *btn = btns.at(i);
if (btn->isChecked()) {
checkedIndex = i;
}
btn->setIcon(QIcon(pixNormal));
btn->setIconSize(QSize(iconWidth, iconHeight));
btn->installEventFilter(this);
connect(btn, SIGNAL(toggled(bool)), this, SLOT(toggled(bool)));
this->btns << btn;
this->pixNormal << pixNormal;
this->pixHover << pixHover;
this->pixPressed << pixPressed;
this->pixChecked << pixChecked;
}
//主动触发一下选中的按钮
if (checkedIndex >= 0) {
QMetaObject::invokeMethod(btns.at(checkedIndex), "toggled", Q_ARG(bool, true));
}
}

View File

@@ -0,0 +1,176 @@
#ifndef ICONHELPER_H
#define ICONHELPER_H
/**
* 超级图形字体类 作者:feiyangqingyun(QQ:517216493) 2016-11-23
* 1. 可传入多种图形字体文件,一个类通用所有图形字体。
* 2. 默认已经内置了阿里巴巴图形字体FontAliBaBa、国际知名图形字体FontAwesome、天气图形字体FontWeather。
* 3. 可设置 QLabel、QAbstractButton 文本为图形字体。
* 4. 可设置图形字体作为 QAbstractButton 按钮图标。
* 5. 内置万能的方法 getPixmap 将图形字体值转换为图片。
* 6. 无论是设置文本、图标、图片等都可以设置图标的大小、尺寸、颜色等参数。
* 7. 内置超级导航栏样式设置,将图形字体作为图标设置到按钮。
* 8. 支持各种颜色设置比如正常颜色、悬停颜色、按下颜色、选中颜色。
* 9. 可设置导航的位置为 left、right、top、bottom 四种。
* 10. 可设置导航加深边框颜色和粗细大小。
* 11. 导航面板的各种切换效果比如鼠标悬停、按下、选中等都自动处理掉样式设置。
* 12. 全局静态方法,接口丰富,使用极其简单方便。
*/
#include <QtGui>
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
#include <QtWidgets>
#endif
#ifdef quc
class Q_DECL_EXPORT IconHelper : public QObject
#else
class IconHelper : public QObject
#endif
{
Q_OBJECT
public:
//样式颜色结构体
struct StyleColor {
QString position; //位置 left right top bottom
bool defaultBorder; //默认有边框
quint32 iconSize; //图标字体尺寸
quint32 iconWidth; //图标图片宽度
quint32 iconHeight; //图标图片高度
quint32 borderWidth; //边框宽度
QString borderColor; //边框颜色
QString normalBgColor; //正常背景颜色
QString normalTextColor; //正常文字颜色
QString hoverBgColor; //悬停背景颜色
QString hoverTextColor; //悬停文字颜色
QString pressedBgColor; //按下背景颜色
QString pressedTextColor; //按下文字颜色
QString checkedBgColor; //选中背景颜色
QString checkedTextColor; //选中文字颜色
StyleColor() {
position = "left";
defaultBorder = false;
iconSize = 12;
iconWidth = 15;
iconHeight = 15;
borderWidth = 3;
borderColor = "#029FEA";
normalBgColor = "#292F38";
normalTextColor = "#54626F";
hoverBgColor = "#40444D";
hoverTextColor = "#FDFDFD";
pressedBgColor = "#404244";
pressedTextColor = "#FDFDFD";
checkedBgColor = "#44494F";
checkedTextColor = "#FDFDFD";
}
//设置常规颜色 普通状态+加深状态
void setColor(const QString &normalBgColor,
const QString &normalTextColor,
const QString &darkBgColor,
const QString &darkTextColor) {
this->normalBgColor = normalBgColor;
this->normalTextColor = normalTextColor;
this->hoverBgColor = darkBgColor;
this->hoverTextColor = darkTextColor;
this->pressedBgColor = darkBgColor;
this->pressedTextColor = darkTextColor;
this->checkedBgColor = darkBgColor;
this->checkedTextColor = darkTextColor;
}
};
//阿里巴巴图形字体类
static IconHelper *iconFontAliBaBa;
//FontAwesome图形字体类
static IconHelper *iconFontAwesome;
//天气图形字体类
static IconHelper *iconFontWeather;
//图形字体索引
static int iconFontIndex;
//初始化图形字体
static void initFont();
//获取图形字体
static QFont getIconFontAliBaBa();
static QFont getIconFontAwesome();
static QFont getIconFontWeather();
//根据值获取图形字体类
static IconHelper *getIconHelper(int icon);
//设置图形字体到标签
static void setIcon(QLabel *lab, int icon, quint32 size = 12);
//设置图形字体到按钮
static void setIcon(QAbstractButton *btn, int icon, quint32 size = 12);
//设置图形字体到图标
static void setPixmap(QAbstractButton *btn, const QColor &color,
int icon, quint32 size = 12,
quint32 width = 15, quint32 height = 15,
int flags = Qt::AlignCenter);
//获取指定图形字体,可以指定文字大小,图片宽高,文字对齐
static QPixmap getPixmap(const QColor &color, int icon, quint32 size = 12,
quint32 width = 15, quint32 height = 15,
int flags = Qt::AlignCenter);
//指定导航面板样式,带图标和效果切换+悬停颜色+按下颜色+选中颜色
static void setStyle(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const StyleColor &styleColor);
static void setStyle(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const StyleColor &styleColor);
static void setStyle(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const StyleColor &styleColor);
//默认构造函数,传入字体文件+字体名称
explicit IconHelper(const QString &fontFile, const QString &fontName, QObject *parent = 0);
protected:
bool eventFilter(QObject *watched, QEvent *event);
private:
QFont iconFont; //图形字体
QList<QAbstractButton *> btns; //按钮队列
QList<QPixmap> pixNormal; //正常图片队列
QList<QPixmap> pixHover; //悬停图片队列
QList<QPixmap> pixPressed; //按下图片队列
QList<QPixmap> pixChecked; //选中图片队列
private slots:
//按钮选中状态切换处理
void toggled(bool checked);
public:
//获取图形字体
QFont getIconFont();
//设置图形字体到标签
void setIcon1(QLabel *lab, int icon, quint32 size = 12);
//设置图形字体到按钮
void setIcon1(QAbstractButton *btn, int icon, quint32 size = 12);
//设置图形字体到图标
void setPixmap1(QAbstractButton *btn, const QColor &color,
int icon, quint32 size = 12,
quint32 width = 15, quint32 height = 15,
int flags = Qt::AlignCenter);
//获取指定图形字体,可以指定文字大小,图片宽高,文字对齐
QPixmap getPixmap1(const QColor &color, int icon, quint32 size = 12,
quint32 width = 15, quint32 height = 15,
int flags = Qt::AlignCenter);
//指定导航面板样式,带图标和效果切换+悬停颜色+按下颜色+选中颜色
void setStyle1(QWidget *widget, QList<QPushButton *> btns, QList<int> icons, const StyleColor &styleColor);
void setStyle1(QWidget *widget, QList<QToolButton *> btns, QList<int> icons, const StyleColor &styleColor);
void setStyle1(QWidget *widget, QList<QAbstractButton *> btns, QList<int> icons, const StyleColor &styleColor);
};
#endif // ICONHELPER_H