QT c++中的消失小部件

Fading away widget in QT C++

本文关键字:小部 消失 c++ QT      更新时间:2023-10-16

我想做一个简单的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

#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()。这通常不会产生平滑的动画。

我在Kubuntu Linux上得到一个警告,在Qt5下:

QWidget:: painengine:不应该再被调用

起始行在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);来获得停止警告,所以我这样做了。在去掉这条线之后,它对我有用——尽管你的减法模型很奇怪。你在修改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();
}

等。(也见@狱中猴子注意到你的定时器不一定是update()调用的唯一来源)关于让这些警告更有声音,所以你不会错过它们,你可以看看Qt的基本噪声调试钩子,我应该更新。

如果改变了,alpha混合窗口在你的平台上根本不起作用,你应该明确地提到你的情况是什么…