Qt standard dialogs example: Open file
Asked Answered
P

1

9

I'm beginning to learn Qt for use in one of my projects, and what I need to do is create a GUI that allows the user to open a file. I was looking through the examples and I found one of them that has exactly what I need; the problem is that it's also got a whole lot of other stuff, to the point where I have no idea what I'm looking at or what I'm looking for.

Basically, my question is this:

How do I make what you can see below in the image, where after clicking on the button and selecting the relevant file, it shows and stores the file path in the box to the right? enter image description here

I have already figured out how to make the button open the file dialog, my only problem is getting it to store and display the filepath.

Presswork answered 17/1, 2013 at 4:0 Comment(1)
Better show the code, which opens the dialog, and the code which handles dialog closing (assuming you do nor open it with exec(), but you probably should use exec() for this case).Footprint
P
18

Solved it with this:

void OpenXMLFile::on_File1Button_clicked()
{
    file1Name = QFileDialog::getOpenFileName(this,
         tr("Open XML File 1"), "/home", tr("XML Files (*.xml)"));
    ui->File1Path->setText(file1Name);

}

void OpenXMLFile::on_File2Button_clicked()
{
    file2Name = QFileDialog::getOpenFileName(this,
         tr("Open XML File 2"), "/home", tr("XML Files (*.xml)"));
    ui->File2Path->setText(file2Name);

}

Where this is my GUI:

enter image description here

(The boxes next to the buttons are Line Edits if anyone was wondering)

Presswork answered 17/1, 2013 at 5:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.