日志
关于phonon视频播放器退出全屏的解决方案
2011-10-07 18:10
本人最近几天在学习Phonon的使用,在视频播放器上遇到了一些问题,其中相对麻烦点的是videoWidget进入全屏后用鼠标事件实现退出全屏的问题,在网上找了挺长时间无果,又去了外国的QT centre论坛还是没有找到答案,到发现遇到这个问题的老外都没有人提供解决具体解决方案。自己经过多次试验,找到了一些解决方案,特此写出来,希望能帮助到一些遇到同样问题的朋友,文章没有什么含金量。
先看看官方文档对此的描述如下: fullScreen : boolThis property holds whether the video is shown using the complete screen.The property differs from QWidget::fullScreen in that it is writeable.By default the widget is not shown in fullScreen.Warning: When switching to full screen mode using setFullScreen(), the widget onto which the video is rendered is shown as a top-level window. Key event forwarding is handled by VideoWidget, but if you need to handle other events, e.g., mouse events, you should handle fullscreen mode yourself.Access functions:
可知当进入全屏后videoWidget成为顶级窗口,但按键事件还是有效的,然而像鼠标事件等默认情况下已经对videoWidget无效。所以可以用QAction的方法实现退出全屏,这个方法我挺早就实现了,只是想使用鼠标双击实现退出全屏,所以才在纠结。 QAction方法如下: // 退出全屏模式的Action QAction *exitFullScreenAction = new QAction(videoWidget); videoWidget->addAction(exitFullScreenAction); videoWidget->setAcceptDrops(true); exitFullScreenAction->setShortcuts(QList<QKeySequence>() << QKeySequence(Qt::Key_Escape)<< QKeySequence(Qt::Key_MediaStop)); connect(exitFullScreenAction, SIGNAL(triggered()), videoWidget,SLOT(exitFullScreen())); 第二种方法是我是使用eventFilter事件过滤器的方法,首先在定义videoWidget的时候对其安装一个事件过滤器;如下: videoWidget = new Phonon::VideoWidget(this); videoWidget->setAspectRatio(Phonon::VideoWidget::AspectRatioAuto); Phonon::createPath(mediaObject, videoWidget); videoWidget->installEventFilter(this); 然后实现eventFilter函数: // 事件过滤器 bool videoPlayer::eventFilter(QObject *obj, QEvent *event) { QMouseEvent *mouse = static_cast<QMouseEvent *>(event); // 转换为鼠标事件 if (obj == videoWidget) { // 双击实现全屏和推出全屏 if (event->type() == QEvent::MouseButtonDblClick) { if(mouse->button() == Qt::LeftButton){ if(!bFull){ // 不是全屏 则进入全屏 bFull 是类成员变量 标志着是否进入了全屏 bool类型 videoWidget->enterFullScreen(); bFull = true; } else{ videoWidget->exitFullScreen(); bFull = false; } return true; } } else return false; } else return videoPlayer::eventFilter(obj, event); } 当然还有其他的方法,今天回看《精通QT4编程》看到类似的解决方法有五种,不过也是刚看到的,具体怎么实现没有试验过,有朋友提供好的方法本人非常感谢。本人是新人,接触QT时间很短,也没有学过C++、java之类的,如果有错望大家见谅。 贴一下效果图吧: |
下一篇: 解决QT浏览器中链接打不开的问题