【问题标题】:QGraphicView doesn't show anythingQGraphicsView 不显示任何内容
【发布时间】:2019-08-02 07:35:26
【问题描述】:

我尝试使用 QGraphicView 显示视频,但我的代码没有显示任何内容。有什么问题

QGraphicsScene scene;
QGraphicsVideoItem *item1 = new QGraphicsVideoItem;
item1->setPos(0,100);

QMediaPlayer * player1 = new QMediaPlayer;
player1->setVideoOutput(item1);
player1->setMedia(QUrl("/home/1.wmv"));
QGraphicsView view;
view.scale(0.3,0.3);
view.setScene(&scene);
view.show();
player1->play();

【问题讨论】:

  • 更改为QGraphicsScene *scene = new QGraphicsScene;QGraphicsView *view = new QGraphicsView(scene);,提供minimal reproducible example

标签: c++ qt qwidget


【解决方案1】:

您正在创建一个QGraphicsVideoItem,并将其用作QMediaPlayer 的输出,并正在创建一个QGraphicsView,并在其上分配一个QGraphicsScene。但是您没有将QGraphicsVideoItem(或QMediaPlayer)连接到QGraphicsView(或QGraphicsScene),因此QGraphicsView 中显然没有显示任何内容。

请注意,这只是基于您提供的不完整代码的猜测答案。以后请务必包含所有相关代码/创建Minimal, Reproducible Example

【讨论】:

    【解决方案2】:

    你忘了:

    scene->addItem(item1);
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        QGraphicsScene * scene = new QGraphicsScene();
        QGraphicsView * view = new QGraphicsView(scene);
    
        QGraphicsVideoItem *item1 = new QGraphicsVideoItem;
        QMediaPlayer * player1 = new QMediaPlayer;
    
        scene->addItem(item1);
        item1->setPos(0,100);
    
        view->scale(0.3,0.3);
        view->show();
    
        player1->setVideoOutput(item1);
        player1->setMedia(QUrl::fromLocalFile("/home/user/Musik/musik.mp4"));
    
        player1->play();
    
        return a.exec();
    }
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-29
      • 2020-11-10
      • 2012-04-12
      • 2019-11-11
      • 2017-09-25
      • 2012-08-02
      • 2019-08-11
      • 2018-06-25
      相关资源
      最近更新 更多