这部分问题已经解决
我想要将带有QGraphicsVideoItem的QGraphicsScene渲染到QImage。当QGraphicsScene只有QGraphicsTextItem时,一切工作正常。但是,如果我用QGraphicsVideoItem替换QGraphicsTextItem,它无法输出正确的图像。 如何解决这个问题?谢谢... 以下代码是用来测试这个问题的。
#include <QApplication>
#include <QGraphicsScene>
#include <QMediaPlayer>
#include <QGraphicsVideoItem>
#include <QImage>
#include <QGraphicsView>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QGraphicsScene scene;
/* part 1. videoItem */
QMediaPlayer* videoPlayer = new QMediaPlayer;
QGraphicsVideoItem* screen = new QGraphicsVideoItem;
videoPlayer->setVideoOutput(screen);
scene.addItem(screen);
videoPlayer->setMedia(QUrl::fromLocalFile("./data/Taylor Swift - Blank Space.mp4"));
videoPlayer->play();
videoPlayer->setVolume(100);
/* part 2. textItem */
/*QGraphicsScene scene;
QGraphicsTextItem* text = new QGraphicsTextItem("aaaaaaa");
scene.addItem(text);*/
QGraphicsView view(&scene);
view.resize(1920, 1080);
view.show();
videoPlayer->pause();
QImage image(1920, 1080, QImage::Format_ARGB32);
image.fill(Qt::blue);
QString pngName = "scene.png";
QPainter painter(&image);
painter.setRenderHint(QPainter::Antialiasing);
scene.render(&painter);
image.save(pngName);
return a.exec();
}
在你的.pro文件中添加以下内容可能会有帮助:)
QT += core gui
QT += core
#QT += 3d
QT += gui
QT += multimedia
QT += widgets
QT += multimediawidgets
新问题
在我的项目中,我想在QGLView窗口(Qt3D)中渲染视频。然而,QGraphicsView和QGLView似乎不能同时呈现。如果我不使用QGLView的show()方法,我可以获取视频帧。如果我使用QGLView的show()方法,我无法获得正确的帧...
那么,我该如何实现上述想法?