首页 > 解决方案 > 在“fitInView”呼叫后,十字准线在视频屏幕上的错误位置

问题描述

这个问题与此有关: Video size not fill in QGraphicsView window

我尝试使用 'scene->addLine(...)' 在屏幕上添加一条 'red' 线。但是,在“fitInView”调用之后还差得很远。一开始,十字准线在正确的位置,屏幕中间。但是,当视频开始时(在“fitInView”Lambda 调用之后),它完全消失了。我认为问题在于“场景”的大小和位置不正确。如何使“场景”的大小和起始(x,y)与“视图”相匹配,以便将十字准线绘制在正确的位置。

以下是新代码:

#include <QApplication>
#include <QMainWindow>
#include <QMediaPlayer>
#include <QGraphicsView>
#include <QGraphicsVideoItem>
#include <QGraphicsScene>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    QGraphicsView* view = new QGraphicsView;
    QGraphicsScene* scene = new QGraphicsScene(view);

    view->setScene(scene);
    QMediaPlayer * player = new QMediaPlayer;
    QGraphicsVideoItem *item = new QGraphicsVideoItem;
    player->setVideoOutput(item);
    player->setMedia(QUrl::fromLocalFile("/path/to/tmp1.mp4"));
    // player->setMedia(QUrl("rtsp://10/0/8/100:8554/0"); // Live IP camera
    scene->addItem(item);
w.resize(640, 480);

    QObject::connect(scene, &QGraphicsScene::sceneRectChanged, [=](const QRectF &) { view->fitInView(item); });
    player->play();
    w.setCentralWidget(view);
    w.show();    

   // Draw a crosshair
   QPen p(Qt::red);
   qreal centerX, centerY;
   centerX = view->width() / 2;
   centerY = view->height() / 2;
   qreal halfLen = centerX / 2;
   (void)scene->addLine(halfLen, centerY, centerX + halfLen, centerY, p);
   (void)scene->addLine(centerX, centerY - 30, centerX, centerY + 30, p);

    return a.exec();
}

标签: c++qtqgraphicsviewqgraphicsscene

解决方案


推荐阅读