簡體   English   中英

在QT C ++中淡出小部件

[英]Fading away widget in QT C++

我想制作一個簡單的QWidget,它是一個簡單的矩形消失。 主要問題是油漆事件每次塗在同一個地方實際上使效果相反,它使顏色更強。 有沒有辦法實現這個功能? 你能提供一些簡單的例子嗎?

我的代碼:`

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    void paintEvent(QPaintEvent*);
    ~Widget();

private:
    Ui::Widget *ui;
    int alfa;
    int i;
    QTimer time;

};

#endif // WIDGET_H

`

和cpp:

#include "widget.h"
#include "ui_widget.h"
#include <QColor>
#include <QPainter>
#include <QBrush>
#include <QTimer>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent,Qt::Popup | Qt::FramelessWindowHint),
    ui(new Ui::Widget),
    time()
{
    ui->setupUi(this);
    setAttribute(Qt::WA_NoSystemBackground, true);
    setAttribute(Qt::WA_TranslucentBackground, true);
    setAttribute(Qt::WA_PaintOnScreen);
    time.setInterval(500);
    time.start();
    connect(&time,SIGNAL(timeout()),this,SLOT(update()));
    alfa=100;
    i=0;

}

void Widget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    int rectBase = height();

    QColor c(255,0,255);
    alfa=alfa-(i*10);
    c.setAlpha(alfa);
    qDebug()<<c.alpha();
    i++;

    painter.setBrush(QBrush(c));
    painter.drawRect(0, 0, width(),height());


}

Widget::~Widget()
{
    delete ui;
}

你不應該依賴QWidget::paintEvent()來改變你的alpha級別,因為它可以被調用少於或多於你想要的(多次update()調用可能只會導致一個paintEvent()paintEvent()被調用當你不指望它)。

因此,獲得結果的更可靠方法是使用單獨的插槽來降低alpha級別,然后調用update() 您的類定義可能如下所示:

class Widget : public QWidget
{
    Q_OBJECT
public:
    Widget( QWidget * inParent );

private:
    void paintEvent(QPaintEvent *);

private slots:
    void animate();

private:
    QTimer * mTimer;
    int mAlpha;
};

聲明:

Widget::Widget( QWidget * inParent )
    :
      QWidget( inParent ),
      mTimer( new QTimer( this ) ),
      mAlpha( 255 )
{
    setAttribute(Qt::WA_NoSystemBackground, true);
    setAttribute(Qt::WA_TranslucentBackground, true);
    setAttribute(Qt::WA_PaintOnScreen);
    mTimer->setInterval( 40 );
    mTimer->setSingleShot( false );
    connect( mTimer, SIGNAL(timeout()), this, SLOT(animate()) );
    mTimer->start();
}

void
Widget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setBrush( QColor(255,0,255,mAlpha ) );
    painter.drawRect(rect());
}

void
Widget::animate()
{
    if ( mAlpha > 0 )
    {
        mAlpha -= 3;
    }
    else
    {
        mTimer->stop();
    }
    update();
}

請注意,我確實減少了計時器的間隔。 你每隔半秒就調用一次update() 這通常不會產生平滑的動畫。

在Qt5下,我在Kubuntu Linux上收到此代碼的警告:

QWidget :: paintEngine:不應該再被調用

原始行在qWarning("QWidget::paintEngine: Should no longer be called"); ,在src / widgets / kernel / qwidget_qpa.cpp中,並在此票證中討論了一下:

https://qt.gitorious.org/qt/qtbase-harmattan/commit/3037525

您可以通過刪除setAttribute(Qt::WA_PaintOnScreen);來獲取停止警告setAttribute(Qt::WA_PaintOnScreen); 所以我這樣做了。 取出那條線后,它對我有用 - 雖然你的減法模型很奇怪。 您正在修改alpha以及在每次迭代時更改減法值; 你可能不打算兩者兼而有之。 所以要么改為:

QColor c (255, 0, 255);
alfa = alfa - 10;
if (alfa >= 0) {
    c.setAlpha(alfa);
} else {
    time.stop();
}

...要么:

QColor c(255,0,255);
if (alfa - i * 10 >= 0) {
    c.setAlpha(alfa - i * 10);
    i++;
} else {
   time.stop();
}

等等(另請參閱@PrisonMonkeys關於你的計時器的說明不一定是update()調用的唯一來源。)關於讓這些警告更加直言不諱,你不要錯過它們,你可能會看看The Essential Noisy Debug Hook For Qt ,我應該更新。

如果進行了更改,Alpha混合窗口根本不適用於您的平台,您應該明確提到您的情況......因為它對我有用。

暫無
暫無

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

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