我有一個QPushButton
與CSS設計。 我希望他在點擊時改變尺寸。我用QPropertyAnimation(mybutton,"geometry")
來達到這個目標。 然而,尺寸政策是固定的。要調整這個因子,我想使用QPushButton的font屬性。調整字體大小 - 字體屬性
class MyButton : public QPushButton
{
Q_OBJECT
public:
explicit MyButton(QWidget *parent = Q_NULLPTR);
~MyButton();
};
而且我.ccp
:
MyButton::MyButton(QWidget *parent) : QPushButton(parent)
{
this->setGeometry(150,20,340,50);
this->setStyleSheet("border-radius: 25; background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #eeeeee, stop: 1 #5F6060);");
this->setText("Menu");
this->setFont(QFont("Colibri",25));
this->setCursor(Qt::PointingHandCursor);
QPalette pal;
pal.setColor(QPalette::ButtonText,Qt::white);
this->setPalette(pal);
}
我嘗試使用QPropertyAnimation
如下動畫:
animationBoutonMenuText = new QPropertyAnimation(myButton,"font");
animationBoutonMenuText->setDuration(300);
animationBoutonMenuText->setKeyValueAt(0,QFont("Colibri",25));
animationBoutonMenuText->setKeyValueAt(0.5,QFont("Colibri",30));
animationBoutonMenuText->setKeyValueAt(1,QFont("Colibri",25));
animationBoutonMenuText->start();
但它不工作。它在點擊時重置我的字體大小(我猜默認值是10或11像素)並且它保持默認大小。你有什麼想法,爲什麼?
Ps:我見過this,但那些css
標籤似乎並不適用於Qt。我錯了嗎 ? 這導致了另一個問題(對不起),我們可以修改(意思是使用宏觀的Q_PROPERTY
)css因素嗎?如border-radius
,這應該隨我的按鈕大小而變化。
編輯:
#include "mybutton.h"
QVariant myFontInterpolator(const QFont &start, const QFont &end, qreal progress)
{
int a = start.pixelSize();
int b = end.pixelSize();
int c = (1-progress)*a + progress*b;
QFont rt(start);
rt.setPointSize(c);
return (rt);
}
MyButton::MyButton(QWidget *parent) : QPushButton(parent)
{
this->setGeometry(150,20,340,50);
this->setStyleSheet("border-radius: 25; background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #eeeeee, stop: 1 #5F6060);");
this->setText("Menu");
this->setFont(QFont("Colibri",25));
this->setCursor(Qt::PointingHandCursor);
qRegisterAnimationInterpolator<QFont>(myFontInterpolator);
QPalette pal;
pal.setColor(QPalette::ButtonText,Qt::white);
this->setPalette(pal);
}
MyButton::~MyButton()
{
}
EDIT 2(一段代碼來獲得我想要的行爲):
QVariant myFontInterpolator(const QFont &start, const QFont &end, qreal progress)
{
if (progress<0.5)
{
int a = (1-progress)*25 + progress*30;
QFont rt(start);
rt.setPointSize(a);
return rt;
}
else
{
int a = (1-progress)*30 + progress*25;
QFont rt(start);
rt.setPointSize(a);
return rt;
}
}
與動漫:
animationBoutonMenuText = new QPropertyAnimation(boutonMenu,"font");
animationBoutonMenuText->setDuration(300);
animationBoutonMenuText->setStartValue(QFont("Colibri",25));
animationBoutonMenuText->setEndValue(QFont("Colibri",25));
animationBoutonMenuText->start();
你有沒有關於如何使用插值系統的例子?我應該返回哪種類型來更改我的字體大小? 另外感謝你的例子,但正如解釋我知道如何使用幾何屬性的動畫。 Ps:爲什麼我的邊界半徑「太大」?它是高度的一半,所以我的橫向邊框是半圓而不是直線 – TaiZzZ
我試過但沒有成功(請參閱編輯) – TaiZzZ
我在mybutton.cpp中爲字體添加了動畫。它改變了我的預期大小。 –