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
Forum Updated to NodeBB v4.3 + New Features

[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.5k Views 1 Watching
  • 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on 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
    0
    • SGaistS SGaist

      Hi and welcome to devnet,

      You can use isValid from QModelIndex

      M Offline
      M Offline
      mar0029
      wrote on 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
      0
      • M mar0029

        @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 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
        0
        • R raf924

          @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 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
          0
          • M mar0029

            @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 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
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on 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

                @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 last edited by mar0029
                #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
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • SGaistS SGaist

                    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 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
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 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
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 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
                        • SGaistS SGaist

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

                          M Offline
                          M Offline
                          mar0029
                          wrote on 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
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 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

                            • Login

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