繁体   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