Results 1 to 6 of 6

Thread: QTableView has constant number of rows

  1. #1
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QTableView has constant number of rows

    Hello,

    I've got a little problem with QTableView and my own model subclassed from QAbstractTableView. The model is read only, user can change/add data using external dialog. When I add some data, table has still the same number of rows and new data doesn't appear. I know the data is correctly added. When I remove some data from a model, number of rows doesn't change either, last rows of the table just becomes empty. Here is header of my model:

    Qt Code:
    1. class EmployeesTableModel : public QAbstractTableModel {
    2. Q_OBJECT
    3. public:
    4. EmployeesTableModel( QObject* parent = 0 );
    5. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    6. int columnCount ( const QModelIndex & parent = QModelIndex() ) const;
    7. QVariant data(const QModelIndex &index, int role) const;
    8. QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
    9. void setEmployeesList( std::vector<WPEmployee>* employees );
    10. private:
    11. std::vector<WPEmployee>* _employees;
    12. };
    To copy to clipboard, switch view to plain text mode 

    thanks for any help
    Last edited by high_flyer; 10th December 2008 at 12:13.

  2. #2
    Join Date
    Aug 2008
    Location
    Nanjing, China
    Posts
    66
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView has constant number of rows

    please paste your implementation of
    Qt Code:
    1. int rowCount(const QModelIndex &parent = QModelIndex()) const;
    To copy to clipboard, switch view to plain text mode 
    You should emit correct signal when you change(del or edit) the data in the model to notify the view.You could read the qt doc about Model / view programming.
    Jerry

  3. #3
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView has constant number of rows

    When you modify (or add to) the underlying data (you passed the std::vector by pointer) the model will obviously not know about it.

    So, you have to choices:
    i) make all modifications by interacting with your model class (and emit the appropriate signals there; that way you have the possibility to be more precise and say things like "20 rows have been added" - allowing you to keep the current selection...)
    ii) modify the underlying data and tell the model afterwards about it, so it can reset itself (QAbstractItemModel::reset(); you will lose the current selection) that way

    usually, when dealing with a model, all changes to the models data should be made through the model. otherwise you kinda defeat its purpose...

    HTH

  4. The following user says thank you to caduel for this useful post:

    tomeks (10th December 2008)

  5. #4
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView has constant number of rows

    here is rowcount():
    int EmployeesTableModel::rowCount(const QModelIndex &parent ) const {
    return _employees->size();
    }
    columncount() returns 3.

    Are you talking about QAbstractItemModel::rowsInserted(), QAbstractItemModel::rowsRemoved() and QAbstractItemModel::dataChanged() signals? Should i really emit these signals, even if i don't change data directly in model? Model contains only pointer to data, which is changed in other dialog. All changed data appears on the view, only number of rows is constant.

  6. #5
    Join Date
    Dec 2008
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QTableView has constant number of rows

    thanks very much caduel! QAbstractItemModel::reset() solves the problem.

  7. #6
    Join Date
    Aug 2008
    Location
    Nanjing, China
    Posts
    66
    Thanks
    12
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTableView has constant number of rows

    Quote Originally Posted by tomeks View Post
    Are you talking about QAbstractItemModel::rowsInserted(), QAbstractItemModel::rowsRemoved() and QAbstractItemModel::dataChanged() signals?
    er, I think the pairs begininsetrows() ,endinsetrows() and beginRemoveRows(), end removerows() are proper functions you should call to insert/remove data. Their implementation emit the proper signals. dataChanged() is mostly used in setData().
    Should i really emit these signals, even if i don't change data directly in model? Model contains only pointer to data, which is changed in other dialog. All changed data appears on the view, only number of rows is constant.
    I guess you try to avoid let the model become a editable model to make things easier.But the way you choose is not efficent esp when your data is large. Becasue reset() really tell the view to do much more things than you need. I suggest you implement

    Qt Code:
    1. Qt::ItemFlags flags(const QModelIndex &index) const;
    2. bool setData(const QModelIndex &index, const QVariant &value,
    3. int role = Qt::EditRole);
    4. bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex());
    5. bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex());
    To copy to clipboard, switch view to plain text mode 
    to achieve your goal. They allow you to edit your model flexiblely.
    Last edited by calmspeaker; 10th December 2008 at 15:35.
    Jerry

Similar Threads

  1. Set height of QTableView to fit exact number of rows.
    By Ben.Hines in forum Qt Programming
    Replies: 3
    Last Post: 17th January 2019, 01:49
  2. Remove selected rows from a QTableView
    By niko in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2016, 12:49
  3. KDE/QWT doubt on debian sarge
    By hildebrand in forum KDE Forum
    Replies: 13
    Last Post: 25th April 2007, 06:13
  4. Replies: 6
    Last Post: 5th March 2006, 21:05
  5. QTableView number of rows and scrollbar issue
    By jnk5y in forum Qt Programming
    Replies: 3
    Last Post: 1st March 2006, 06:55

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.