Css 调整字体大小-字体属性

Css 调整字体大小-字体属性,css,qt,Css,Qt,我有一个用css设计的QPushButton。 我想让他在点击时改变尺寸。我使用了QPropertyAnimation(mybutton,“geometry”)来实现这个目标。 然而,规模政策是固定的。为了调整这个系数,我想使用QPushButton的字体属性 class MyButton : public QPushButton { Q_OBJECT public: explicit MyButton(QWidget *parent = Q_NULLPTR); ~My

我有一个用css设计的
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:我看到了,但是那些
css
标签似乎在Qt上不起作用。我错了吗? 这就引出了另一个问题(对不起),我们可以修改(意思是使用
Q_属性
macro)css因子吗?例如
边框半径
,应随“我的按钮”的大小而变化

编辑:

#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()
{

}

您的代码中有一些不正确的地方,这就是为什么它不能像您期望的那样工作。首先,QFont不在QVariantAnimation列表中,这意味着您不能像您那样使用QPropertyAnimationQFont制作动画。可能属性的列表

但是,可以创建插值器使其工作。就像那样(它与上一个链接位于同一页面中)

并非所有QVariant类型都受支持。以下是目前的工作清单 支持的QVariant类型:

Int
UInt
Double
Float
QLine
QLineF
QPoint
QPointF
QSize
QSizeF
QRect
QRectF
QColor
如果需要插入其他变量类型,包括自定义 类型,您必须自己为这些类型实现插值。做 这样,就可以为给定类型注册插值函数。这 函数接受3个参数:起始值、结束值和 目前的进展

例如:

QVariant霉色素内极体(常量QColor&start,常量QColor&end, (实际进度){ ... 返回QColor(…);}。。。qRegisterAnimationInterpolator(霉色素Interpolator)

另一个选项是重新实现interpolated(),它返回 正在插值的值的插值值

您的边界半径太大,值为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.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
\ifndef主窗口
#定义主窗口
#包括
名称空间用户界面{
类主窗口;
}
类主窗口:公共QMainWindow
{
Q_对象
公众:
显式主窗口(QWidget*parent=0);
~main窗口();
私人:
Ui::MainWindow*Ui;
};
#endif//main窗口
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”
#包括
#包括
QVariant myFontInterpolator(常量QFont&start、常量QFont&end、qreal progress)
{
int a=start.pixelSize();
int b=end.pixelSize();
INTC=(1-进度)*a+进度*b;
QFont rt(启动);
rt.setPointSize(rt.pointSize()-c);
返回(rt);
}
MyButton::MyButton(QWidget*parent):QPushButton(parent)
{
这->设置几何体(150,20340,50);
此->设置样式表(“背景色:QlineArgent(x1:0,y1:0,x2:0,y2:1,停止:0#eeeeee,停止:1#5F6060);边框半径:19;”;
此->设置文本(“菜单”);
这->设置字体(QFont(“Colibri”,25));
this->setCursor(Qt::PointingHandCursor);//Marche que au début(我选择了这条路)
qRegisterAnimationInterpolator(myFontInterpolator);
QPalette-pal;
pal.setColor(Qpalete::ButtonText,Qt::white);
这->设置调色板(pal);
}
void MyButton::MousePresseEvent(QMouseEvent*ev)
{
auto animationBoutonMenuText=新的QPropertyAnimation(此“字体”);
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
\ifndef MYBUTTON\u H
#定义我的按钮
#包括
类MyButton:公共QPushButton
{
Q_对象
公众:
显式MyButton(QWidget*parent=Q_NULLPTR);
~MyButton(){}
公众时段:
无效鼠标压力事件(QMouseEvent*ev);
};
#endif//MYBUTTON\u H
编辑:
mybutton.cpp已更新为随动画变化的字体。我不确定这是你想要的动画,但你可以从它开始。

你有关于如何使用插值系统的例子吗?我应该返回什么类型来更改字体大小?另外,感谢您的示例,但正如所解释的,我知道如何使用几何体属性的动画。为什么我的边界半径“太大”?它是高度的一半,因此我的侧边是半圆形而不是线条。我在mybutton.cpp中为字体添加动画。它按照我的预期更改了大小。我在代码中更改了实现,所以我的实现与您几乎相同,但我有几个错误:QVariant不完整,qRegisterAnimationInterpolator未声明,预期在“>”之前有主表达式。你没有吗?我没有更改我以前的任何代码,因此动画的声明是相同的,我不确定它是否正确。不,我没有任何错误。确保添加所有必要的包含项。(您可以在我的代码中看到它们)包含已修复的一个,但最后两个保留。你做了什么“声明”了qRegisterAnimationInterpolator吗?
#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
#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();
}
#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