2

我在 Qt Creator 中设计了一个表单。我有三个控件。

  • Group Box(底部控件)
  • Widget(中间控件)
  • 标签(最上面的控件)

我在标签上显示/播放视频的帧(可能不是正确的方式,但我是 Qt 的新手,所以暂时它对我有用)。

我的控件是固定大小的。

我的问题是如何根据我的控件(即小部件或标签)调整视频大小?我希望所有视频都以与我的小部件或标签相同的大小播放(它们都具有相同的大小)。

为了更清楚,我有我的程序的两个快照。

我希望第二个视频(较小的)与第一个视频播放相同的视频。或者,如果视频大于 Widget,则必须将其缩小到标签的大小,一种拉伸和收缩或其他方式。

您可以在两个视频中看到我的小部件的边框。

任何解决方案将不胜感激。提前致谢

第一个视频(普通视频)

这是第二个视频(小)

4

2 回答 2

3

myLabel.setScaledContents(true)应该这样做。

于 2012-06-01T06:40:12.393 回答
1

我在我的代码中发现了问题。我在不知不觉中调整了我不应该这样做的标签的大小。删除那行代码后,我解决了我的问题。

我的代码如下:

 std::string fileName = QFileDialog::getOpenFileName(this,tr("Open Video"), ".",tr("Video Files (*.mp4 *.avi)")).toStdString();

 cv::VideoCapture capture(fileName);

 if (!capture.isOpened())
 {
     QMessageBox msgBox;
     msgBox.setWindowTitle("Error");
     msgBox.setIcon(QMessageBox::Critical);
     msgBox.setButtonText(2,"OK");
     msgBox.setText("The video was not loaded!");
     msgBox.exec();
 }

 Mat cur_frame, pre_frame; // current video frame

 capture.read(imgsize);

 cv::Mat out(imgsize.rows,imgsize.cols,CV_8SC1);
 cv::Mat out2(imgsize.rows,imgsize.cols,CV_8SC1);

     Mat cur_frame_gray(imgsize.rows,imgsize.cols,CV_8SC1);

if (!paused) {
         // read next frame if any
         if (!capture.read(cur_frame)) break;

         cvtColor(cur_frame,cur_frame_gray,CV_RGB2GRAY);

         QCoreApplication::processEvents();

         //read second frame if any
         if(!capture.read(pre_frame)) break;
         cvtColor(pre_frame,pre_frame_gray,CV_RGB2GRAY);

         QCoreApplication::processEvents();

         Mat resizedframe1,resizedframe2,resizedframe3,resizedframe4; // to contain resized image

         cvtColor(cur_frame,cur_frame,CV_BGR2RGB);

        // here I am dividing the rows and columns of the frames by 2 to make them smaller. 
         cv::resize(cur_frame,resizedframe1,cv::Size(cur_frame.cols/2,cur_frame.rows/2));

         IplImage  myFrame=resizedframe4;

        Mat matFrame=&myFrame;

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

         //display on label
         //the label stretches and shrinks according to the frame of the video.

         ui->label->setScaledContents(true);
         ui->label->setPixmap(QPixmap::fromImage(img1));

}
// resize the label to fit the image
         // The problem was here, I was resizing the label but I shouldn't have to do it.
         // by removing the following line of code, my problem was solved :)
           ui->label->resize(ui->label->pixmap()->size());

         QCoreApplication::processEvents();

         waitKey(delay);

 }

         // Close the video file
         capture.release();

}
于 2012-06-01T14:49:55.333 回答