문제

I'm a beginner in all this stuff. I'm trying to make a function that opens files (using Qt in windows), I tried some functions from QProcess library but they are unable to fit in my needs.So, I decided to use system() function to execute files.The problem is system function only opening .exe files and other files from my system drive and is not opening any file from any other drive.Is there a built-in function in Qt that I can use to open any file with default program assigned for that file type. Why is this happening.What am I doing wrong?
My code:

    QString FilePath = openFileDialog.getOpenFileName(this, tr("Open File"),"/home",tr("All Files"));
ui->Label_7->setText("Choose file to open.");
const char *file;
QByteArray bArray;
bArray = FilePath.toLatin1();
file = bArray.data();

system(file);
도움이 되었습니까?

해결책

You can use the QDesktopServices::openUrl function to open local files with a suitable application. Try this:

void Widget::open()
{
    QString filename = QFileDialog::getOpenFileName();
    if (!filename.isEmpty())
    {
        QUrl url = QUrl::fromLocalFile(filename);

        QDesktopServices::openUrl(url);
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top