[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
    doThisOtherThing

    Any tips in the right direction? I've scoured the documentation and have come up empty.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You can use isValid from QModelIndex



  • @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.



  • @mar0029 Get the index with the index()method : http://doc.qt.io/qt-5/qfilesystemmodel.html#index-1 I think



  • @raf924

    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?



  • @mar0029 I'm not sure what you mean. This ?

    QString QFileSystemModel::filePath(const QModelIndex & index) const
    Returns the path of the item stored in the model under the index given.
    

  • Lifetime Qt Champion

    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


  • Lifetime Qt Champion

    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 ?



  • @SGaist

    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.


  • Lifetime Qt Champion

    Then you use QListView::selectedIndexes to retrieve the current selection and work with that directly


  • Lifetime Qt Champion

    By the way setting the Qt::WA_DeleteOnClose attribute on the widget will be cleaner



  • @SGaist
    Thank you so much for your replies.
    After much ado, we've solved the problem!

    I had to do

    QModelIndexList test = ui->listView->selectionModel()->selectedIndexes();
    if(test.empty() == true)
    {
    ...
    }
    

    Thank you again everyone!


  • Lifetime Qt Champion

    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 :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.