Results 1 to 8 of 8

Thread: Qtableview add item and reload table

  1. #1
    Join Date
    Sep 2017
    Posts
    29
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Qtableview add item and reload table

    I use QStandardItemModel to display data in QTableVIew.
    i set already tableview model to the itemmodel

    Qt Code:
    1. table->setModel(yourMomModel);
    To copy to clipboard, switch view to plain text mode 

    then i added the item in model

    Qt Code:
    1. yourMomModel.append(item2);
    To copy to clipboard, switch view to plain text mode 

    but the data not showing in QtableView.
    anybody know whats wrong thanks.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Qtableview add item and reload table

    How did you create the model and item?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qtableview add item and reload table

    These two lines of code are incompatible:

    Qt Code:
    1. table->setModel(yourMomModel);
    2.  
    3. // and
    4.  
    5. yourMomModel.append(item2);
    To copy to clipboard, switch view to plain text mode 

    In the first line QAbstractItemView::setModel() takes a pointer to a model. In the second line, you use "dot" notation to dereference, which implies "yourMomModel" is a stack or member variable, not a pointer. Which is it? Do they refer to the same instance?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Sep 2017
    Posts
    29
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Qtableview add item and reload table

    nvm. i solved it.

  5. #5
    Join Date
    Sep 2017
    Posts
    29
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Qtableview add item and reload table

    Here's how i did it:

    I used QstandardItemModel and add QStandardItem to it.
    then apply the model to my QTableView.

    code:

    Qt Code:
    1. // header
    2. private:
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. // cpp
    2. // in constructor
    3. myModel = new QStandardItemModel(0, 0, this); // an empty model
    4.  
    5. ui->myTableView->setModel(myModel);
    6.  
    7. // create the item
    8. QStandardItem *item1 = new QStandardItem("Test");
    9.  
    10. // set the item to the model
    11. myModel->setItem(0,0,item1); // 0,0 is row/colum.
    To copy to clipboard, switch view to plain text mode 
    I am noob. Are you noob? Lets learn Qt together! https://qtnoobies.blogspot.com/

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qtableview add item and reload table

    Ah, this is a common misunderstanding. You have created an empty model (0 rows and 0 columns). Calling setItem( row, column, item ) requires that the row and column already exist. That is not the case for an empty model, so nothing happens.

    You should take one of the following steps:

    1 - create your model with non-zero row and column counts
    2 - use setRowCount() and setColumnCount() to grow the model as needed
    3 - use appendRow() or appendColumn() to add items and grow the model at the same time

    A typical scenario is to create a model with 0 rows and "n" columns (since you usually know how many columns your table will have). Then you create the list of new items for a row and call appendRow() with the list. That sticks the row onto the end of the model and grows rowCount() by 1.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Sep 2017
    Posts
    29
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Windows

    Default Re: Qtableview add item and reload table

    Quote Originally Posted by d_stranz View Post
    Ah, this is a common misunderstanding. You have created an empty model (0 rows and 0 columns). Calling setItem( row, column, item ) requires that the row and column already exist. That is not the case for an empty model, so nothing happens.

    You should take one of the following steps:

    1 - create your model with non-zero row and column counts
    2 - use setRowCount() and setColumnCount() to grow the model as needed
    3 - use appendRow() or appendColumn() to add items and grow the model at the same time

    A typical scenario is to create a model with 0 rows and "n" columns (since you usually know how many columns your table will have). Then you create the list of new items for a row and call appendRow() with the list. That sticks the row onto the end of the model and grows rowCount() by 1.
    Actually what I need was the tableview start as empty. And then I setitem starting from 0,0. Seems to add items to it nicely.
    I am noob. Are you noob? Lets learn Qt together! https://qtnoobies.blogspot.com/

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Qtableview add item and reload table

    void QStandardItemModel::setItem(int row, int column, QStandardItem *item)

    Sets the item for the given row and column to item. The model takes ownership of the item. If necessary, the row count and column count are increased to fit the item.
    You are correct, my mistake. I rarely use QStandardItemModel, which behaves differently in this regard. Other types of models typically require the code to set the size of the model before adding to it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. The following user says thank you to d_stranz for this useful post:

    GeneCode (28th September 2017)

Similar Threads

  1. Validator for the table widget item in table
    By mukunda in forum Qt Programming
    Replies: 4
    Last Post: 6th June 2011, 23:07
  2. reload qtableview after sorted
    By poporacer in forum Newbie
    Replies: 0
    Last Post: 18th April 2011, 21:07
  3. Find item in table
    By JohnToddSr in forum Qt Programming
    Replies: 1
    Last Post: 3rd July 2009, 16:24
  4. How to refresh table item
    By vieraci in forum Qt Programming
    Replies: 5
    Last Post: 1st June 2009, 00:43
  5. Qt4 Table Item Spanning
    By billyp in forum Qt Programming
    Replies: 1
    Last Post: 20th December 2008, 17:11

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.