【发布时间】:2018-05-14 16:03:26
【问题描述】:
我在 QT 中构建了一个简单的媒体播放器, 这是代码:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QMediaPlayer* player = new QMediaPlayer;
QVideoWidget *vw = new QVideoWidget;
player->setVideoOutput(vw);
w.setCentralWidget(vw);
QFile io("C:\\file.mp4");
io.open(QFile::ReadOnly);
player->setMedia(QUrl::fromLocalFile("C:\\file.mp4"), &io);
vw->show();
player->play();
return a.exec();
}
当我尝试运行 MAIN.CPP 文件中的代码时,它可以工作并且一切正常。
当我尝试从文件 MAINWINDOW.CPP 运行它时,它不起作用(即使代码是相同的,除了这一行 -
player = new QMediaPlayer(this);
vw= new QVideoWidget(this);
this->setCentralWidget(vw);
和player 和vw 现在在MAINWINDOW.h)
为什么会这样?
【问题讨论】:
-
可能你的
QFile超出范围 - 试试QFile *io = new QFile("C:\\file.mp4", this);