[英]Qt - There is a bug in QPropertyAnimation?
我面臨着非常嚴重的情況。 通過寫這個問題,我希望真正的專業人士會對我要描述的問題發表意見。 我在https://bugreports.qt.io/中報告了一個錯誤:
我已經為QTextEdit的maximumWidth屬性創建了QPropertyAnimation,盡管它適用於minimumWidth屬性,但它無法正常工作(它將狀態從開始狀態立即更改為結束狀態)。 請參閱隨附的代碼。
並已附加.h和.cpp文件。 在此處查看這些文件(文件名為new.h和new.cpp)。
我得到了以下回應:
MaximumWidth不是要設置動畫的屬性。 它保留了窗口小部件可以具有的最大寬度,它與布局等有關。 更改maximumWidth(以及minimumWidth)不一定會觸發重新布局和重新繪制。 您應該設置大小的動畫。
請向我解釋說它是錯誤還是沒有? 請告訴我minimumWith屬性是如何動畫的,但是當它涉及到maximumWidth屬性時,那我應該不工作,可以嗎? 我只是不明白他們的意思,請解釋。
PS我之所以編寫此代碼,是因為我想通過動畫關閉正確的QTextEdit,並確保在調整主窗口(按鈕和兩個QTextEdit所在的位置)的大小時,不會恢復關閉的QTextEdit。
您是否檢查了maximumWidth的實際值? 您似乎未在代碼中設置特定的maximumWidth。
maximumWidth的默認值為16777215,並且您設置的持續時間為1毫秒。 結束動畫。 在1毫秒內從16777215衰減到3。 我想看起來像“即時”。
我不認為這是一個錯誤。 我稱其為“不確定行為”。 這意味着,如果您嘗試設置minimumWidth的動畫,則沒有人可以肯定地告訴您應該發生什么,並且代碼可能進行了一些優化或在某些情況下可以工作,但在某些情況下卻沒有。
無論如何,不應該使用minimumWidth和maximumWidth來定義QWidget的大小,而不能定義它的大小。 也就是說,它們並非旨在執行您要嘗試執行的操作,因此可以將其稱為錯誤。 如果要設置動畫,則必須使用確定性方法,在這種情況下,將使用geometry屬性。
這不是錯誤,您從錯誤報告中得到的響應很好地解釋了代碼和解決方案的問題。
親愛的Sofahamster:我已將代碼更改為以下代碼,並且工作正常。 感謝您的提示!
頭文件
class MyWidget : public QWidget
{
Q_OBJECT
QTextEdit *m_textEditor1;
QTextEdit *m_textEditor2;
QPushButton *m_pushButton;
QHBoxLayout *m_layout;
QVBoxLayout *m_buttonLayout;
int m_deltaX;
bool m_isClosed;
public:
MyWidget(QWidget * parent = 0);
~MyWidget(){}
void resizeEvent( QResizeEvent * event );
private slots:
void closeOrOpenTextEdit2(bool isClosing);
};
源文件
MyWidget::MyWidget(QWidget * parent):QWidget(parent),m_deltaX(0)
{
m_pushButton = new QPushButton(this);
m_pushButton->setText(">");
m_pushButton->setCheckable(true);
m_pushButton->setFixedSize(16,16);
connect(m_pushButton, SIGNAL(clicked(bool)), this, SLOT(closeOrOpenTextEdit2(bool)));
m_textEditor1 = new QTextEdit(this);
m_textEditor1->setText("AAAAA AAAAAAAAAAA AAAAAAAAAAA AAAAAAA AAAAAAAAAAA AAAAAAAAAAA AA");
m_textEditor2 = new QTextEdit(this);
m_buttonLayout = new QVBoxLayout();
m_buttonLayout->addWidget(m_pushButton);
m_buttonLayout->addItem( new QSpacerItem(1, 1, QSizePolicy::Minimum, QSizePolicy::Expanding) );
m_layout = new QHBoxLayout;
m_layout->addWidget(m_textEditor1, 10);
m_layout->addSpacing(15);
m_layout->addLayout(m_buttonLayout);
m_layout->setSpacing(0);
m_layout->addWidget(m_textEditor2, 4);
setLayout(m_layout);
resize(800,500);
}
void MyWidget::closeOrOpenTextEdit2(bool isClosing)
{
m_isClosed = isClosing;
QPropertyAnimation *animation1 = new QPropertyAnimation(m_textEditor2, "maximumWidth");
if(isClosing) //close the second textEdit
{
m_textEditor2->setMaximumWidth(m_textEditor2->width());
int textEdit2_start = m_textEditor2->maximumWidth();
m_deltaX = textEdit2_start;
int textEdit2_end = 3;
animation1->setDuration(500);
animation1->setStartValue(textEdit2_start);
animation1->setEndValue(textEdit2_end);
m_pushButton->setText("<");
}
else //open
{
int textEdit2_start = m_textEditor2->maximumWidth();
int textEdit2_end = m_deltaX;
animation1->setDuration(500);
animation1->setStartValue(textEdit2_start);
animation1->setEndValue(textEdit2_end);
m_pushButton->setText(">");
}
animation1->start();
}
void MyWidget::resizeEvent( QResizeEvent * event )
{
if(!m_isClosed)
m_textEditor2->setMaximumWidth( QWIDGETSIZE_MAX );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.