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. Assign QStringList model object to listview
Forum Updated to NodeBB v4.3 + New Features

Assign QStringList model object to listview

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 4 Posters 840 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 everyone,

    I have created a class :
    fili .h

    #ifndef LISTVIEW_H
    #define LISTVIEW_H
    
    #include <QWidget>
    #include <QDialog>
    #include <QStringListModel>
    #include <QStringList>
    #include <QAbstractItemView>
    #include <QListView>
    
    class listview : QDialog
    {
    public:
        listview(const QStringList &leaders, QWidget *parent);
        void insert();
        void del();
        QStringList leaders() const;
    
    private:
        QListView *listView;
        QStringListModel *model;
    
    
    };
    
    

    .cpp file

    #include "listview.h"
    
    listview::listview(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 listview::insert(){
        int row = listView->currentIndex().row();
       
        model->insertRows(row,1);
        QModelIndex index = model->index(row);
        listView->setCurrentIndex(index);
    }
    
    void listview::del(){
        model->removeRows(listView->currentIndex().row(),1);
    
    
    }
    QStringList listview::leaders() const{
    
        return leaders();
    
    }
    
    

    Know in my view I have a QListWidget, with 3 buttons Add, delete and insert buttons, I want them to run with methods of my class, then in my Mainwindow I should instantiate an object of this class, but I don't know how to link my object to my QListView, can someone help me?

    Thanks

    jsulmJ VRoninV 2 Replies Last reply
    0
    • J jss193

      Hello everyone,

      I have created a class :
      fili .h

      #ifndef LISTVIEW_H
      #define LISTVIEW_H
      
      #include <QWidget>
      #include <QDialog>
      #include <QStringListModel>
      #include <QStringList>
      #include <QAbstractItemView>
      #include <QListView>
      
      class listview : QDialog
      {
      public:
          listview(const QStringList &leaders, QWidget *parent);
          void insert();
          void del();
          QStringList leaders() const;
      
      private:
          QListView *listView;
          QStringListModel *model;
      
      
      };
      
      

      .cpp file

      #include "listview.h"
      
      listview::listview(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 listview::insert(){
          int row = listView->currentIndex().row();
         
          model->insertRows(row,1);
          QModelIndex index = model->index(row);
          listView->setCurrentIndex(index);
      }
      
      void listview::del(){
          model->removeRows(listView->currentIndex().row(),1);
      
      
      }
      QStringList listview::leaders() const{
      
          return leaders();
      
      }
      
      

      Know in my view I have a QListWidget, with 3 buttons Add, delete and insert buttons, I want them to run with methods of my class, then in my Mainwindow I should instantiate an object of this class, but I don't know how to link my object to my QListView, can someone help me?

      Thanks

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @jss193

      class MainWindow...
      {
      private:
          listview *view;
      }
      ...
      MainWindow::MainWindow(...)
      {
          view = new listview();
          connect(ui->insertButton, &QPushButton::clicked, view, &listview::insert);
          // same for other 2 buttons
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply
      0
      • J jss193

        Hello everyone,

        I have created a class :
        fili .h

        #ifndef LISTVIEW_H
        #define LISTVIEW_H
        
        #include <QWidget>
        #include <QDialog>
        #include <QStringListModel>
        #include <QStringList>
        #include <QAbstractItemView>
        #include <QListView>
        
        class listview : QDialog
        {
        public:
            listview(const QStringList &leaders, QWidget *parent);
            void insert();
            void del();
            QStringList leaders() const;
        
        private:
            QListView *listView;
            QStringListModel *model;
        
        
        };
        
        

        .cpp file

        #include "listview.h"
        
        listview::listview(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 listview::insert(){
            int row = listView->currentIndex().row();
           
            model->insertRows(row,1);
            QModelIndex index = model->index(row);
            listView->setCurrentIndex(index);
        }
        
        void listview::del(){
            model->removeRows(listView->currentIndex().row(),1);
        
        
        }
        QStringList listview::leaders() const{
        
            return leaders();
        
        }
        
        

        Know in my view I have a QListWidget, with 3 buttons Add, delete and insert buttons, I want them to run with methods of my class, then in my Mainwindow I should instantiate an object of this class, but I don't know how to link my object to my QListView, can someone help me?

        Thanks

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        @jss193 said in Assign QStringList model object to listview:

        Know in my view I have a QListWidget, with 3 buttons Add

        • QListView != QListWidget.
        • you never give a parent, add to a layout or show your view so I'm not sure how the list view is displayed at all

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • jsulmJ jsulm

          @jss193

          class MainWindow...
          {
          private:
              listview *view;
          }
          ...
          MainWindow::MainWindow(...)
          {
              view = new listview();
              connect(ui->insertButton, &QPushButton::clicked, view, &listview::insert);
              // same for other 2 buttons
          }
          
          J Offline
          J Offline
          jss193
          wrote on last edited by
          #4

          Hello, thanks for your help, but problem remains still the same,
          I think that I did not explain good enough, I have created that generic class shown above and afterwards I created a second view which prompts from a first ui, this second ui view contains a listView Widget whose I want to glue with my base class listview, so, First thing I tried was to create an object listview *aaaa;, then tried this, ui->listView->setModel(model), but I can't cause of model var from listview class is private.

          How could I do this?

          thanks!

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

            Hi,

            Then why not make the model a parameter of your dialog ? That way you can more easily share it between several classes.

            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
              #6

              You mean creating a new model for my second view and pass it to my listviewclass constructor instead of making it a private var of listview class?

              Thanks

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

                I mean creating one model and use it for both dialogs.

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

                J 1 Reply Last reply
                0
                • SGaistS SGaist

                  I mean creating one model and use it for both dialogs.

                  J Offline
                  J Offline
                  jss193
                  wrote on last edited by
                  #8

                  @SGaist you mean remove it from class and create the model in main function, true? Its a good idea. Thanks

                  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