- #include <QTextStream>
- #include <QFile>
- QFile file("./styles/default.qss");
- file.open(QFile::ReadOnly);
- QString styleSheet = file.readAll();//QLatin1String(file.readAll());
- a.setStyleSheet(styleSheet);
#ifndef DIALOG_H#define DIALOG_H #include <QDialog> #include <QGridLayout> #include <QLabel> #include <QPushButton> #include <QLineEdit> namespace Ui {class Dialog;} class Dialog : public QDialog{ Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); private: Ui::Dialog *ui; QGridLayout *layout1; QPushButton *btn1; QPushButton *btn2; QPushButton *btn3; QPushButton *btn4; QLineEdit *edit1;}; #endif // DIALOG_H #include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); this->setWindowFlags(this->windowFlags()&Qt::WindowMaximizeButtonHint&Qt::WindowMinimizeButtonHint); //为对话框添加上最大化和最小化按钮 layout=new QBoxLayout(this); layout1=new QGridLayout(this); btn1=new QPushButton(this); btn1->setStyleSheet("QPushButton{color:red;background:yellow}"); //设定前景颜色,就是字体颜色 // btn1->setStyleSheet("QPushButton{background:yellow}"); btn1->setText("Button1"); btn2=new QPushButton(this); btn2->setStyleSheet("QPushButton{color:red;background-color:rgb(200,155,100)}"); //使用rgb来设定背景颜色 btn2->setText("Button2"); btn3=new QPushButton(this); btn3->setStyleSheet("QPushButton{background-image:url (image/1.png);background-repeat: repeat-xy;background-position: center;background-attachment: fixed;background-attachment: fixed;background-attachment: fixed;;background-clip: padding}"); //设定按钮的背景图片,background-repeat能够设定背景图片的 重复规则,这里设定仅在xy方向都重复,因此图片会被重复一 次 //background-position用来设定图片的位置,是左(left)仍是右(right),仍是在中间(center),是上(top)仍是底部(bottom) //background-attachment用来这定背景图片是否卷动或者和窗口大小相匹配,默认是卷动的 btn3->setText("Button3"); btn4=new QPushButton(this); btn4->setStyleSheet("QPushButton{border: 3px solid red;border-radius:8px}"); //设定边框宽度以及颜色 //可使用border-top,border-right,border-bottom,border-left分别设定按钮的上下左右边框, //一样有border-left-color, border-left-style, border-left-width.等分别来设定他们的颜色,样式和宽度 //border-image用来设定边框的背景图片。 //border-radius用来设定边框的弧度。能够设定圆角的按钮 btn4->setText("Button4"); //字体设定 //font-family来设定字体所属家族, //font-size来设定字体大小 //font-style来设定字体样式 //font-weight来设定字体深浅 //height用来设定其高低 //selection-color用来设定选中时候的颜色 edit1=new QLineEdit(this); edit1->setStyleSheet("QLineEdit{font: bold italic large "Times New Roman"; font-size:25px;color:rgb(55,100,255); height:50px;border:4px solid rgb(155,200,33);border-radius:15px;selection-color:pink}"); //父窗口的设定 //icon-size来设定图片大小 this->setWindowIcon(QIcon("image/1.png")); this->setStyleSheet("QWidget{background:write url(image/2.png);icon-size:20px 5px}"); //设定整个对话框的背景颜色 // this->setStyleSheet("QWidget{icon-size:20px 5px}"); layout1->addWidget(btn1,0,0); layout1->addWidget(btn2,0,1); l ayout1->addWidget(btn3,1,0); layout1->addWidget(btn4,1,1); layout1->addWidget(edit1,2,0); setLayout(layout1);}Dialog::~Dialog(){ delete ui;}
------------------------------QPushBUtton美化
第一篇
http://blog.csdn.net/yiyaaixuexi/article/details/6706729
第二篇
http://blog.csdn.net/yiyaaixuexi/article/details/6343337
有不少朋友都抱怨,为何本身使Qt作的小项目,UI那么土那么俗,方方框框,基本控件很丑,要不是Qt的跨平台,才不去学习它。呵呵,其实我想说,嵌入式系统中的图形界面,统统交给QT,绝对没问题!
简单说说自定义Button,QPushButton是经常使用组件之一,先看下效果。
当单击按钮后,效果如图:
实现代码:
这下明白喽?咱们只是须要在mousePressEvent和mouseReleaseEvent中,添加setIcon(QIcon(*buttonPicture))的处理,就让Button动起来了o(∩_∩)o ...


