C++ 如何将QFile的参数传递给QT上的其他函数?
我有这段代码,我想把在“void main window::on_button_2_clicked()”函数中选择的文件的目录传递给“char*videoName”。我该怎么做?C++ 如何将QFile的参数传递给QT上的其他函数?,c++,qt,char,qstring,qfile,C++,Qt,Char,Qstring,Qfile,我有这段代码,我想把在“void main window::on_button_2_clicked()”函数中选择的文件的目录传递给“char*videoName”。我该怎么做? 我不知道怎么做,请帮帮我!!!!一, #include "mainwindow.h" #include "ui_mainwindow.h" #include <opencv2/opencv.hpp> #include <opencv2/highgui.hpp> #include <open
我不知道怎么做,请帮帮我!!!!一,
#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.h”
#包括“ui_main window.h”
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
使用名称空间cv;
字符arg1;
int stop;
主窗口::主窗口(QWidget*父窗口):
QMainWindow(父级),
用户界面(新用户界面::主窗口)
{
用户界面->设置用户界面(此);
}
MainWindow::~MainWindow()
{
删除用户界面;
}
void主窗口::在按钮上单击()
{
字符*视频名称=???
脚垫框架,温文尔雅的形象;
矢量圆;
namedWindow(“视频处理”,CV\u窗口\u自动调整大小);
视频捕获(videoName);
如果(!capture.isOpened())
printf(“1”);
bool-stop(假);
//康迪��o de parada se usu�里约热内卢执行委员会��o
双帧速率=capture.get(CV_CAP_PROP_FPS);//四角体的Obter分类群�处理程序
整数延迟=1000/帧速率;
//cout有很多方法:
您在同一个“mainwindow”类中使用了该变量
同样基于函数名,我假设您试图在另一个插槽中使用“videoname”
所以只需将“videoname”声明为类私有变量。
并使用“qStr.toLatin1().data()
”将文件名/目录名分配给“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);
}
谢谢,它可以编译,但不起作用。我只想将我选择的文件的方式发送到namevideo变量。例如“home/pi/video.avi”。你知道吗?你的意思是完整的文件路径吗?上面的代码给出了完整的路径。你想修剪它并只发送“home/pi/video.avi”吗?如果是这样,请调用“filename=filename.mid(filename.indexOf(“home/pi”),filename.length()+1);“在分配给_videoname之前,您是对的,我需要完整的路径,但我不知道为什么它不起作用。