I am creating an application with Qt 5, in C++. I want to let the user choose a file, and then write the (absolute) path of the chosen file in a QLineEdit
widget.
This path will be then read by other widgets, in order to process the chosen file.
I have managed to connect a button to the QFileDialog::getOpenFileName
, using a slot-event-connect approach, so that pressing the button, the "Choose a file" window appears, and I managed to save the path in a QString
variable.
I have doubts on what to do next. Should I use another event-slot-connect? But what would be the event? the closure of the QFileDialog::getOpenFileName window
?
I like the idea of using the QLineEdit
widget as a 'global variable', because the user can also paste the path manually, but I am open to other solutions.
Is the event-slot-connect approach a good option?
I have previous experience with PyQT, used in Python (and I have already realized the application in Python, with a callback function), but I am rather new to C++. In particular, I struggle to learn the proper ways to pass parameters between objects.
This is what I have done so far:
myApp.h
#ifndef MYAPP_H
#define MYAPP_H
#include <QApplication>
#include <QWidget>
#include <QGridLayout>
#include <QPushButton>
#include <QSize>
#include <QFileDialog>
#include <QString>
#include <QLineEdit>
class MainWindow : public QWidget {
public:
MainWindow(QWidget *parent = nullptr);
private slots:
void SelectInput();
};
#endif /* MYAPP_H */
myApp.cpp
#include "myApp.h"
MainWindow::MainWindow(QWidget *parent) // questa è la definizione del costruttore
: QWidget(parent) {
setWindowTitle("Frames extraction");
auto *layout = new QGridLayout(this);
auto *inputbox = new QLineEdit(this);
layout->addWidget(inputbox, 0, 0);
inputbox->setFixedSize(QSize(800, 25));
auto *inputBtn = new QPushButton("select input");
layout->addWidget(inputBtn, 0, 2);
connect(inputBtn, &QPushButton::clicked, this, &MainWindow::SelectInput);
}
void MainWindow::SelectInput()
{
QString inputFile = QFileDialog::getOpenFileName(this, "Select a file...");
}
main.cpp
#include "myApp.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow window;
window.resize(600, 350);
window.show();
return app.exec();
}
There are a couple of problems in the code reported in the question:
The definition of the MainWindow::SelectInput()
slot function in the myApp.cpp file should create a QDialog instance, not just call it, and then should contain also a 'connect()' which connects this dialog with the QLineEdit widget,
The QLineEdit
widget should be declared in the header, and not in the creator, so that it is global to the class, and can be reached by the 'connect()' instance mentioned in the previous point.
So, those are the amended versions of the three files, that solve the problem and give the required functionality:
myApp.h
#ifndef MYAPP_H
#define MYAPP_H
#include <QApplication>
#include <QWidget>
#include <QGridLayout>
#include <QPushButton>
#include <QSize>
#include <QFileDialog>
#include <QString>
#include <QLineEdit>
class MainWindow : public QWidget {
public:
MainWindow(QWidget *parent = nullptr);
QLineEdit *inputbox;
private slots:
void SelectInput();
};
#endif /* MYAPP_H */
myApp.cpp
#include "myApp.h"
MainWindow::MainWindow(QWidget *parent) // questa è la definizione del costruttore
: QWidget(parent) {
setWindowTitle("Frames extraction");
auto *layout = new QGridLayout(this);
inputbox = new QLineEdit(this);
layout->addWidget(inputbox, 0, 0);
inputbox->setFixedSize(QSize(800, 25));
auto *inputBtn = new QPushButton("select input");
layout->addWidget(inputBtn, 0, 2);
connect(inputBtn, &QPushButton::clicked, this, &MainWindow::SelectInput);
}
void MainWindow::SelectInput()
{
auto *theDialog = new QFileDialog(this);
connect(theDialog, &QFileDialog::fileSelected, inputbox,
&QLineEdit::setText);
theDialog->exec();
}
main.cpp
#include "myApp.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
MainWindow window;
window.resize(600, 350);
window.show();
return app.exec();
}