Qt显示一个控件一秒后消失.
大家好,我想实现这样的一个效果: 一个小方块,等点击某个操作后,在屏幕的右下角显示一个反馈信息,等一秒后消失.有点
类似android手机里的退出提示控件ctoast,
我的代码思路是这样的: 继承一个QWidget,再里面放一个定时器,等时间到后,关闭当前这个Widget,这样做放到别的类中
构造函数还可以,可是放到普通函数中,就出现段错误,请问下这个是什么情况.
我的代码如下
#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QWidget>#include <QtGui/QWidget>#include <QTimer>#include <QGroupBox>#include <QLabel>class CToast : public QWidget{ Q_OBJECTpublic: explicit CToast(const int timeMs=10000,const QString string="set success!",QWidget *parent = 0); ~CToast();private slots: void closewidget();private: QLabel *label; QGroupBox *grpWidget; QTimer *timer;};#include <QTimer>class Widget : public QWidget{ Q_OBJECT public: Widget(QWidget *parent = 0); ~Widget();private slots: void timerouttest();private: QTimer *m_timer;};#endif // WIDGET_H
#include "widget.h"#include <QHBoxLayout>#include <QLabel>#include <QDesktopWidget>#include <QApplication>#include <QMessageBox>#include <QTimer>#include <QGridLayout>#include <QGraphicsColorizeEffect>CToast::CToast(const int timeMs, const QString string, QWidget *parent) : QWidget(parent){ setWindowFlags(Qt::FramelessWindowHint); //设置无windows标题 setFixedSize(80,40); move(200,150); timer=new QTimer; connect(timer,SIGNAL(timeout()),this,SLOT(closewidget())); timer->start(timeMs); setStyleSheet("background-color: black;border-radius:15px"); label=new QLabel((string)); label->setStyleSheet("color:white"); QGridLayout *rowWin=new QGridLayout; rowWin->setContentsMargins(5,5,5,5); rowWin->addWidget(label,0,0); setLayout(rowWin); } CToast::~CToast() { delete label; delete grpWidget; delete timer; timer=NULL; } void CToast::closewidget() { timer->stop(); close(); delete this; }Widget::Widget(QWidget *parent) : QWidget(parent){ m_timer=new QTimer(); connect(m_timer,SIGNAL(timeout()),this,SLOT(timerouttest())); m_timer->start(1000);}Widget::~Widget(){ }void Widget::timerouttest(){ CToast *to1=new CToast(1000*1,"lajdsajs");}