Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] How to see if file is selected in list view with QFileSystemModel
QtWS25 Last Chance

[SOLVED] How to see if file is selected in list view with QFileSystemModel

Scheduled Pinned Locked Moved General and Desktop
qfilesystemmodeqlistviewqtreeview
14 Posts 3 Posters 8.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mar0029
    wrote on 2 Nov 2015, 21:04 last edited by mar0029 11 Jun 2015, 14:30
    #1

    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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 2 Nov 2015, 22:42 last edited by
      #2

      Hi and welcome to devnet,

      You can use isValid from QModelIndex

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 4 Nov 2015, 14:02
      0
      • S SGaist
        2 Nov 2015, 22:42

        Hi and welcome to devnet,

        You can use isValid from QModelIndex

        M Offline
        M Offline
        mar0029
        wrote on 4 Nov 2015, 14:02 last edited by
        #3

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

        R 1 Reply Last reply 4 Nov 2015, 15:14
        0
        • M mar0029
          4 Nov 2015, 14:02

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

          R Offline
          R Offline
          raf924
          wrote on 4 Nov 2015, 15:14 last edited by
          #4

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

          M 1 Reply Last reply 4 Nov 2015, 16:41
          0
          • R raf924
            4 Nov 2015, 15:14

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

            M Offline
            M Offline
            mar0029
            wrote on 4 Nov 2015, 16:41 last edited by
            #5

            @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?

            R 1 Reply Last reply 4 Nov 2015, 16:56
            0
            • M mar0029
              4 Nov 2015, 16:41

              @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?

              R Offline
              R Offline
              raf924
              wrote on 4 Nov 2015, 16:56 last edited by
              #6

              @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.
              
              M 1 Reply Last reply 4 Nov 2015, 17:27
              0
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 4 Nov 2015, 17:18 last edited by
                #7

                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.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • R raf924
                  4 Nov 2015, 16:56

                  @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.
                  
                  M Offline
                  M Offline
                  mar0029
                  wrote on 4 Nov 2015, 17:27 last edited by mar0029 11 Apr 2015, 19:41
                  #8

                  @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

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 4 Nov 2015, 20:32 last edited by
                    #9

                    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 ?

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M 1 Reply Last reply 4 Nov 2015, 20:42
                    0
                    • S SGaist
                      4 Nov 2015, 20:32

                      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 ?

                      M Offline
                      M Offline
                      mar0029
                      wrote on 4 Nov 2015, 20:42 last edited by
                      #10

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

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 4 Nov 2015, 21:09 last edited by
                        #11

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

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        M 1 Reply Last reply 6 Nov 2015, 14:30
                        0
                        • S Offline
                          S Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on 4 Nov 2015, 21:28 last edited by
                          #12

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

                          Interested in AI ? www.idiap.ch
                          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          1
                          • S SGaist
                            4 Nov 2015, 21:09

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

                            M Offline
                            M Offline
                            mar0029
                            wrote on 6 Nov 2015, 14:30 last edited by
                            #13

                            @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!

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 6 Nov 2015, 20:16 last edited by
                              #14

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

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              0

                              5/14

                              4 Nov 2015, 16:41

                              9 unread
                              • Login

                              • Login or register to search.
                              5 out of 14
                              • First post
                                5/14
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved