改进代码

This commit is contained in:
feiyangqingyun
2021-09-20 12:46:24 +08:00
parent e1065dd361
commit c1137a20ee
35 changed files with 1220 additions and 693 deletions

View File

@@ -10,6 +10,13 @@ frmNavButton::frmNavButton(QWidget *parent) : QWidget(parent), ui(new Ui::frmNav
{
ui->setupUi(this);
this->initForm();
this->initBtn1();
this->initBtn2();
this->initBtn3();
this->initBtn4();
this->initBtn5();
this->initBtn6();
this->initBtn7();
}
frmNavButton::~frmNavButton()
@@ -18,6 +25,22 @@ frmNavButton::~frmNavButton()
}
void frmNavButton::initForm()
{
icons << 0xf17b << 0xf002 << 0xf013 << 0xf021 << 0xf0e0 << 0xf135;
ui->navButton11->setChecked(true);
ui->navButton23->setChecked(true);
ui->navButton31->setChecked(true);
ui->navButton44->setChecked(true);
ui->navButton53->setChecked(true);
ui->navButton61->setChecked(true);
ui->navButton75->setChecked(true);
//设置整体圆角
ui->widgetNav5->setStyleSheet(".QWidget{background:#292929;border:1px solid #292929;border-radius:20px;}");
}
void frmNavButton::initBtn1()
{
quint32 size = 15;
quint32 pixWidth = 15;
@@ -44,13 +67,14 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick1()));
}
}
size = 15;
pixWidth = 20;
pixHeight = 20;
void frmNavButton::initBtn2()
{
quint32 size = 15;
quint32 pixWidth = 20;
quint32 pixHeight = 20;
QList<int> icons;
icons << 0xf17b << 0xf002 << 0xf013 << 0xf021 << 0xf0e0 << 0xf135;
QColor normalBgColor = QColor("#2D9191");
QColor hoverBgColor = QColor("#187294");
QColor checkBgColor = QColor("#145C75");
@@ -90,13 +114,20 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick2()));
}
}
normalBgColor = QColor("#292F38");
hoverBgColor = QColor("#1D2025");
checkBgColor = QColor("#1D2025");
normalTextColor = QColor("#54626F");
hoverTextColor = QColor("#FDFDFD");
checkTextColor = QColor("#FDFDFD");
void frmNavButton::initBtn3()
{
quint32 size = 15;
quint32 pixWidth = 20;
quint32 pixHeight = 20;
QColor normalBgColor = QColor("#292F38");
QColor hoverBgColor = QColor("#1D2025");
QColor checkBgColor = QColor("#1D2025");
QColor normalTextColor = QColor("#54626F");
QColor hoverTextColor = QColor("#FDFDFD");
QColor checkTextColor = QColor("#FDFDFD");
btns3 << ui->navButton31 << ui->navButton32 << ui->navButton33 << ui->navButton34;
for (int i = 0; i < btns3.count(); i++) {
@@ -132,15 +163,18 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick3()));
}
}
size = 15;
pixWidth = 15;
pixHeight = 15;
void frmNavButton::initBtn4()
{
quint32 size = 15;
quint32 pixWidth = 15;
quint32 pixHeight = 15;
icon = 0xf105;
iconNormal = IconHelper::getPixmap(QColor(100, 100, 100).name(), icon, size, pixWidth, pixHeight);
iconHover = IconHelper::getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
iconCheck = IconHelper::getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
int icon = 0xf105;
QPixmap iconNormal = IconHelper::getPixmap(QColor(100, 100, 100).name(), icon, size, pixWidth, pixHeight);
QPixmap iconHover = IconHelper::getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
QPixmap iconCheck = IconHelper::getPixmap(QColor(255, 255, 255).name(), icon, size, pixWidth, pixHeight);
btns4 << ui->navButton41 << ui->navButton42 << ui->navButton43 << ui->navButton44;
for (int i = 0; i < btns4.count(); i++) {
@@ -154,7 +188,7 @@ void frmNavButton::initForm()
btn->setLinePosition(NavButton::LinePosition_Right);
btn->setShowIcon(true);
btn->setIconSpace(25);
btn->setIconSpace(10);
btn->setIconSize(QSize(15, 15));
btn->setIconNormal(iconNormal);
btn->setIconHover(iconHover);
@@ -162,21 +196,24 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick4()));
}
}
size = 15;
pixWidth = 20;
pixHeight = 20;
void frmNavButton::initBtn5()
{
QFont font;
font.setPixelSize(15);
font.setBold(true);
normalBgColor = QColor("#292929");
hoverBgColor = QColor("#064077");
checkBgColor = QColor("#10689A");
normalTextColor = QColor("#FFFFFF");
hoverTextColor = Qt::yellow;
checkTextColor = QColor("#FFFFFF");
quint32 size = 15;
quint32 pixWidth = 20;
quint32 pixHeight = 20;
QColor normalBgColor = QColor("#292929");
QColor hoverBgColor = QColor("#064077");
QColor checkBgColor = QColor("#10689A");
QColor normalTextColor = QColor("#FFFFFF");
QColor hoverTextColor = Qt::yellow;
QColor checkTextColor = QColor("#FFFFFF");
btns5 << ui->navButton51 << ui->navButton52 << ui->navButton53 << ui->navButton54 << ui->navButton55;
for (int i = 0; i < btns5.count(); i++) {
@@ -210,13 +247,24 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick5()));
}
}
normalBgColor = QColor("#E6393D");
hoverBgColor = QColor("#EE0000");
checkBgColor = QColor("#A40001");
normalTextColor = QColor("#FFFFFF");
hoverTextColor = QColor("#FFFFFF");
checkTextColor = QColor("#FFFFFF");
void frmNavButton::initBtn6()
{
QFont font;
font.setPixelSize(15);
font.setBold(true);
quint32 size = 15;
quint32 pixWidth = 20;
quint32 pixHeight = 20;
QColor normalBgColor = QColor("#E6393D");
QColor hoverBgColor = QColor("#EE0000");
QColor checkBgColor = QColor("#A40001");
QColor normalTextColor = QColor("#FFFFFF");
QColor hoverTextColor = QColor("#FFFFFF");
QColor checkTextColor = QColor("#FFFFFF");
btns6 << ui->navButton61 << ui->navButton62 << ui->navButton63 << ui->navButton64 << ui->navButton65;
for (int i = 0; i < btns6.count(); i++) {
@@ -250,14 +298,25 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick6()));
}
}
void frmNavButton::initBtn7()
{
QFont font;
font.setPixelSize(15);
font.setBold(true);
QColor normalTextColor = QColor("#FFFFFF");
QColor hoverTextColor = QColor("#FFFFFF");
QColor checkTextColor = QColor("#FFFFFF");
//设置背景色为画刷
QLinearGradient normalBgBrush(0, 0, 0, ui->navButton61->height());
QLinearGradient normalBgBrush(0, 0, 0, ui->navButton71->height());
normalBgBrush.setColorAt(0.0, QColor("#3985BF"));
normalBgBrush.setColorAt(0.5, QColor("#2972A9"));
normalBgBrush.setColorAt(1.0, QColor("#1C6496"));
QLinearGradient hoverBgBrush(0, 0, 0, ui->navButton61->height());
QLinearGradient hoverBgBrush(0, 0, 0, ui->navButton71->height());
hoverBgBrush.setColorAt(0.0, QColor("#4897D1"));
hoverBgBrush.setColorAt(0.5, QColor("#3283BC"));
hoverBgBrush.setColorAt(1.0, QColor("#3088C3"));
@@ -283,17 +342,6 @@ void frmNavButton::initForm()
connect(btn, SIGNAL(clicked(bool)), this, SLOT(buttonClick7()));
}
ui->navButton11->setChecked(true);
ui->navButton23->setChecked(true);
ui->navButton31->setChecked(true);
ui->navButton44->setChecked(true);
ui->navButton53->setChecked(true);
ui->navButton61->setChecked(true);
ui->navButton75->setChecked(true);
//设置整体圆角
ui->widgetNav5->setStyleSheet(".QWidget{background:#292929;border:1px solid #292929;border-radius:20px;}");
}
void frmNavButton::buttonClick1()

View File

@@ -19,6 +19,7 @@ public:
private:
Ui::frmNavButton *ui;
QList<int> icons;
QList<NavButton *> btns1;
QList<NavButton *> btns2;
QList<NavButton *> btns3;
@@ -29,6 +30,14 @@ private:
private slots:
void initForm();
void initBtn1();
void initBtn2();
void initBtn3();
void initBtn4();
void initBtn5();
void initBtn6();
void initBtn7();
void buttonClick1();
void buttonClick2();
void buttonClick3();

File diff suppressed because it is too large Load Diff

View File

@@ -2,6 +2,9 @@
IconHelper *IconHelper::iconFontAliBaBa = 0;
IconHelper *IconHelper::iconFontAwesome = 0;
IconHelper *IconHelper::iconFontWeather = 0;
int IconHelper::iconFontIndex = -1;
void IconHelper::initFont()
{
static bool isInit = false;
@@ -13,103 +16,101 @@ void IconHelper::initFont()
if (iconFontAwesome == 0) {
iconFontAwesome = new IconHelper(":/image/fontawesome-webfont.ttf", "FontAwesome");
}
if (iconFontWeather == 0) {
iconFontWeather = new IconHelper(":/image/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)
{
initFont();
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
if (icon > 0xe000 && icon < 0xf000) {
iconFontAliBaBa->setIcon1(lab, icon, size);
} else if (icon > 0xf000) {
iconFontAwesome->setIcon1(lab, icon, size);
}
getIconHelper(icon)->setIcon1(lab, icon, size);
}
void IconHelper::setIcon(QAbstractButton *btn, int icon, quint32 size)
{
initFont();
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
if (icon > 0xe000 && icon < 0xf000) {
iconFontAliBaBa->setIcon1(btn, icon, size);
} else if (icon > 0xf000) {
iconFontAwesome->setIcon1(btn, icon, 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)
{
initFont();
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
if (icon > 0xe000 && icon < 0xf000) {
iconFontAliBaBa->setPixmap1(btn, color, icon, size, width, height, flags);
} else if (icon > 0xf000) {
iconFontAwesome->setPixmap1(btn, color, icon, size, width, height, 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)
{
initFont();
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
QPixmap pix;
if (icon > 0xe000 && icon < 0xf000) {
pix = iconFontAliBaBa->getPixmap1(color, icon, size, width, height, flags);
} else if (icon > 0xf000) {
pix = iconFontAwesome->getPixmap1(color, icon, size, width, height, flags);
}
return pix;
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)
{
initFont();
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
int icon = icons.first();
if (icon > 0xe000 && icon < 0xf000) {
iconFontAliBaBa->setStyle1(widget, btns, icons, styleColor);
} else if (icon > 0xf000) {
iconFontAwesome->setStyle1(widget, btns, icons, styleColor);
}
getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
}
void IconHelper::setStyle(QWidget *widget, QList<QToolButton *> btns,
QList<int> icons, const IconHelper::StyleColor &styleColor)
{
initFont();
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
int icon = icons.first();
if (icon > 0xe000 && icon < 0xf000) {
iconFontAliBaBa->setStyle1(widget, btns, icons, styleColor);
} else if (icon > 0xf000) {
iconFontAwesome->setStyle1(widget, btns, icons, styleColor);
}
getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
}
void IconHelper::setStyle(QWidget *widget, QList<QAbstractButton *> btns,
QList<int> icons, const IconHelper::StyleColor &styleColor)
{
initFont();
//自动根据不同的字体的值选择对应的类,fontawesome 0xf开头 iconfont 0xe开头
int icon = icons.first();
if (icon > 0xe000 && icon < 0xf000) {
iconFontAliBaBa->setStyle1(widget, btns, icons, styleColor);
} else if (icon > 0xf000) {
iconFontAwesome->setStyle1(widget, btns, icons, styleColor);
}
getIconHelper(icon)->setStyle1(widget, btns, icons, styleColor);
}
IconHelper::IconHelper(const QString &fontFile, const QString &fontName, QObject *parent) : QObject(parent)
{
//判断图形字体是否存在,不存在则加入
@@ -122,6 +123,7 @@ IconHelper::IconHelper(const QString &fontFile, const QString &fontName, QObject
}
}
//再次判断是否包含字体名称防止加载失败
if (fontDb.families().contains(fontName)) {
iconFont = QFont(fontName);
#if (QT_VERSION >= QT_VERSION_CHECK(4,8,0))
@@ -179,6 +181,11 @@ void IconHelper::toggled(bool checked)
}
}
QFont IconHelper::getIconFont()
{
return this->iconFont;
}
void IconHelper::setIcon1(QLabel *lab, int icon, quint32 size)
{
iconFont.setPixelSize(size);
@@ -269,8 +276,9 @@ void IconHelper::setStyle1(QWidget *widget, QList<QAbstractButton *> btns, QList
}
//如果图标是左侧显示则需要让没有选中的按钮左侧也有加深的边框,颜色为背景颜色
//如果图标在文字上面而设置的边框是 top bottom 也需要启用加深边框
QStringList qss;
if (styleColor.textBesideIcon) {
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 {

View File

@@ -8,11 +8,18 @@
/**
* 超级图形字体类 作者:feiyangqingyun(QQ:517216493) 2016-11-23
* 1. 可传入多种图形字体文件。
* 2. 可设置 QLabel+QAbstractButton 图形字体
* 3. 可设置按钮图标
* 4. 可获取指定尺寸的图形字体图片
* 5. 超级导航栏样式设置,带图标和效果切换、悬停颜色、按下颜色、选中颜色
* 1. 可传入多种图形字体文件,一个类通用所有图形字体
* 2. 默认已经内置了阿里巴巴图形字体FontAliBaBa、国际知名图形字体FontAwesome、天气图形字体FontWeather
* 3. 可设置 QLabel、QAbstractButton 文本为图形字体
* 4. 可设置图形字体作为 QAbstractButton 按钮图标
* 5. 内置万能的方法 getPixmap 将图形字体值转换为图片
* 6. 无论是设置文本、图标、图片等都可以设置图标的大小、尺寸、颜色等参数。
* 7. 内置超级导航栏样式设置,将图形字体作为图标设置到按钮。
* 8. 支持各种颜色设置比如正常颜色、悬停颜色、按下颜色、选中颜色。
* 9. 可设置导航的位置为 left、right、top、bottom 四种。
* 10. 可设置导航加深边框颜色和粗细大小。
* 11. 导航面板的各种切换效果比如鼠标悬停、按下、选中等都自动处理掉样式设置。
* 12. 全局静态方法,接口丰富,使用极其简单方便。
*/
#ifdef quc
@@ -28,7 +35,7 @@ public:
//样式颜色结构体
struct StyleColor {
QString position; //位置 left right top bottom
bool textBesideIcon; //文字在图标左侧
bool defaultBorder; //默认有边框
quint32 iconSize; //图标字体尺寸
quint32 iconWidth; //图标图片宽度
@@ -48,7 +55,7 @@ public:
StyleColor() {
position = "left";
textBesideIcon = false;
defaultBorder = false;
iconSize = 12;
iconWidth = 15;
@@ -87,20 +94,38 @@ public:
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);
@@ -124,6 +149,9 @@ private slots:
void toggled(bool checked);
public:
//获取图形字体
QFont getIconFont();
//设置图形字体到标签
void setIcon1(QLabel *lab, int icon, quint32 size = 12);
//设置图形字体到按钮