场景1:
在做点击窗口播放视频时,遇到点击窗口,窗口的重写事件没有被触发。。。
原因是窗口上有个label,鼠标点击窗口上的label,触发的是label的事件。。如果要把事件分发给label的父窗口,我这用到了事件分发器。。
使用事件分发器:
- 控件指定 installEventFilter(QObject *obj) :指定在那个obj窗口上安装事件过滤器(事件分发到父窗口,再有父窗口到子部件,过滤是查看事件)
- obj窗口实现 bool eventFilter(QObject *watched, QEvent *event) 函数,通过event的type来判断事件的类型,做相应处理
.h文件:
class ShowGifPreview : public QWidget
{
Q_OBJECT
public:
ShowGifPreview(QWidget* parent);
void setGifPath(const QString& gifPath);
void updateGifList();
protected:
virtual void mousePressEvent(QEvent *e);
bool eventFilter(QObject *watched, QEvent *event);
public slots:
void gifShow();
signals:
void gifClicked();
void gifFinished();
private:
QLabel* m_currentPreview;
QLabel* m_gifPreview;
bool m_isGifClicked = false;
QMovie* m_gifMovie;
QString m_gifPath="";
};
.cpp文件:
ShowGifPreview::ShowGifPreview(QWidget *parent):QWidget(parent)
{
// m_timer = new QTimer(this);
m_gifMovie = new QMovie();
m_gifPreview = new QLabel(this);
m_gifPreview->setMovie(m_gifMovie);
QHBoxLayout* mainLayout = new QHBoxLayout;
mainLayout->addWidget(m_gifPreview);
setLayout(mainLayout);
QPixmap pix("E:/newWork/vivoThemeTool/test/showGif/frames/0.jpg");
m_gifPreview->setPixmap(pix);
m_gifPath = "E:/newWork/vivoThemeTool/test/build-showGif-Desktop_Qt_5_9_3_MinGW_32bit-Release/release/tmp-edit.gif";
//安装事件过滤器
m_gifPreview->installEventFilter(this);
}
void ShowGifPreview::setGifPath(const QString &gifPath)
{
if(gifPath.isEmpty()) return;
QFile::remove(QApplication::applicationDirPath() + "/tmp-edit.gif");
QFile::copy(gifPath, QApplication::applicationDirPath() + "/tmp-edit.gif");
m_gifPath = QApplication::applicationDirPath() + "/tmp-edit.gif";
qDebug() << gifPath << " :" << m_gifPath;
}
void ShowGifPreview::updateGifList()
{
// m_picList = list;
}
//方式2:安装事件过滤器
bool ShowGifPreview::eventFilter(QObject *watched, QEvent *event)
{
qDebug() << "event";
//过滤mouseEvent
QMouseEvent *mouse = static_cast<QMouseEvent*>(event);
if(event->type() == QMouseEvent::MouseButtonPress && mouse->button() == Qt::LeftButton){//右键按下
qDebug() << "event type: press";
m_gifMovie->stop();
if(!m_gifPath.isEmpty()){
if(QFileInfo(m_gifPath).exists()){
qDebug() << "start";
m_gifMovie->setFileName("tmp-eidt.gif");
m_gifMovie->start();
return true;//表示事件已处理
}
}
}
return QWidget::eventFilter(watched, event);
}
//方式1:no use 原因是label挡住了widget,点击label不能触发widget的事件
void ShowGifPreview::mousePressEvent(QEvent *e)
{
qDebug() << "start before";
if(!m_isGifClicked){
if(!m_gifPath.isEmpty()){
if(QFileInfo(m_gifPath).exists()){
qDebug() << "start";
m_gifMovie->setFileName("tmp-eidt.gif");
m_gifMovie->start();
}
}
} /*else {
m_gifMovie->stop();
}*/
m_isGifClicked = ~m_isGifClicked;
}
eventFilter 返回true表示事件已处理
场景2:
在自定义标题栏添加显示 文件名称,原始是通过每次 setText()来更新标题,这里介绍一种更好的方式,widget通过 setWindowTitle 会发送 windowTitleChange信号,在 自定义的标题栏里实现 eventFilter() ,为使用title的widget安装事件过滤器,就可以监测标题变化,并实时更新了
bool ScreenlockTitleBar::eventFilter(QObject *obj, QEvent *event)
{
if(event->type() == QEvent::WindowTitleChange){
QWidget *w = qobject_cast<QWidget*>(obj);
if(w){
m_lb_name->setText(w->windowTitle());
return true;//表示已处理
}
}
return QWidget::eventFilter(obj, event);
}