自定义Qt按钮 收藏
Qt自带的PushButton样式比较单一,在开发的时候往往按钮的形状各异,所以需要自定义Qt的按钮。其方法是做一张图片来作为按钮,如果需要动态效果的话,可以做两张图片进行替换。按钮的载体可以是QLabel、QPushButton,可以通过QStyle类来设计样式,如果对QStyle不太了解的话,可以用下面的方法来实现。
1. 使用QPushButton
通过自定义一个按钮样式函数,在该函数中设置按钮的样式。(可以设计一个QPushButton的子类来完成设置)
实现代码:
view plaincopy to clipboardprint?
QPushButton *custButton(QString str,QString str1)
{
QPushButton *pushButton= new QPushButton;
pushButton->setGeometry(10,10,200,200); //按钮的位置及大小
pushButton->clearMask();
pushButton->setBackgroundRole( QPalette::Base);
QPixmap mypixmap; mypixmap.load(str);
pushButton->setFixedSize( mypixmap.width(), mypixmap.height() );
pushButton->setMask(mypixmap.createHeuristicMask());
pushButton->setIcon(mypixmap);
pushButton->setIconSize(QSize(myp