簡體   English   中英

Qt Animate QPushButton圖標

[英]Qt Animate QPushButton Icon

我想設置QPushButton圖標的大小的動畫,因為當我嘗試設置整個QPushButton對象大小的動畫時,圖標根本沒有調整大小。 似乎QPushButton圖標上沒有屬性“ setScaledContents”。 我試圖在我的自定義QPushButton方法中將以下代碼用於圖標(懸停時):

QIcon *icon = new QIcon(this->icon());
QPropertyAnimation *animation = new QPropertyAnimation((QObject*)icon, "size");
animation->setDuration(1000);
QSize test = this->size();
animation->setStartValue(test);

animation->setEndValue(QSize(test.width()+100,test.height()+100));

animation->start();

但是我在此代碼上收到錯誤“分段失敗”。 我還有其他方法可以使QPushButton圖標動畫嗎?

您需要做的是改為傳遞對QObject的有效引用(根據上述Chris的評論)

如果我簡單地替換傳遞給QPropertyAnimation的參數,您的代碼就可以正常工作:

// ui->pushButton is a QPushButton*
QPropertyAnimation *animation = new QPropertyAnimation(ui->pushButton, "size");
animation->setDuration(1000);
QSize test = this->size();
animation->setStartValue(test);
animation->setEndValue(QSize(test.width()+100,test.height()+100));
animation->start();

我將假設您是QPushButton的子類(解釋this-> icon())...也許您可以嘗試直接訪問它? 盡管我猜測它是由基類/父類私有的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM