调整字体大小 - 字体属性
我有一个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();
有一些事情,在你的代码中是不正确的,这就是为什么它不像你期望的那样工作。首先,QFont是不是在QVariantAnimation名单,这意味着像你一样,你不能做动画与QPropertyAnimation和QFont。 here可能属性的列表。
但是,您可以创建一个插补器使其工作。就像那样(它和前面的链接在同一页面)。
并非所有QVariant类型都受支持。下面是目前 支持的QVariant类型的列表:
Int UInt Double Float QLine QLineF QPoint QPointF QSize QSizeF QRect QRectF QColor
如果需要其他插值变异类型,包括自定义 类型,你必须实现插值为这些自己。要做 这个,你可以注册给定类型的插补函数。这个 函数需要3个参数:起始值,结束值和当前进度。
实施例:
的QVariant myColorInterpolator(常量的QColor &开始,常量的QColor &端, QREAL进展){ ... 返回的QColor(...); } ...qRegisterAnimationInterpolator(myColorInterpolator);
另一种选择是重新实现interpolated(),它将为插值后的值返回 插值。
你的边界半径是太大,与的值,所以是一个很好的边界半径。 所以这里有一个动画的小代码(但并不完全是你想要的)。
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已经更新与动画改变字体。我不确定这是你寻找的动画,但你可以从它开始。
你有没有关于如何使用插值系统的例子?我应该返回哪种类型来更改我的字体大小? 另外感谢你的例子,但正如解释我知道如何使用几何属性的动画。 Ps:为什么我的边界半径“太大”?它是高度的一半,所以我的横向边框是半圆而不是直线 – TaiZzZ
我试过但没有成功(请参阅编辑) – TaiZzZ
我在mybutton.cpp中为字体添加了动画。它改变了我的预期大小。 –