Question

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);
Was it helpful?

Solution

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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top