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
和
#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中,并且在这篇文章中讨论了一点:
您可以通过删除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混合窗口在你的平台上根本不起作用,你应该明确地提到你的情况是什么…
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 最小硬币更换问题(自上而下方法)
- Qt VTK交互风格的信号到小部件
- 芬威克树(BIT).找到具有给定累积频率的最小索引,单位为 O(logN)
- 如何在Qt窗口小部件中使用QStringView(或QStringRef)
- 使用递归的数组的最小值.这是怎么回事
- 为什么在浮点中从大到小会引入更多的误差
- 找到两对数字,使它们的乘积的绝对差最小化
- 将地址分配给本地指针后,公共对象的变量将消失
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 最大和最小 1 C++函数
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 我可以做些什么来消除或最小化这种将提供相同功能和行为的代码重复
- 最小化/最大化后,MFC窗口图形消失了
- C++ 创建小部件数组
- 当在另一个较小的对象Opengl后面时,“大对象”将消失
- QTabWidget角QToolButton小部件正在消失
- 当程序最小化时位图消失
- QT c++中的消失小部件
- 查找滚动条消失时QScrollArea小部件的大小