2017-06-21 156 views
1

我有一個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(); 

回答

1

有一些事情,在你的代碼中是不正確的,這就是爲什麼它不像你期望的那樣工作。首先,QFont是不是在QVariantAnimation名單,這意味着像你一樣,你不能做動畫與QPropertyAnimationQFonthere可能屬性的列表。

但是,您可以創建一個插補器使其工作。就像那樣(它和前面的鏈接在同一頁面)。

並非所有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已經更新與動畫改變字體。我不確定這是你尋找的動畫,但你可以從它開始。

+0

你有沒有關於如何使用插值系統的例子?我應該返回哪種類型來更改我的字體大小? 另外感謝你的例子,但正如解釋我知道如何使用幾何屬性的動畫。 Ps:爲什麼我的邊界半徑「太大」?它是高度的一半,所以我的橫向邊框是半圓而不是直線 – TaiZzZ

+0

我試過但沒有成功(請參閱編輯) – TaiZzZ

+0

我在mybutton.cpp中爲字體添加了動畫。它改變了我的預期大小。 –