[SOLVED] How to see if file is selected in list view with QFileSystemModel
-
Hello!
I have created a simple file viewer. It consists of a QTreeView as the directory viewer and a QListView as the file viewer. Both views use the QFileSystemModel as their model. I want to be able to check if a folder has been selected or not in the QListView.
I've tried things like
-
if(ui->listView->currentIndex()) == (-1)), which gives a 'no match for operator ==' error
-
different connect statements that check for file types of the QFileSystemModel
I really want to be able to write an if... else... statement. The psuedocode being :
if(noFileSelected)
modalDialog for error
else
doThisOtherThingAny tips in the right direction? I've scoured the documentation and have come up empty.
-
-
@SGaist
Thank you for your reply!! A quick question:
How would I go about implementing QModelIndex::isValid() ? I can't call isValid without a QModelIndex object and I can't redefine my QFileSystemModel object as a QModelIndex.Any tips would be greatly appreciated.
-
Thank you for your reply!
I am able to use "myModelIndex.isValid()" as a boolean value now but only if I set
myModelIndex = myModel->index(myFilePath,0)
where in index(), QString myFilePath = "C:/" and the integer is an arbitrary column value.
I know I need myFilePath to be that of the index value in my signal function void QAbstractItemView::clicked(const QModelIndex & index). Question is, how do I go about doing that? -
Before going further, can you post the code you are currently using ? That way we are all on the same page about what you are trying to achieve and how.
-
@raf924
@SGaist
Here are the two functions of concern.void MainWindow::on_dirView_clicked(const QModelIndex &index) { QString sPath = dirModel->fileInfo(index).absoluteFilePath(); ui->listView->setRootIndex(listModel->setRootPath(sPath)); } void MainWindow::loadButton() { //index = new QModelIndex(this); //qDebug() << "Load Button Test" << endl; //testing purposes. // index = new QModelIndex(this); //QString sPath = listModel->filePath(index); QString sPath = "C:/"; QModelIndex test = listModel->index(sPath,0); if (test.isValid() == false) { QDialog* loadMessage = new QDialog; loadMessage->setFixedSize(250,150); QVBoxLayout *vertLayout = new QVBoxLayout; QLabel *messageLabel = new QLabel("No File was selected. Please select a .dat file."); QPushButton *messageOKButton = new QPushButton("OK"); vertLayout->addWidget(messageLabel); vertLayout->addWidget(messageOKButton); loadMessage->setLayout(vertLayout); connect(messageOKButton, SIGNAL(clicked(bool)), loadMessage, SLOT(accept())); loadMessage->exec(); } else { ui->mpfTAB->setCurrentIndex(1); } }
aside: Thank you for your help!!
edit(s): figured out formatting -
You have a memory leak, you're not deleting loadMessage.
So if I understand you correctly, when you call loadButton, you would like to act on the currently selected item from listModel, right ?
-
Whoops. I'll do a
connect(loadMessage, SIGNAL(finished(int)), loadMessage, SLOT(deleteLater)));
to clean up my leak.
That is correct. Really, there should never be an invalid file type because I specified the file type in MainWindow with
listModel->setNameFilters(matFilters);
where matFilters is specified as.dat
. So all I really need to see is if any selection has been made in listView. -
Then you use QListView::selectedIndexes to retrieve the current selection and work with that directly
-
By the way setting the Qt::WA_DeleteOnClose attribute on the widget will be cleaner
-
You're welcome !
By the way, now you can use the "Topic Tool" button to mark the thread as solved, it keeps the things cleaner :)