Solved QListView with file navigation
-
Hi! I am trying to implement a small file browser embedded in my application GUI with the QListView widget. I have successfully managed to display the contents of a predefined folder and filter only the XML type files I am interested in. However, I'd like to give users a choice of changing the root folder from within that list view. Is it possible to add folder navigation inside the widget? What would I need to add to my listview->doubleclicked slot?
Here's my code:
void MeasurementModePage::setupListView() { QString protocolsPath = "D:/"; qDebug() << QCoreApplication::applicationFilePath(); qDebug() << QCoreApplication::applicationDirPath(); qDebug() << protocolsPath; listmodel = new QFileSystemModel(this); listmodel->setRootPath(protocolsPath); QStringList filters; filters << "*.xml"; listmodel->setFilter(QDir::NoDotAndDotDot | QDir::Files); // To be removed in case folders have to be also displayed listmodel->setNameFilters(filters); listmodel->setNameFilterDisables(false); listmodel->sort(0, Qt::AscendingOrder); ui->listView_measurementFiles->setModel(listmodel); ui->listView_measurementFiles->setRootIndex(listmodel->index(protocolsPath)); connect(this, &MeasurementModePage::settingsChanged, this, &MeasurementModePage::onSettingsLoad); // Context menu ui->listView_measurementFiles->setContextMenuPolicy(Qt::CustomContextMenu); connect(ui->listView_measurementFiles, &QWidget::customContextMenuRequested, this, &MeasurementModePage:: showCustomContextMenu); } void MeasurementModePage::on_listView_measurementFiles_doubleClicked(const QModelIndex &index) { // Load an XML settings file if the index selected is a file if(listmodel->fileInfo(index).isFile()) { // Load settings with external LIB emit settingsChanged(); } }
On a side note: I would like the root path to be set to a folder where my application is built, where I will place some XML files for the users to choose from. Does it make sense to read it using "QCoreApplication::applicationDirPath();"?
-
Hi,
It depends on the target platform and how you will install your application.
How far would your users be allowed to roam for these xml files ?
Depending on that, a tree view might be better suited.
-
@SGaist I am thinking to give users a freedom of choice. The application would always launch showing the default root path, but they should be allowed to choose any other directory. I will only do the filtering and display XML files exclusively. I'll try with a tree view today and see if this achieves that goal.
Regarding the applitacion directory - the target platform is Windows and for now, the application will come without an installer - just a compiled version to work with.
-
@szumial said in QListView with file navigation:
Regarding the applitacion directory - the target platform is Windows and for now, the application will come without an installer - just a compiled version to work with.
Then you should be good.
-
I ended up implementing a QListView and added a tool button to allow users select any directory with a QFileDialog. Since I intend to make the program easy to use, I don't want them to change the root path frequently. I will place the XML files in a specific directory and the ability to change the folder displayed is just an additional functionality. Thanks for the suggestions!