Qt5--按钮风格生成器

来源:互联网 发布:无人机遥控软件 编辑:程序博客网 时间:2024/06/02 02:06

 找到一个按钮风格自动生成的软件,用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 <QMainWindow>namespace Ui {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>int main(int argc, char *argv[]){    QApplication a(argc, argv);    MainWindow w;    w.show();    return a.exec();}

mainwindow.cpp

#include "mainwindow.h"#include "ui_mainwindow.h"#include <QColorDialog>MainWindow::MainWindow(QWidget *parent) :    QMainWindow(parent),    ui(new Ui::MainWindow){    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=col;    while(rightColor.size()<6)        rightColor.prepend("0");    return rightColor;}void MainWindow::on_renkButton_clicked(){    QColorDialog diag;    QColor bColor=diag.getColor(QColor(bgGrad_stop0));    if(bColor.isValid())    {        ui->renkLabel->setPalette(bColor);        ui->renkLabel->setAutoFillBackground(true);        color=bColor.name();        updateStylesheet();    }}void MainWindow::on_renkButton2_clicked(){    QColorDialog diag;    QColor bColor=diag.getColor(QColor(bgGrad_stop11));    if(bColor.isValid())    {        ui->renkLabel2->setPalette(bColor);        ui->renkLabel2->setAutoFillBackground(true);        solid=bColor.name();        updateStylesheet();    }}void MainWindow::on_grenkButton1_clicked(){    QColorDialog diag;    QColor bColor=diag.getColor(QColor(bgGrad_stop0));    if(bColor.isValid())    {        ui->grenkLabel1->setPalette(bColor);        ui->grenkLabel1->setAutoFillBackground(true);        bgGrad_stop0=bColor.name();        updateStylesheet();    }}void MainWindow::on_grenkButton2_clicked(){    QColorDialog diag;    QColor bColor=diag.getColor(QColor(bgGrad_stop11));    if(bColor.isValid())    {        ui->grenkLabel2->setPalette(bColor);        ui->grenkLabel2->setAutoFillBackground(true);        bgGrad_stop11=bColor.name();        int h,s,v;        bColor.getHsv(&h,&s,&v);        s= s/2;        bColor.setHsv(h,s,v);        ui->grenkLabel3->setPalette(bColor);        ui->grenkLabel3->setAutoFillBackground(true);        bgGrad_stop12=bColor.name();        bColor.getHsv(&h,&s,&v);        s= s/2;        bColor.setHsv(h,s,v);        ui->grenkLabel4->setPalette(bColor);        ui->grenkLabel4->setAutoFillBackground(true);        bgGrad_stop13=bColor.name();        updateStylesheet();    }}void MainWindow::on_grenkButton3_clicked(){    QColorDialog diag;    QColor bColor=diag.getColor(QColor(bgGrad_stop12));    if(bColor.isValid())    {        ui->grenkLabel3->setPalette(bColor);        ui->grenkLabel3->setAutoFillBackground(true);        bgGrad_stop12=bColor.name();        updateStylesheet();    }}void MainWindow::on_grenkButton4_clicked(){    QColorDialog diag;    QColor bColor=diag.getColor(QColor(bgGrad_stop13));    if(bColor.isValid())    {        ui->grenkLabel4->setPalette(bColor);        ui->grenkLabel4->setAutoFillBackground(true);        bgGrad_stop13=bColor.name();        updateStylesheet();    }}void MainWindow::on_bordRadSlider_valueChanged(int value){    borderRadius= value;    updateStylesheet();}void MainWindow::on_bordThickSlider_valueChanged(int value){    border=value;    updateStylesheet();}void MainWindow::on_gRadiusSlider_valueChanged(int value){    bgGrad_radius=value / 100.0;    updateStylesheet();}void MainWindow::on_fontComboBox_currentIndexChanged(const QString &arg1){    fontFamily=ui->fontComboBox->currentFont().family();    updateStylesheet();}void MainWindow::on_fontSizeSlider_valueChanged(int value){    fontSize=value;    updateStylesheet();}

ui文件

<?xml version="1.0" encoding="UTF-8"?><ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow">  <property name="geometry">   <rect>    <x>0</x>    <y>0</y>    <width>715</width>    <height>582</height>   </rect>  </property>  <property name="windowTitle">   <string>MainWindow</string>  </property>  <widget class="QWidget" name="centralWidget">   <widget class="QGroupBox" name="groupBox_4">    <property name="geometry">     <rect>      <x>9</x>      <y>14</y>      <width>381</width>      <height>141</height>     </rect>    </property>    <property name="title">     <string>边缘设置</string>    </property>    <layout class="QGridLayout" name="gridLayout_5">     <item row="0" column="0">      <widget class="QLabel" name="label">       <property name="text">        <string>圆角弧度</string>       </property>      </widget>     </item>     <item row="0" column="1">      <widget class="QSlider" name="bordRadSlider">       <property name="maximum">        <number>30</number>       </property>       <property name="orientation">        <enum>Qt::Horizontal</enum>       </property>      </widget>     </item>     <item row="1" column="0">      <widget class="QLabel" name="label_2">       <property name="text">        <string>边缘厚度</string>       </property>      </widget>     </item>     <item row="1" column="1">      <widget class="QSlider" name="bordThickSlider">       <property name="maximum">        <number>15</number>       </property>       <property name="orientation">        <enum>Qt::Horizontal</enum>       </property>      </widget>     </item>     <item row="2" column="0">      <widget class="QPushButton" name="renkButton2">       <property name="text">        <string>边缘颜色</string>       </property>      </widget>     </item>     <item row="2" column="1">      <widget class="QLabel" name="renkLabel2">       <property name="text">        <string/>       </property>      </widget>     </item>    </layout>   </widget>   <widget class="QGroupBox" name="groupBox_5">    <property name="geometry">     <rect>      <x>20</x>      <y>170</y>      <width>370</width>      <height>111</height>     </rect>    </property>    <property name="minimumSize">     <size>      <width>0</width>      <height>50</height>     </size>    </property>    <property name="title">     <string>字体设置</string>    </property>    <layout class="QGridLayout" name="gridLayout_6">     <item row="0" column="0">      <widget class="QLabel" name="label_5">       <property name="text">        <string>Family</string>       </property>      </widget>     </item>     <item row="1" column="0">      <widget class="QLabel" name="label_4">       <property name="text">        <string>字体大小</string>       </property>      </widget>     </item>     <item row="1" column="1">      <widget class="QSlider" name="fontSizeSlider">       <property name="minimum">        <number>1</number>       </property>       <property name="maximum">        <number>30</number>       </property>       <property name="orientation">        <enum>Qt::Horizontal</enum>       </property>      </widget>     </item>     <item row="0" column="1">      <widget class="QFontComboBox" name="fontComboBox"/>     </item>     <item row="2" column="0">      <widget class="QPushButton" name="renkButton">       <property name="text">        <string>字体颜色</string>       </property>      </widget>     </item>     <item row="2" column="1">      <widget class="QLabel" name="renkLabel">       <property name="text">        <string/>       </property>      </widget>     </item>    </layout>   </widget>   <widget class="QGroupBox" name="groupBox_2">    <property name="geometry">     <rect>      <x>20</x>      <y>288</y>      <width>371</width>      <height>251</height>     </rect>    </property>    <property name="title">     <string>颜色渐变设置</string>    </property>    <layout class="QGridLayout" name="gridLayout_2">     <item row="1" column="0">      <widget class="QPushButton" name="grenkButton1">       <property name="text">        <string>G.Color1</string>       </property>      </widget>     </item>     <item row="1" column="1">      <widget class="QLabel" name="grenkLabel1">       <property name="text">        <string/>       </property>      </widget>     </item>     <item row="2" column="0">      <widget class="QPushButton" name="grenkButton2">       <property name="text">        <string>G.Color2</string>       </property>      </widget>     </item>     <item row="2" column="1">      <widget class="QLabel" name="grenkLabel2">       <property name="text">        <string/>       </property>      </widget>     </item>     <item row="3" column="0">      <widget class="QPushButton" name="grenkButton3">       <property name="text">        <string>G.Color3</string>       </property>      </widget>     </item>     <item row="3" column="1">      <widget class="QLabel" name="grenkLabel3">       <property name="text">        <string/>       </property>      </widget>     </item>     <item row="4" column="0">      <widget class="QPushButton" name="grenkButton4">       <property name="text">        <string>G.Color4</string>       </property>      </widget>     </item>     <item row="4" column="1">      <widget class="QLabel" name="grenkLabel4">       <property name="text">        <string/>       </property>      </widget>     </item>     <item row="5" column="0">      <widget class="QLabel" name="label_3">       <property name="text">        <string>G.Radius</string>       </property>      </widget>     </item>     <item row="5" column="1">      <widget class="QSlider" name="gRadiusSlider">       <property name="maximum">        <number>300</number>       </property>       <property name="orientation">        <enum>Qt::Horizontal</enum>       </property>      </widget>     </item>     <item row="0" column="1">      <widget class="QComboBox" name="comboBox">       <item>        <property name="text">         <string>Radial Gradyan</string>        </property>       </item>       <item>        <property name="text">         <string>Lineer Gradyan</string>        </property>       </item>      </widget>     </item>     <item row="0" column="0">      <widget class="QLabel" name="label_6">       <property name="text">        <string>G. Type</string>       </property>      </widget>     </item>    </layout>   </widget>   <widget class="QGroupBox" name="groupBox">    <property name="geometry">     <rect>      <x>400</x>      <y>14</y>      <width>291</width>      <height>141</height>     </rect>    </property>    <property name="title">     <string>按钮预览</string>    </property>    <layout class="QGridLayout" name="gridLayout_3">     <item row="0" column="1">      <widget class="QPushButton" name="pushButton">       <property name="styleSheet">        <string notr="true"/>       </property>       <property name="text">        <string>PushButton</string>       </property>      </widget>     </item>     <item row="0" column="0">      <spacer name="horizontalSpacer">       <property name="orientation">        <enum>Qt::Horizontal</enum>       </property>       <property name="sizeHint" stdset="0">        <size>         <width>40</width>         <height>20</height>        </size>       </property>      </spacer>     </item>     <item row="0" column="2">      <spacer name="horizontalSpacer_2">       <property name="orientation">        <enum>Qt::Horizontal</enum>       </property>       <property name="sizeHint" stdset="0">        <size>         <width>40</width>         <height>20</height>        </size>       </property>      </spacer>     </item>    </layout>   </widget>   <widget class="QTextEdit" name="styleOutput">    <property name="geometry">     <rect>      <x>410</x>      <y>180</y>      <width>279</width>      <height>361</height>     </rect>    </property>   </widget>  </widget>  <widget class="QStatusBar" name="statusBar"/> </widget> <layoutdefault spacing="6" margin="11"/> <resources/> <connections/></ui>

这里写图片描述

这里写图片描述

原创粉丝点击