我有一个用{c}设计的QPushButton
。
我希望他在点击时改变大小。我使用QPropertyAnimation(mybutton,"geometry")
来实现这一目标。
然而,规模政策是固定的。要调整此因子,我想使用QPushButton的字体属性。
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()
{
}
编辑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();
答案 0 :(得分:1)
在您的代码中有一些不正确的东西,这就是为什么它没有按预期工作的原因。首先, QFont 不在 QVariantAnimation 列表中,这意味着您无法使用 QPropertyAnimation 和 QFont 进行动画制作像你所做地。 here可能的属性列表。
但是,您可以创建插补器以使其工作。就像那样(它与上一个链接在同一页面中)。
并非所有QVariant类型都受支持。以下是目前的清单 支持QVariant类型:
Int UInt Double Float QLine QLineF QPoint QPointF QSize QSizeF QRect QRectF QColor
如果您需要插入其他变体类型,包括自定义 类型,你必须自己实现插值。去做 这样,您可以为给定类型注册插补器功能。这个 函数有3个参数:起始值,结束值和 目前的进展。
示例:
QVariant myColorInterpolator(const QColor&amp; start,const QColor&amp; end, qreal progress){ ... 返回QColor(...); } ... qRegisterAnimationInterpolator(myColorInterpolator);
另一种选择是重新实现插值(),返回 插值的插值。
您的border-radius 太大,值 15 ,边界半径很好。 所以这里有一个带有动画的小代码(但不能完全按照你想要的那样)。
test.pro:
#-------------------------------------------------
#
# Project created by QtCreator 2017-06-26T12:49:54
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += main.cpp\
mainwindow.cpp \
mybutton.cpp
HEADERS += mainwindow.h \
mybutton.h
FORMS += mainwindow.ui
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 "mybutton.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
auto myButton = new MyButton(this);
ui->verticalLayout_2->addWidget(myButton);
}
MainWindow::~MainWindow()
{
delete ui;
}
mainwindow.h:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mybutton.cpp:
#include "mybutton.h"
#include <QPropertyAnimation>
#include <QDebug>
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(rt.pointSize() - c);
return (rt);
}
MyButton::MyButton(QWidget *parent) : QPushButton(parent)
{
this->setGeometry(150,20,340,50);
this->setStyleSheet("background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #eeeeee, stop: 1 #5F6060); border-radius: 19;");
this->setText("Menu");
this->setFont(QFont("Colibri", 25));
this->setCursor(Qt::PointingHandCursor); // Marche que au début (si on est pas passé au dessus d'autre chose)
qRegisterAnimationInterpolator<QFont>(myFontInterpolator);
QPalette pal;
pal.setColor(QPalette::ButtonText,Qt::white);
this->setPalette(pal);
}
void MyButton::mousePressEvent(QMouseEvent *ev)
{
auto animationBoutonMenuText = new QPropertyAnimation(this,"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();
}
mybutton.h:
#ifndef MYBUTTON_H
#define MYBUTTON_H
#include <QPushButton>
class MyButton : public QPushButton
{
Q_OBJECT
public:
explicit MyButton(QWidget *parent = Q_NULLPTR);
~MyButton() {}
public slots:
void mousePressEvent(QMouseEvent *ev);
};
#endif // MYBUTTON_H
编辑: mybutton.cpp已使用随动画更改的字体进行了更新。我不确定这是你想要的动画,但你可以从它开始。