找到一个按钮风格自动生成的软件,用Qt5写的。开源,大家研究学习一下。最后有源码Qt5.6编译。
参考博客:http://www.cnblogs.com/newstart/p/4337873.html
一个顶好的网站,上面有很多Qt的源码工程:https://www.linux-apps.com/content/show.php/AnalogWidgets?content=87780
mainwindow.h
/****************************************************************************
**
** Copyright (C) 2013 Yigit Agabeyli.
**
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 as published by the Free Software Foundation
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include
class MainWindow;
}class MainWindow : public QMainWindow
{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = 0);~MainWindow();private slots:void on_renkButton_clicked();void on_renkButton2_clicked();void on_grenkButton1_clicked();void on_grenkButton2_clicked();void on_grenkButton3_clicked();void on_grenkButton4_clicked();void on_bordRadSlider_valueChanged(int value);void on_bordThickSlider_valueChanged(int value);void on_gRadiusSlider_valueChanged(int value);void on_fontComboBox_currentIndexChanged(const QString &arg1);void on_fontSizeSlider_valueChanged(int value);private:Ui::MainWindow *ui;QString styleText;QString color;int border;QString solid;int borderRadius;int padding;double bgGrad_x1;double bgGrad_x2;double bgGrad_y1;double bgGrad_y2;double bgGrad_cx;double bgGrad_cy;double bgGrad_fx;double bgGrad_fy;double bgGrad2_cx;double bgGrad2_cy;double bgGrad2_fx;double bgGrad2_fy;float bgGrad_radius;QString bgGrad_stop0;QString bgGrad_stop11;QString bgGrad_stop12;QString bgGrad_stop13;QString fontFamily;QString gradyanType;QString gradyanString;int fontSize;void updateStylesheet();QString prependZeros(QString);
};#endif // MAINWINDOW_H
main.cpp
#include "mainwindow.h"
#include
{QApplication a(argc, argv);MainWindow w;w.show();return a.exec();
}
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include
{ui->setupUi(this);color="#333";border=2;ui->bordThickSlider->setValue(border);solid="#555";ui->renkLabel->setPalette(QPalette(QColor(color)));ui->renkLabel->setAutoFillBackground(true);ui->renkLabel2->setPalette(QPalette(QColor(solid)));ui->renkLabel2->setAutoFillBackground(true);fontFamily="MS Shell Dlg 2";ui->fontComboBox->setCurrentFont(QFont(fontFamily));fontSize=8;ui->fontSizeSlider->setValue(fontSize);borderRadius=11;ui->bordRadSlider->setValue(borderRadius);padding=5;gradyanType="qradialgradient";bgGrad_cx=0.3;bgGrad_cy=-0.4;bgGrad_fx=0.3;bgGrad_fy=-0.4;bgGrad2_cx=0.4;bgGrad2_cy=-0.1;bgGrad2_fx=0.4;bgGrad2_fy=-0.1;bgGrad_radius=1.35;ui->gRadiusSlider->setValue(bgGrad_radius*100);bgGrad_stop0="#fff";bgGrad_stop11="#888";bgGrad_stop12="#bbb";bgGrad_stop13="#ddd";ui->grenkLabel1->setPalette(QPalette(QColor(bgGrad_stop0)));ui->grenkLabel1->setAutoFillBackground(true);ui->grenkLabel2->setPalette(QPalette(QColor(bgGrad_stop11)));ui->grenkLabel2->setAutoFillBackground(true);ui->grenkLabel3->setPalette(QPalette(QColor(bgGrad_stop12)));ui->grenkLabel3->setAutoFillBackground(true);ui->grenkLabel4->setPalette(QPalette(QColor(bgGrad_stop13)));ui->grenkLabel4->setAutoFillBackground(true);updateStylesheet();
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::updateStylesheet()
{styleText= QString(" QPushButton {\ncolor: "+color+";\nborder: "+QString::number(border)+"px solid "+solid+";\nfont: "+QString::number(fontSize)+"pt \""+fontFamily+"\""++";\nborder-radius: "+QString::number(borderRadius)+"px;\npadding: "+QString::number(padding)+"px;\nbackground: qradialgradient(cx: "+QString::number(bgGrad_cx)+", cy: "+QString::number(bgGrad_cy)+",\nfx: "+QString::number(bgGrad_fx)+", fy: "+QString::number(bgGrad_fy)+",\nradius: "+QString::number(bgGrad_radius)+", stop: 0 "+bgGrad_stop0+", stop: 1 "+bgGrad_stop11+");\nmin-width: 80px;\n}\n\n"+"QPushButton:hover {\nbackground: qradialgradient(cx: "+QString::number(bgGrad_cx)+", cy: "+QString::number(bgGrad_cy)+",\nfx: "+QString::number(bgGrad_fx)+", fy: "+QString::number(bgGrad_fy)+",\nradius: "+QString::number(bgGrad_radius)+", stop: 0 "+bgGrad_stop0+", stop: 1 "+bgGrad_stop12+");\n}\n\n "+"QPushButton:pressed {\nbackground: qradialgradient(cx: "+QString::number(bgGrad2_cx)+", cy: "+QString::number(bgGrad2_cy)+",\nfx: "+QString::number(bgGrad2_fx)+", fy: "+QString::number(bgGrad2_fy)+",\nradius: "+QString::number(bgGrad_radius)+", stop: 0 "+bgGrad_stop0+", stop: 1 "+bgGrad_stop13+");\n}");ui->pushButton->setStyleSheet(styleText);ui->styleOutput->setText(styleText);
}
QString MainWindow::prependZeros(QString col)
{QString rightColor&#61;col;while(rightColor.size()<6)rightColor.prepend("0");return rightColor;}
void MainWindow::on_renkButton_clicked()
{QColorDialog diag;QColor bColor&#61;diag.getColor(QColor(bgGrad_stop0));if(bColor.isValid()){ui->renkLabel->setPalette(bColor);ui->renkLabel->setAutoFillBackground(true);color&#61;bColor.name();updateStylesheet();}}
void MainWindow::on_renkButton2_clicked()
{QColorDialog diag;QColor bColor&#61;diag.getColor(QColor(bgGrad_stop11));if(bColor.isValid()){ui->renkLabel2->setPalette(bColor);ui->renkLabel2->setAutoFillBackground(true);solid&#61;bColor.name();updateStylesheet();}
}void MainWindow::on_grenkButton1_clicked()
{QColorDialog diag;QColor bColor&#61;diag.getColor(QColor(bgGrad_stop0));if(bColor.isValid()){ui->grenkLabel1->setPalette(bColor);ui->grenkLabel1->setAutoFillBackground(true);bgGrad_stop0&#61;bColor.name();updateStylesheet();}
}void MainWindow::on_grenkButton2_clicked()
{QColorDialog diag;QColor bColor&#61;diag.getColor(QColor(bgGrad_stop11));if(bColor.isValid()){ui->grenkLabel2->setPalette(bColor);ui->grenkLabel2->setAutoFillBackground(true);bgGrad_stop11&#61;bColor.name();int h,s,v;bColor.getHsv(&h,&s,&v);s&#61; s/2;bColor.setHsv(h,s,v);ui->grenkLabel3->setPalette(bColor);ui->grenkLabel3->setAutoFillBackground(true);bgGrad_stop12&#61;bColor.name();bColor.getHsv(&h,&s,&v);s&#61; s/2;bColor.setHsv(h,s,v);ui->grenkLabel4->setPalette(bColor);ui->grenkLabel4->setAutoFillBackground(true);bgGrad_stop13&#61;bColor.name();updateStylesheet();}}void MainWindow::on_grenkButton3_clicked()
{QColorDialog diag;QColor bColor&#61;diag.getColor(QColor(bgGrad_stop12));if(bColor.isValid()){ui->grenkLabel3->setPalette(bColor);ui->grenkLabel3->setAutoFillBackground(true);bgGrad_stop12&#61;bColor.name();updateStylesheet();}
}void MainWindow::on_grenkButton4_clicked()
{QColorDialog diag;QColor bColor&#61;diag.getColor(QColor(bgGrad_stop13));if(bColor.isValid()){ui->grenkLabel4->setPalette(bColor);ui->grenkLabel4->setAutoFillBackground(true);bgGrad_stop13&#61;bColor.name();updateStylesheet();}
}void MainWindow::on_bordRadSlider_valueChanged(int value)
{borderRadius&#61; value;updateStylesheet();
}void MainWindow::on_bordThickSlider_valueChanged(int value)
{border&#61;value;updateStylesheet();
}void MainWindow::on_gRadiusSlider_valueChanged(int value)
{bgGrad_radius&#61;value / 100.0;updateStylesheet();
}void MainWindow::on_fontComboBox_currentIndexChanged(const QString &arg1)
{fontFamily&#61;ui->fontComboBox->currentFont().family();updateStylesheet();
}void MainWindow::on_fontSizeSlider_valueChanged(int value)
{fontSize&#61;value;updateStylesheet();
}
ui文件
<ui version&#61;"4.0"><class>MainWindowclass><widget class&#61;"QMainWindow" name&#61;"MainWindow"><property name&#61;"geometry"><rect><x>0x><y>0y><width>715width><height>582height>rect>property><property name&#61;"windowTitle"><string>MainWindowstring>property><widget class&#61;"QWidget" name&#61;"centralWidget"><widget class&#61;"QGroupBox" name&#61;"groupBox_4"><property name&#61;"geometry"><rect><x>9x><y>14y><width>381width><height>141height>rect>property><property name&#61;"title"><string>边缘设置string>property><layout class&#61;"QGridLayout" name&#61;"gridLayout_5"><item row&#61;"0" column&#61;"0"><widget class&#61;"QLabel" name&#61;"label"><property name&#61;"text"><string>圆角弧度string>property>widget>item><item row&#61;"0" column&#61;"1"><widget class&#61;"QSlider" name&#61;"bordRadSlider"><property name&#61;"maximum"><number>30number>property><property name&#61;"orientation"><enum>Qt::Horizontalenum>property>widget>item><item row&#61;"1" column&#61;"0"><widget class&#61;"QLabel" name&#61;"label_2"><property name&#61;"text"><string>边缘厚度string>property>widget>item><item row&#61;"1" column&#61;"1"><widget class&#61;"QSlider" name&#61;"bordThickSlider"><property name&#61;"maximum"><number>15number>property><property name&#61;"orientation"><enum>Qt::Horizontalenum>property>widget>item><item row&#61;"2" column&#61;"0"><widget class&#61;"QPushButton" name&#61;"renkButton2"><property name&#61;"text"><string>边缘颜色string>property>widget>item><item row&#61;"2" column&#61;"1"><widget class&#61;"QLabel" name&#61;"renkLabel2"><property name&#61;"text"><string/>property>widget>item>layout>widget><widget class&#61;"QGroupBox" name&#61;"groupBox_5"><property name&#61;"geometry"><rect><x>20x><y>170y><width>370width><height>111height>rect>property><property name&#61;"minimumSize"><size><width>0width><height>50height>size>property><property name&#61;"title"><string>字体设置string>property><layout class&#61;"QGridLayout" name&#61;"gridLayout_6"><item row&#61;"0" column&#61;"0"><widget class&#61;"QLabel" name&#61;"label_5"><property name&#61;"text"><string>Familystring>property>widget>item><item row&#61;"1" column&#61;"0"><widget class&#61;"QLabel" name&#61;"label_4"><property name&#61;"text"><string>字体大小string>property>widget>item><item row&#61;"1" column&#61;"1"><widget class&#61;"QSlider" name&#61;"fontSizeSlider"><property name&#61;"minimum"><number>1number>property><property name&#61;"maximum"><number>30number>property><property name&#61;"orientation"><enum>Qt::Horizontalenum>property>widget>item><item row&#61;"0" column&#61;"1"><widget class&#61;"QFontComboBox" name&#61;"fontComboBox"/>item><item row&#61;"2" column&#61;"0"><widget class&#61;"QPushButton" name&#61;"renkButton"><property name&#61;"text"><string>字体颜色string>property>widget>item><item row&#61;"2" column&#61;"1"><widget class&#61;"QLabel" name&#61;"renkLabel"><property name&#61;"text"><string/>property>widget>item>layout>widget><widget class&#61;"QGroupBox" name&#61;"groupBox_2"><property name&#61;"geometry"><rect><x>20x><y>288y><width>371width><height>251height>rect>property><property name&#61;"title"><string>颜色渐变设置string>property><layout class&#61;"QGridLayout" name&#61;"gridLayout_2"><item row&#61;"1" column&#61;"0"><widget class&#61;"QPushButton" name&#61;"grenkButton1"><property name&#61;"text"><string>G.Color1string>property>widget>item><item row&#61;"1" column&#61;"1"><widget class&#61;"QLabel" name&#61;"grenkLabel1"><property name&#61;"text"><string/>property>widget>item><item row&#61;"2" column&#61;"0"><widget class&#61;"QPushButton" name&#61;"grenkButton2"><property name&#61;"text"><string>G.Color2string>property>widget>item><item row&#61;"2" column&#61;"1"><widget class&#61;"QLabel" name&#61;"grenkLabel2"><property name&#61;"text"><string/>property>widget>item><item row&#61;"3" column&#61;"0"><widget class&#61;"QPushButton" name&#61;"grenkButton3"><property name&#61;"text"><string>G.Color3string>property>widget>item><item row&#61;"3" column&#61;"1"><widget class&#61;"QLabel" name&#61;"grenkLabel3"><property name&#61;"text"><string/>property>widget>item><item row&#61;"4" column&#61;"0"><widget class&#61;"QPushButton" name&#61;"grenkButton4"><property name&#61;"text"><string>G.Color4string>property>widget>item><item row&#61;"4" column&#61;"1"><widget class&#61;"QLabel" name&#61;"grenkLabel4"><property name&#61;"text"><string/>property>widget>item><item row&#61;"5" column&#61;"0"><widget class&#61;"QLabel" name&#61;"label_3"><property name&#61;"text"><string>G.Radiusstring>property>widget>item><item row&#61;"5" column&#61;"1"><widget class&#61;"QSlider" name&#61;"gRadiusSlider"><property name&#61;"maximum"><number>300number>property><property name&#61;"orientation"><enum>Qt::Horizontalenum>property>widget>item><item row&#61;"0" column&#61;"1"><widget class&#61;"QComboBox" name&#61;"comboBox"><item><property name&#61;"text"><string>Radial Gradyanstring>property>item><item><property name&#61;"text"><string>Lineer Gradyanstring>property>item>widget>item><item row&#61;"0" column&#61;"0"><widget class&#61;"QLabel" name&#61;"label_6"><property name&#61;"text"><string>G. Typestring>property>widget>item>layout>widget><widget class&#61;"QGroupBox" name&#61;"groupBox"><property name&#61;"geometry"><rect><x>400x><y>14y><width>291width><height>141height>rect>property><property name&#61;"title"><string>按钮预览string>property><layout class&#61;"QGridLayout" name&#61;"gridLayout_3"><item row&#61;"0" column&#61;"1"><widget class&#61;"QPushButton" name&#61;"pushButton"><property name&#61;"styleSheet"><string notr&#61;"true"/>property><property name&#61;"text"><string>PushButtonstring>property>widget>item><item row&#61;"0" column&#61;"0"><spacer name&#61;"horizontalSpacer"><property name&#61;"orientation"><enum>Qt::Horizontalenum>property><property name&#61;"sizeHint" stdset&#61;"0"><size><width>40width><height>20height>size>property>spacer>item><item row&#61;"0" column&#61;"2"><spacer name&#61;"horizontalSpacer_2"><property name&#61;"orientation"><enum>Qt::Horizontalenum>property><property name&#61;"sizeHint" stdset&#61;"0"><size><width>40width><height>20height>size>property>spacer>item>layout>widget><widget class&#61;"QTextEdit" name&#61;"styleOutput"><property name&#61;"geometry"><rect><x>410x><y>180y><width>279width><height>361height>rect>property>widget>widget><widget class&#61;"QStatusBar" name&#61;"statusBar"/>widget><layoutdefault spacing&#61;"6" margin&#61;"11"/><resources/><connections/>
ui>