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. Data not displaying on the QTreeView, QTableView,QListView classes
QtWS25 Last Chance

Data not displaying on the QTreeView, QTableView,QListView classes

Scheduled Pinned Locked Moved Unsolved General and Desktop
21 Posts 3 Posters 3.6k 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.
  • T Offline
    T Offline
    thippu
    wrote on last edited by thippu
    #1

    Hi,

    1. I'm subclassing QAbstractTableModel class, called class Addressbookmodel and I'm populating the model.

    2. On QWidget, I'm creating QVBoxLayout and setting it as the main layout for the view and adding QTreeView,QTableView,QListView objects to it.

    3. file path and accessing are working fine.
      But problem is,

    4. QTreeView, QTableview created on a stack is not getting displayed on the widget(at least it should show the layout of the widget, right?, why object created on the heap are shown because I have created QListView on heap memory, it is shown on the widget with empty data from the model

    5. why the model data is not showing?

    code: widget.cpp

    Widget::Widget(QWidget *parent)
    : QWidget(parent)
    {
    QFile file("path");
    if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
    qDebug()<<"can't access the file";
    }
    QString addresses=QString::fromUtf8(file.readAll());
    Addressbookmodel model(addresses);
    
    QListView *listView=new QListView();
    listView->setModel(&model);
    listView->setModelColumn(0);
    listView->show();
    
    QTreeView treeView;
    treeView.setModel(&model);
    treeView.show();
    
    QTableView tableView;
    tableView.setModel(&model);
    tableView.show();
    
    setMiniumSize(800,400);
    QVBoxLayout *Vlayout=new QVBoxLayout(this);
    Vlayout->addWidget(listView);
    Vlayout->addWidget(&treeView);
    Vlayout->addWidget(&tableView);
    }
    

    csv file:
    'Name', 'Address'
    'thippu', 'talacaverlayout'

    VRoninV JonBJ 2 Replies Last reply
    0
    • T thippu

      Hi,

      1. I'm subclassing QAbstractTableModel class, called class Addressbookmodel and I'm populating the model.

      2. On QWidget, I'm creating QVBoxLayout and setting it as the main layout for the view and adding QTreeView,QTableView,QListView objects to it.

      3. file path and accessing are working fine.
        But problem is,

      4. QTreeView, QTableview created on a stack is not getting displayed on the widget(at least it should show the layout of the widget, right?, why object created on the heap are shown because I have created QListView on heap memory, it is shown on the widget with empty data from the model

      5. why the model data is not showing?

      code: widget.cpp

      Widget::Widget(QWidget *parent)
      : QWidget(parent)
      {
      QFile file("path");
      if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
      {
      qDebug()<<"can't access the file";
      }
      QString addresses=QString::fromUtf8(file.readAll());
      Addressbookmodel model(addresses);
      
      QListView *listView=new QListView();
      listView->setModel(&model);
      listView->setModelColumn(0);
      listView->show();
      
      QTreeView treeView;
      treeView.setModel(&model);
      treeView.show();
      
      QTableView tableView;
      tableView.setModel(&model);
      tableView.show();
      
      setMiniumSize(800,400);
      QVBoxLayout *Vlayout=new QVBoxLayout(this);
      Vlayout->addWidget(listView);
      Vlayout->addWidget(&treeView);
      Vlayout->addWidget(&tableView);
      }
      

      csv file:
      'Name', 'Address'
      'thippu', 'talacaverlayout'

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

      @thippu said in Data not displaying on the QTreeView, QTableView,QListView classes:

      QTreeView, QTableview created on a stack is not getting displayed

      Because they go out of scope and get deleted before they can get displayed. The same is true for your model

      "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
      4
      • T thippu

        Hi,

        1. I'm subclassing QAbstractTableModel class, called class Addressbookmodel and I'm populating the model.

        2. On QWidget, I'm creating QVBoxLayout and setting it as the main layout for the view and adding QTreeView,QTableView,QListView objects to it.

        3. file path and accessing are working fine.
          But problem is,

        4. QTreeView, QTableview created on a stack is not getting displayed on the widget(at least it should show the layout of the widget, right?, why object created on the heap are shown because I have created QListView on heap memory, it is shown on the widget with empty data from the model

        5. why the model data is not showing?

        code: widget.cpp

        Widget::Widget(QWidget *parent)
        : QWidget(parent)
        {
        QFile file("path");
        if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
        {
        qDebug()<<"can't access the file";
        }
        QString addresses=QString::fromUtf8(file.readAll());
        Addressbookmodel model(addresses);
        
        QListView *listView=new QListView();
        listView->setModel(&model);
        listView->setModelColumn(0);
        listView->show();
        
        QTreeView treeView;
        treeView.setModel(&model);
        treeView.show();
        
        QTableView tableView;
        tableView.setModel(&model);
        tableView.show();
        
        setMiniumSize(800,400);
        QVBoxLayout *Vlayout=new QVBoxLayout(this);
        Vlayout->addWidget(listView);
        Vlayout->addWidget(&treeView);
        Vlayout->addWidget(&tableView);
        }
        

        csv file:
        'Name', 'Address'
        'thippu', 'talacaverlayout'

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @thippu

        Addressbookmodel model(addresses);
        QTreeView treeView;
        QTableView tableView;
        

        Exactly as @VRonin says: all three of these need to be allocated on the heap (new ...) for them to persist once the code you show in your Widget constructor has exited. Or, you might get away with making them members (without new) of your Widget class itself, if that lifetime is sufficient. (As per @VRonin below, just don't try that way, stick to new!)

        VRoninV T 2 Replies Last reply
        3
        • JonBJ JonB

          @thippu

          Addressbookmodel model(addresses);
          QTreeView treeView;
          QTableView tableView;
          

          Exactly as @VRonin says: all three of these need to be allocated on the heap (new ...) for them to persist once the code you show in your Widget constructor has exited. Or, you might get away with making them members (without new) of your Widget class itself, if that lifetime is sufficient. (As per @VRonin below, just don't try that way, stick to new!)

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

          @JonB said in Data not displaying on the QTreeView, QTableView,QListView classes:

          Or, you might get away with making them members

          If you do you must be careful that they must go out of scope before the parent tries to call delete on them. Usually it's not worth the effort and it's just better to allocate widgets on the heap

          "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

          JonBJ 1 Reply Last reply
          1
          • VRoninV VRonin

            @JonB said in Data not displaying on the QTreeView, QTableView,QListView classes:

            Or, you might get away with making them members

            If you do you must be careful that they must go out of scope before the parent tries to call delete on them. Usually it's not worth the effort and it's just better to allocate widgets on the heap

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by JonB
            #5

            @VRonin
            That's why I said "might get away with". I know how fond you C++ people here are about using the stack instead of the heap if at all possible (whereas I am Python/PyQt and so just use the heap the whole time :) ), so I put that in in case someone complained, I did wonder about it. I'll just cross it through!

            T 1 Reply Last reply
            1
            • JonBJ JonB

              @thippu

              Addressbookmodel model(addresses);
              QTreeView treeView;
              QTableView tableView;
              

              Exactly as @VRonin says: all three of these need to be allocated on the heap (new ...) for them to persist once the code you show in your Widget constructor has exited. Or, you might get away with making them members (without new) of your Widget class itself, if that lifetime is sufficient. (As per @VRonin below, just don't try that way, stick to new!)

              T Offline
              T Offline
              thippu
              wrote on last edited by
              #6

              @JonB said in Data not displaying on the QTreeView, QTableView,QListView classes:

              . (As per @VRonin below, just don't try that way, stick to new!)

              Sure.

              1 Reply Last reply
              0
              • JonBJ JonB

                @VRonin
                That's why I said "might get away with". I know how fond you C++ people here are about using the stack instead of the heap if at all possible (whereas I am Python/PyQt and so just use the heap the whole time :) ), so I put that in in case someone complained, I did wonder about it. I'll just cross it through!

                T Offline
                T Offline
                thippu
                wrote on last edited by
                #7

                @JonB , @VRonin I want to learn model/view architecture, so I picked up the Book Qt 4 art of building to study and practice, went to 207 page for basic understanding and page 222 example I want to study and implement,

                1. They have told to store the .csv file format like this :
                  ’’title (column 1)’’, ’’title (column 2)’’, ...,’’title (column n)’’
                  ’’value’’, ’’value’’, ..., ’’value’’
                  ’’value’’, ’’value’’, ..., ’’value’’
                  ’’value’’, ’’value’’, ..., ’’value’’
                  if I store like this and do qDebug() on the QList object, which has all the values of lines, it is giving output "" "" so, I did change the format and stored like this with values :
                  csv file:
                  'Name', 'Address'
                  'thippu', 'talacaverlayout'
                  after this change, qDebug() did show file data.
                2. output and Ui
                  2)Not understanding why the data is not displaying on the Views.
                  code AddressbookGui:
                //csv file read method.
                QStringList splitCSVLine(const QString& line)
                {
                bool inItem = false;
                QStringList items;
                QString item;
                for (int pos = 0; pos < line.length(); pos++)
                {
                QChar c=line.at(pos);
                if ( c == ’\’’) {
                if (inItem) {
                items.append(item);
                item = "";
                }
                inItem = !inItem;
                }
                else
                if (inItem) {
                item += c;
                }
                }
                return items;
                }
                
                //constructor
                AddressbookModel::AddressbookModel(const QString& addresses,
                QObject *parent): QAbstractTableModel(parent)
                {
                QStringList records = addresses.split(’\n’);
                QStringList line;
                foreach(QString record, records)
                addressBook.append(splitCSVLine(record));
                }
                
                
                int AddressbookModel::rowCount(const QModelIndex &parent ) const
                {
                Q_UNUSED(parent);
                return addressBook.count() - 2;
                }
                
                
                //rowCount method
                int AddressbookModel::columnCount(const QModelIndex &parent ) const
                {
                Q_UNUSED(parent);
                return addressBook.at(0).count();
                }
                
                //headerData method
                QVariant AddressbookModel::headerData(int section,Qt::Orientation orientation, int role) const
                {
                if (orientation == Qt::Horizontal) 
                {
                if (role == Qt::DisplayRole) 
                {
                 return addressBook.at(0).at(section);
                }
                }
                return QAbstractTableModel::headerData(section, orientation, role);
                }
                
                
                //data method
                QVariant AddressbookModel::data(const QModelIndex &index,
                int role) const
                {
                if (!index.isValid()) return QVariant();
                QStringList addressRecord = addressBook.at(index.row()+1);
                if (role == Qt::DisplayRole || role == Qt::EditRole) {
                return addressRecord.at(index.column());
                }
                if (role == Qt::ToolTipRole) {
                QString tip, key, value;
                tip = "<table>";
                int maxLines = addressRecord.count();
                for (int i = 0; i < maxLines; i++) {
                key = headerData(i, Qt::Horizontal, Qt::DisplayRole)
                .toString();
                value = addressRecord.at(i);
                if (!value.isEmpty())
                tip += QString("<tr><td><b>%1</b>: %2</td></tr>")
                .arg(key, value);
                }
                tip += "</table>";
                return tip;
                }
                return QVariant();
                }
                
                
                
                
                1 Reply Last reply
                0
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by VRonin
                  #8
                  1. pass your model through the Model Test
                  2. make sure it survives and doesn't go out of scope as your views did

                  "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

                  T 1 Reply Last reply
                  1
                  • VRoninV VRonin
                    1. pass your model through the Model Test
                    2. make sure it survives and doesn't go out of scope as your views did
                    T Offline
                    T Offline
                    thippu
                    wrote on last edited by
                    #9

                    @VRonin Yeah, I did that no errors, bro.
                    code what I did is: inside QWidget.

                    new QAbstractItemModelTester(model,QAbstractItemModelTester::FailureReportingMode::Fatal);
                    
                    
                    JonBJ 1 Reply Last reply
                    0
                    • T thippu

                      @VRonin Yeah, I did that no errors, bro.
                      code what I did is: inside QWidget.

                      new QAbstractItemModelTester(model,QAbstractItemModelTester::FailureReportingMode::Fatal);
                      
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @thippu
                      As @VRonin asked, how do we know from the code you post what the scope/lifetime of your AddressbookModel instance is?

                      T 3 Replies Last reply
                      0
                      • JonBJ JonB

                        @thippu
                        As @VRonin asked, how do we know from the code you post what the scope/lifetime of your AddressbookModel instance is?

                        T Offline
                        T Offline
                        thippu
                        wrote on last edited by thippu
                        #11

                        @JonB I did convert all the objects from stack memory to heap, I'm not getting what you need?

                        1 Reply Last reply
                        0
                        • JonBJ JonB

                          @thippu
                          As @VRonin asked, how do we know from the code you post what the scope/lifetime of your AddressbookModel instance is?

                          T Offline
                          T Offline
                          thippu
                          wrote on last edited by
                          #12

                          @JonB
                          And also I did modelTest, There was no error.

                          1 Reply Last reply
                          0
                          • JonBJ JonB

                            @thippu
                            As @VRonin asked, how do we know from the code you post what the scope/lifetime of your AddressbookModel instance is?

                            T Offline
                            T Offline
                            thippu
                            wrote on last edited by thippu
                            #13

                            @JonB changed all to the heap, I think they will stay till application die, right?
                            code I have changed:

                            Widget::Widget(QWidget *parent)
                            : QWidget(parent)
                            {
                            QFile file("path");
                            if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
                            {
                            qDebug()<<"can't access the file";
                            }
                            QString *addresses=new QString(QString::fromUtf8(file.readAll()));
                            Addressbookmodel *model=new Addressbookmodel(addresses,this);
                            
                            QListView *listView=new QListView();
                            listView->setModel(model);
                            listView->setModelColumn(0);
                            listView->show();
                            
                            QTreeView *treeView=new QTreeView;
                            treeView.setModel(model);
                            treeView.show();
                            
                            QTableView *tableView=new QTableView;
                            tableView.setModel(model);
                            tableView.show();
                            
                            setMiniumSize(800,400);
                            QVBoxLayout *Vlayout=new QVBoxLayout(this);
                            Vlayout->addWidget(listView);
                            Vlayout->addWidget(treeView);
                            Vlayout->addWidget(tableView);
                            }
                            
                            JonBJ 1 Reply Last reply
                            0
                            • T thippu

                              @JonB changed all to the heap, I think they will stay till application die, right?
                              code I have changed:

                              Widget::Widget(QWidget *parent)
                              : QWidget(parent)
                              {
                              QFile file("path");
                              if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
                              {
                              qDebug()<<"can't access the file";
                              }
                              QString *addresses=new QString(QString::fromUtf8(file.readAll()));
                              Addressbookmodel *model=new Addressbookmodel(addresses,this);
                              
                              QListView *listView=new QListView();
                              listView->setModel(model);
                              listView->setModelColumn(0);
                              listView->show();
                              
                              QTreeView *treeView=new QTreeView;
                              treeView.setModel(model);
                              treeView.show();
                              
                              QTableView *tableView=new QTableView;
                              tableView.setModel(model);
                              tableView.show();
                              
                              setMiniumSize(800,400);
                              QVBoxLayout *Vlayout=new QVBoxLayout(this);
                              Vlayout->addWidget(listView);
                              Vlayout->addWidget(treeView);
                              Vlayout->addWidget(tableView);
                              }
                              
                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #14

                              @thippu

                              Addressbookmodel *model=new Addressbookmodel(addresses,this);
                              treeView.setModel(&model);
                              tableView.setModel(&model);
                              

                              Now that model is a pointer, why the &s?

                              T 2 Replies Last reply
                              2
                              • JonBJ JonB

                                @thippu

                                Addressbookmodel *model=new Addressbookmodel(addresses,this);
                                treeView.setModel(&model);
                                tableView.setModel(&model);
                                

                                Now that model is a pointer, why the &s?

                                T Offline
                                T Offline
                                thippu
                                wrote on last edited by
                                #15

                                @JonB Sorry, typo mistake.

                                1 Reply Last reply
                                0
                                • JonBJ JonB

                                  @thippu

                                  Addressbookmodel *model=new Addressbookmodel(addresses,this);
                                  treeView.setModel(&model);
                                  tableView.setModel(&model);
                                  

                                  Now that model is a pointer, why the &s?

                                  T Offline
                                  T Offline
                                  thippu
                                  wrote on last edited by
                                  #16

                                  @JonB I did copy and paste from the early reply, So

                                  1 Reply Last reply
                                  0
                                  • VRoninV Offline
                                    VRoninV Offline
                                    VRonin
                                    wrote on last edited by
                                    #17

                                    I'm surprised you don't get into stack overflow.
                                    AddressbookModel::rowCount and AddressbookModel::columnCount form an infinitely deep tree.

                                    You should return 0; if the parent argument isValid()

                                    "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

                                    T 2 Replies Last reply
                                    1
                                    • VRoninV VRonin

                                      I'm surprised you don't get into stack overflow.
                                      AddressbookModel::rowCount and AddressbookModel::columnCount form an infinitely deep tree.

                                      You should return 0; if the parent argument isValid()

                                      T Offline
                                      T Offline
                                      thippu
                                      wrote on last edited by
                                      #18

                                      @VRonin okay,
                                      In the Addressbookmodel.cpp
                                      I changed the code to :

                                      int Addressbookmodel::rowCount(const QModelIndex &parent) const
                                      {
                                         if(parent.isValid())
                                      {
                                        return 0;
                                      }
                                      return addressbook.count-2;
                                      }
                                      
                                      
                                      int Addressbookmodel::columnCount(const QModelIndex &parent)
                                      const
                                      {
                                        if(parent.isValid())
                                        {
                                         return 0;
                                        }
                                      return addressbook.at(0).count();
                                      }
                                      
                                      

                                      I don't know why it is not used in the book and why am not getting data on the widgets still?.

                                      1 Reply Last reply
                                      0
                                      • VRoninV VRonin

                                        I'm surprised you don't get into stack overflow.
                                        AddressbookModel::rowCount and AddressbookModel::columnCount form an infinitely deep tree.

                                        You should return 0; if the parent argument isValid()

                                        T Offline
                                        T Offline
                                        thippu
                                        wrote on last edited by
                                        #19

                                        @VRonin Can share me an example that helps me to understand how to create QTreeView model and subclass it?, so from this I can able to create my own models successfully.

                                        VRoninV 1 Reply Last reply
                                        0
                                        • T thippu

                                          @VRonin Can share me an example that helps me to understand how to create QTreeView model and subclass it?, so from this I can able to create my own models successfully.

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

                                          @thippu said in Data not displaying on the QTreeView, QTableView,QListView classes:

                                          Can share me an example that helps me to understand how to create QTreeView model and subclass it?

                                          http://doc.qt.io/qt-5/qtwidgets-itemviews-simpletreemodel-example.html

                                          "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

                                          T 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