【问题标题】:qt gui to display video using Qfiledialogbox and qlabel .here is my codeqt gui 使用 Qfiledialogbox 和 qlabel 显示视频。这是我的代码
【发布时间】:2020-09-19 21:09:31
【问题描述】:

第一个代码 在此代码中,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 中没有显示。如何在 QMultimedia 中添加 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】:

    VideoCapture(const String & filename) 可以将字符串作为输入而不是 QString。需要做的两件事:

    1. 首先将 QString 文件名转换为字符串
    2. 该字符串的第一位可能有一些额外的字符,因为 filedialog 选择的文件输出与 opencv VideoCapture 需要的输出几乎没有什么不同。

    【讨论】:

    • 我已经使用代码将 QString 转换为字符串 - std::string videopath = fileName.toLocal8Bit().constData();但代码没有运行。以及该字符串的第一个位置需要哪些类型的额外字符。因为 filedialog 提供的路径与我们在 videocapture 中需要的路径相同。
    • 是否还有其他代码可以使用 c++ 而不是 QFiledialog 打开 filedialog。?这样我们就可以得到 String 而不是 QString。
    • 不,对于文件对话框,您需要 qt。首先尝试使用imshow 显示视频,而不是使用qlabel。先检查一下,不显示的问题可能在ui端。
    • 我已成功使用 imshow 显示视频,但我必须从需要打开文件对话框的文件夹中选择文件。如果我使用 qt 使用 filedialogbox,则使用 imshow 不显示视频 videocapture cap is nottaking qstring.如何从文件夹中选择文件。
    猜你喜欢
    • 1970-01-01
    • 2020-05-02
    • 2012-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多