Alex Encore
Alex Encore

Reputation: 309

Qt - Load an image from the computer in GUI

I'm developing a gui application in QtCreator and what the gui should do is this:

Upon clicking on the Open Image button, I should be able to browse my computer to find an image file and load it on the ui window.

This is how the window looks like so far:

enter image description here

A pop up dalog box with a windows-like browser would be great.

I'm not showing any of my code because basically it's the initial source files generated when I create a Gui Aplication.

Edit:

I've managed to create a dialog box to get an image from the computer by applying an action listener to the button and using the following block of code:

void MainWindow::on_pushButton_clicked()
{
    //MyDialog mDialog;
    //mDialog.setModal(true);
    //mDialog.exec();
    QFileDialog dialog(this);
    dialog.setNameFilter(tr("Images (*.png *.xpm *.jpg)"));
    dialog.setViewMode(QFileDialog::Detail);
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
                                                    "C:/",
                                                    tr("Images (*.png *.xpm *.jpg)"));

}

I am now trying to display the image I choose on the right side of the window. Any suggestions?

Upvotes: 2

Views: 9268

Answers (1)

Lucian
Lucian

Reputation: 3554

Please look at the QFileDialog. This shows the open/save file dialog for the system.

From the article:

The QFileDialog class provides a dialog that allow users to select files or directories.

The QFileDialog class enables a user to traverse the file system in order to select one or many files or a directory.

The easiest way to create a QFileDialog is to use the static functions. On Windows, Mac OS X, KDE and GNOME, these static functions will call the native file dialog when possible.

 fileName = QFileDialog::getOpenFileName(this,
     tr("Open Image"), "/home/jana", tr("Image Files (*.png *.jpg *.bmp)"));

Upvotes: 5

Related Questions