如何将参数从 QFile 传递给 QT 上的其他函数?

How can I pass arguments off a QFile to other function on QT?

我有这段代码,我想将 "void MainWindow::on_pushButton_2_clicked() " 函数中选择的文件的目录传递给“ char* videoName ”。我该怎么做?
我不知道该怎么做,请帮助我!!!!1

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>
#include <iostream>
#include <QFileDialog>
#include <QMessageBox>
using namespace std;
using namespace cv;
char arg1;
int stop;

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{



ui->setupUi(this);


}

MainWindow::~MainWindow()
{

delete ui;
}

void MainWindow::on_pushButton_clicked()
{
   char* videoName = ???
   Mat FrameAtual, suave_image;
   vector<Vec3f> circles;

   namedWindow("Video Processado", CV_WINDOW_AUTOSIZE);

   VideoCapture capture(videoName);
   if (!capture.isOpened())
       printf ("1");

           bool stop(false);

                                                                             //Condi��o de parada se usu�rio pressionar alguma tecla durante execu��o

   double FrameRate = capture.get(CV_CAP_PROP_FPS);                                   //Obter taxa de quadros do v�deo a ser processado
   int Delay = 1000 / FrameRate;
   //cout << "Delay = " << Delay;

   while (!stop) {
       if (!capture.read(FrameAtual))                                                   //L� pr�ximo frame, se existir; se n�o existe sai da execu��o
           break;

       cvtColor(FrameAtual, FrameAtual, CV_BGR2GRAY);

       GaussianBlur(FrameAtual, suave_image, Size(5, 5), 1.0);
       HoughCircles(suave_image, circles, CV_HOUGH_GRADIENT, 2, 230, 120,    100, 25, 80);
       vector<Vec3f>::const_iterator itc = circles.begin();
       while (itc != circles.end()) {
           circle(FrameAtual, Point((*itc)[0], (*itc)[1]), (*itc)[2],  Scalar(255), 2);
           ++itc;
       }

       imshow("Video Processado", FrameAtual);

       if (cv::waitKey(Delay) >= 0)                                                 //Introduz delay antes de passar ao prox frame ou interrompe se usu�rio pressionar tecla
           stop = true;
   }






}









void MainWindow::on_pushButton_2_clicked()
{
QString filename=QFileDialog::getOpenFileName(

            this,
            tr("Open File"),
            "/home/pi",
            "All Files (*.*);;"

            );
QMessageBox::information(this,tr("File Name"),filename);



}

方法有很多种:

您正在使用同一个 "mainwindow" class 中的变量。

而且根据函数名称,我假设您正在尝试在另一个插槽中使用 "videoname"。

所以只需将 "videoname" 声明为 class 私有变量即可。

并使用“qStr.toLatin1().data()

将 filename/dirctory 名称分配给您的“char*

伪代码如下:

  void MainWindow::on_pushButton_2_clicked()
    {

    //if you want file name
 QString filename  = QFileDialog::getOpenFileName(

                this,
                tr("Open File"),
                "/home/pi",
                "All Files (*.*);;"

                );

    _videoname = filename.toLatin1().data(); //_videoname (const char*) is your class private variable. you can use any where with in the class.


    //Similary if you want directory name 

    QString dir = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
                                                    "/home/pi",
                                                    QFileDialog::ShowDirsOnly
                                                    | QFileDialog::DontResolveSymlinks);


    _dirname = dir.toLatin1().data();//_dirname (const char*) is your class private variable. you can use any where in the class.


    QMessageBox::information(this,tr("File Name"),filename);

    }