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. QListView issue
Qt 6.11 is out! See what's new in the release blog

QListView issue

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 3 Posters 2.7k 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.
  • J Offline
    J Offline
    jss193
    wrote on last edited by
    #1

    Hello all,

    I am trying to show a QstringList into a ListView, first I create a model Object from class QStringListModel and do a setStringList(listname)
    then I apply the following code:

    listView = new QListView;
       listView->setModel(model);
       listView->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);
    
    

    But it says that cannot initialize a parameter of type QAbstractItemModel with an lvalue of type QStringlistModel

    Can someone help me in solving this?

    Tnaks

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      QListView::setModel() takes a pointer.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • J Offline
        J Offline
        jss193
        wrote on last edited by
        #3

        I know, but even passing memory address of model it prompts the following issue “cannot initialize a parameter of type QAbstractItemModel with an lvalue of type QStringlistModel”

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          I don't think so - please show us your modified code including the definition of the model variable.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • J Offline
            J Offline
            jss193
            wrote on last edited by
            #5

            Here is the file.h

            class TeamDialog :QDialog
            {
            public:
                TeamDialog(const QStringList &leaders, QWidget *parent);
                void insert();
                void del();
                QStringList leaders() const;
            private:
                QStringListModel *model;
                QListView *listView;
            
            };
            

            and here de cpp

            
            TeamDialog::TeamDialog(const QStringList &leaders, QWidget *parent):QDialog(parent)
            {
            
               model = new QStringListModel(this);
               model->setStringList(leaders);
            
               listView = new QListView;
               listView->setModel(&model);
               listView->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);
               
               
            
            }
            
            void TeamDialog::insert(){
                int row = listView->currentIndex().row();
                model->insertRows(row,1);
            
                QModelIndex index = model->index(row);
                listView->setCurrentIndex(index);
                listView->edit(index);
            
            }
            
            void TeamDialog::del(){
                model->removeRows(listView->currentIndex().row(),1);
            }
            
            QStringList TeamDialog::leaders() const{
                return model->stringList();
            }
            
            Christian EhrlicherC 1 Reply Last reply
            0
            • J jss193

              Here is the file.h

              class TeamDialog :QDialog
              {
              public:
                  TeamDialog(const QStringList &leaders, QWidget *parent);
                  void insert();
                  void del();
                  QStringList leaders() const;
              private:
                  QStringListModel *model;
                  QListView *listView;
              
              };
              

              and here de cpp

              
              TeamDialog::TeamDialog(const QStringList &leaders, QWidget *parent):QDialog(parent)
              {
              
                 model = new QStringListModel(this);
                 model->setStringList(leaders);
              
                 listView = new QListView;
                 listView->setModel(&model);
                 listView->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);
                 
                 
              
              }
              
              void TeamDialog::insert(){
                  int row = listView->currentIndex().row();
                  model->insertRows(row,1);
              
                  QModelIndex index = model->index(row);
                  listView->setCurrentIndex(index);
                  listView->edit(index);
              
              }
              
              void TeamDialog::del(){
                  model->removeRows(listView->currentIndex().row(),1);
              }
              
              QStringList TeamDialog::leaders() const{
                  return model->stringList();
              }
              
              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @jss193 said in QListView issue:

              listView->setModel(&model);

              now you're passing a pointer to a pointer to a QStringListModel

              listView->setModel(model);

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • J Offline
                J Offline
                jss193
                wrote on last edited by
                #7

                I tried what you say and prompts the same issue as before “cannot initialize a parameter of type QAbstractItemModel with an lvalue of type QStringlistModel”.

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by Christian Ehrlicher
                  #8
                  #include <QtWidgets>
                  
                  int main(int argc, char **argv)
                  {
                      QApplication app(argc, argv);
                  
                      QStringListModel *model = new QStringListModel;
                      QListView *listView = new QListView;
                      listView->setModel(model);
                  }
                  

                  This compile fine for me - there must be something wrong with your headers - maybe forgot to include QStringListModel oder QListView header file?

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    jss193
                    wrote on last edited by
                    #9

                    Thanks it works it prompts the same issue but compiles well, then how would I pass an object of that class to a QListviewWidget?

                    Thank you very much

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Hi,

                      Do you mean you are trying to set a QStringList model on a QListWidget ? If so, you can't. The convenience views like QListWidget or QTreeWidget already have a model and it can't be changed.

                      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
                      • J Offline
                        J Offline
                        jss193
                        wrote on last edited by
                        #11

                        Thanks , then I should instantiate the class in main window through constructor method, true?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          That's unrelated, if you want to use a QStringListModel, then use a QListView and you should be good to go.

                          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
                          • J Offline
                            J Offline
                            jss193
                            wrote on last edited by
                            #13

                            Thanks, thats just what I meant

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Here is your code modified to work:
                              @jss193 said in QListView issue:

                              model = new QStringListModel(this);
                              model->setStringList(leaders);

                              listView = new QListView;
                              listView->setModel(model);
                              listView->setEditTriggers(QAbstractItemView::AnyKeyPressed | QAbstractItemView::DoubleClicked);

                              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

                              • Login

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