qt gui使用Qfiledialogbox和qlabel来显示视频。

问题描述 投票:1回答:1

第一代码 在这段代码中,VideoCapture cap没有采用执行QFileDialog后获得的文件名(包含视频路径),即cap.open("fileName"),代码没有执行,而cap.open("G:mixer.avi"),代码正在执行。 这意味着如果视频路径直接在cap.open()中给出,那么代码正在执行,但它没有采用QFileDialog输出,即文件名作为输入。

void MainWindow::on_pushButton_clicked()
{
 if(ui->radioButton->isChecked()) {


      QString fileName = QFileDialog::getOpenFileName(this,
          tr("Open Video"), "G://", "All files (*.*);Video files(*.*)");
      ui->lineEdit->setText(fileName);
     VideoCapture cap
    // cap.open("G:/mixer.avi");
     cap.open("fileName");

    connect(timer, SIGNAL(timeout()), this, SLOT(update_window()));
    timer->start(20);}}
    void MainWindow::update_window()
     {
     cap >> frame;

    cvtColor(frame, frame, cv::COLOR_BGR2RGB);

    qt_image = QImage((const unsigned char*) (frame.data), frame.cols, frame.rows, 
     QImage::Format_RGB888);

    ui->label->setPixmap(QPixmap::fromImage(qt_image));

    ui->label->resize(ui->label->pixmap()->size());
}

第二密码 :

在这段带有QFileDialogbox的代码中,视频正在显示,但它没有显示在QLabel中。

 void MainWindow::on_pushButton_2_clicked()
    {
    if(ui->radioButton_2->isChecked()) {

    QString fileName = QFileDialog::getOpenFileName(this,
              tr("Open Video"), "G://", "All files (*.*);Video files(*.*)");
     ui->lineEdit->setText(fileName);
       player = new QMediaPlayer;
            vw=new QVideoWidget;
          player->setVideoOutput(vw) ;
          this->setCentralWidget(vw);
           player->setMedia(QUrl::fromLocalFile(fileName));
           vw->show();
          player->play();

}
}
c++ qt opencv qlabel qmediaplayer
1个回答
0
投票

视频捕捉(const String & filename) 可以接受字符串作为输入,而不是QString。需要做2件事情。

  1. 首先将QString文件名转换为字符串
  2. 在该字符串的前几位可以有一些额外的字符,因为 filedialog 所选择的文件输出与 opencv 的哪个文件几乎没有区别。VideoCapture 需要。
© www.soinside.com 2019 - 2024. All rights reserved.