Qt circular button set

Qt stylesheet achieved by the radio button, which can be achieved rounded button, of course, also be used in other ways, for example, derived by using the drawing event button class, to draw a pattern, or a custom class It can also be achieved by mixing signals and slots mechanism and drawing event! Each method has its advantages, of course, has its own advantages, will be used herein in the destruction mechanism such as not to be of a circular button or a rounded rectangle UI drawing!

 

 

Core code:
#include "widget.h"
#include "ui_widget.h"
#include
// Depending on how the file is loaded, the qss file import!

:: the Widget the Widget (the QWidget * parent):
the QWidget (parent),
UI (new new Ui :: the Widget)
{
ui-> setupUi (the this);
this-> a resize (800,600); // set the size of the main window
button = new QPushButton ( "Love", this); // create button
button-> setGeometry ((this-> width () - 300) / 2, (this-> height () - 300) / 2,300,300);
// according window, go to installation position button
button_style (); // set the style of the buttons
}

void Widget::button_style()
{
QFile file(":/new/prefix1/button.qss"); //通过文件路径创建文件对象
file.open(QFile::ReadOnly); //文件打开方式
QString str = file.readAll(); //获取qss中全部字符
this->setStyleSheet(str); //设置样式表
}

Widget::~Widget()
{
delete ui;
}

样式表设置区*
QPushButton { //设置图1的样式表方式
font-size:100px; //文字大小
color:yellow; //前景色也就是字体颜色
border-radius:150px; //圆角大小,因为矩形的一半为150,所以圆角大小 设置为150以后,将会成为一个原型,小于150,就是圆角矩形了。
background-color:rgba(0,255,0,200); //设置按钮背景为绿色
}

QPushButton:hover { //设置图1的样式表方式:悬停状态
font-size:100px;
color:green;
border-radius:150px;
background-color:rgba(255,255,0,200); //设置按钮背景为黄色 = 红+绿
}
QPushButton:pressed { //设置图1的样式表方式:按下状态
color:orange;
border-width:3;
border-color:orange;
border-style:solid;;
background-color:cyan; //设置按钮背景为青色
}

也可以在 QPushButton 对象下 通过 setStyleSheet()函数直接设置样式表的,但是对于一个部件的多种状态,是有些 不适用的,文件加载这种方式还是蛮好的!
---------------------
设置圆形按钮 最关键的是geometery的宽度和高度要相等,而且通过样式表设置border-radius的值为高度和宽度的一般即可。

 ui->pushButton->setStyleSheet("QPushButton{font-size:100px;color:yellow;border-radius:15px;}");

调色板主要用来也可以用来更改button的颜色

 QPalette pal =ui->pushButton->palette();//调色板
    pal.setColor(QPalette::Button,Qt::red);
    ui->pushButton->setPalette(pal);
    ui->pushButton->setAutoFillBackground(true);
    ui->pushButton->setFlat(true);
    ui->pushButton->setText("");

Guess you like

Origin www.cnblogs.com/tsh292278/p/11275819.html