Results 1 to 20 of 20

Thread: beginInsertRows doesn't appear to be working?

  1. #1
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default beginInsertRows doesn't appear to be working?

    Hi,

    I had this working yesterday, my table was being updated / refreshed by doing the following :

    Qt Code:
    1.  
    2. beginInsertRows(index, 1, nAmount);
    3.  
    4. if( pData )
    5. {
    6. for( int i = 0; i < nAmount; i++ )
    7. {
    8. m_Data.append(pData[i].strId);
    9. m_Data.append(pData[i].strTime);
    10. m_Data.append(pData[i].strData);
    11. QString strCount;
    12. strCount.sprintf( "%i", nCount );
    13. m_Data.append(strCount);
    14. nCount++;
    15. }
    16. }
    17. endInsertRows();
    To copy to clipboard, switch view to plain text mode 

    Now, the table is only refreshed when it scrolls or I click on it?

    Don't know what is wrong?!

    Any help is appreciated.
    Steve

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: beginInsertRows doesn't appear to be working?

    void QAbstractItemModel::beginInsertRows(const QModelIndex& parent, int first, int last)

    Have you noticed that parameters first and last are both indexes (starting from 0)? The last parameter is not the count of inserted items but the index of the last inserted item. I don't know your case, but a range of "from 1 to nAmount" sounds incorrect to me.
    J-P Nurmi

  3. #3
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: beginInsertRows doesn't appear to be working?

    Ok thanks,

    I now tried setting first and last to one, but still, doesn't refresh?!

    Regards,
    Steve

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: beginInsertRows doesn't appear to be working?

    I suggest emitting rowsInserted().

  5. #5
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: beginInsertRows doesn't appear to be working?

    This not a private member?

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: beginInsertRows doesn't appear to be working?

    Quote Originally Posted by wysota View Post
    I suggest emitting rowsInserted().
    Actually one should not do that because QAbstractItemModel might get "out of sync". Methods beginInsertRows() and endInsertRows() emit corresponding rowsAboutToBeInserted() and rowsInserted() for you.
    J-P Nurmi

  7. #7
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: beginInsertRows doesn't appear to be working?

    Thanks jpn,

    I think maybe this has something to do with the fact that I cannot dynamically change the amount of rows in my model, for some reason the amount of rows doesn't get changed visually and I'm confused as to why?

    Kind regards,
    Steve

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: beginInsertRows doesn't appear to be working?

    I think we'd have to see more code to understand what is going on.

    How is this thread related to this one?

  9. #9
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: beginInsertRows doesn't appear to be working?

    Ok, here is most of the code :

    Qt Code:
    1. int DATreeModel::rowCount( const QModelIndex& parent ) const
    2. {
    3. int nCount = m_Data.count() / 4;
    4. return nCount;
    5. }
    6.  
    7. int DATreeModel::columnCount(const QModelIndex &) const
    8. {
    9. return 4;
    10. }
    11.  
    12. QVariant DATreeModel::data( const QModelIndex& index, int role) const
    13. {
    14. QVariant data;
    15.  
    16. if (!index.isValid())
    17. return QVariant();
    18.  
    19. if (index.row() > (m_Data.size() / 4) ) {
    20. return QVariant();
    21. }
    22.  
    23. if(index.column() > 3 ) {
    24. return QVariant();
    25. }
    26.  
    27. if( role == Qt::DisplayRole )
    28. {
    29. int nPos = index.column() + ( 4 * index.row() );
    30. if( nPos < m_Data.size() )
    31. data = m_Data.at( nPos );
    32. }
    33. return data;
    34. }
    35.  
    36. void DATreeModel::setCanData( CANDATA *pData, int nAmount, int nCount )
    37. {
    38. beginInsertRows(QModelIndex(), 1, 1);//nAmount );
    39.  
    40. if( pData )
    41. {
    42. for( int i = 0; i < nAmount; i++ )
    43. {
    44. m_Data.append(pData[i].strId);
    45. m_Data.append(pData[i].strTime);
    46. m_Data.append(pData[i].strData);
    47. QString strCount;
    48. strCount.sprintf( "%i", nCount );
    49. m_Data.append(strCount);
    50. nCount++;
    51.  
    52. }
    53. }
    54. endInsertRows();
    55.  
    56. }
    To copy to clipboard, switch view to plain text mode 

    I'm so confused?

    Kind regards,
    Steve

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: beginInsertRows doesn't appear to be working?

    You didn't answer my question

  11. #11
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: beginInsertRows doesn't appear to be working?

    Sorry, yes, it is related. I have an emit function when data is read which goes back to the view which just then scrolls the table view.

  12. #12
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: beginInsertRows doesn't appear to be working?

    Does DATreeModel::setCanData() get called only once or many times? I think you might be looking for something like this:
    Qt Code:
    1. void DATreeModel::setCanData( CANDATA *pData, int nAmount, int nCount )
    2. {
    3. if( pData )
    4. {
    5. int row = rowCount();
    6. beginInsertRows(QModelIndex(), row, row + nAmount - 1);
    7. ...
    8. endInsertRows();
    9. }
    10. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  13. #13
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: beginInsertRows doesn't appear to be working?

    Hi,

    setCanData() gets called over and over again from another thread. I tried what you suggested and still the data is not displayed until I click on the table view, also, the rowCount() function still not updating the tableview row amount?

    Unless I do something like return 10000 or whatever from the rowCount function, no rows are inserted, I thought rowCount() would do this for you?

    Kind regards,
    Steve

  14. #14
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: beginInsertRows doesn't appear to be working?

    I'm still struggling with this, the rowCount returns the amount of rows, but the view doesn't reflect this?

    Also, QModelIndex() seems to have invalid row and column values in my beginInsertRows...

    Can't believe how difficult this is proving for something so trivial!

    Regards,
    Steve

  15. #15
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: beginInsertRows doesn't appear to be working?

    You can't do it from another thread. Containers are not thread safe. You can only emit a signal from the other thread and catch it in the model and update the model from within the main thread.

  16. #16
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: beginInsertRows doesn't appear to be working?

    Sorry, the worker thread just reads data and emits a signal to main gui thread which then informs the model to update its data. The code below shows the code within the main gui which informs the model of the change with the setCanData function:


    Qt Code:
    1. if( m_pCanReadThread->isRunning() ) // user may have stopped thread
    2. {
    3. if( nCount < 8000 )
    4. {
    5. m_ptreeModel->setCanData( pData, nAmount, nCount );
    6. QModelIndex index = m_ptreeModel->index( nCount, 0 );
    7. ui.tableView->scrollTo( index );
    8. }
    9. else
    10. {
    11. m_pCanReadThread->cancelThread();
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Steve

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: beginInsertRows doesn't appear to be working?

    As a little check emit layoutChanged() at the end of setCanData(). If it makes it work, it means that you have something wrong in your model and using the Modeltest might be a good idea.

  18. #18
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: beginInsertRows doesn't appear to be working?

    Hi,

    emit layoutChanged() doesn't have any effect. Still not refreshing the view nor are the rows being inserted.

    The only way I can get this to work is to set the row count to a big size right at the begining and click on the table view to get the items to appear, very, very strange and totally not acceptable. Obviously when the table has to scroll, the items appear. One thing I have noticed is that the items in the first column of the table always appear?

    Regards,
    Steve

  19. #19
    Join Date
    May 2007
    Posts
    301
    Thanks
    46
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: beginInsertRows doesn't appear to be working?

    Hi,

    The problem is that I turn off the signals from the model and don't turn them back on, I do this for speed, having the signals on reduces the speed I can insert data.

    Regards,
    Steve

  20. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: beginInsertRows doesn't appear to be working?

    You don't have to block signals in this situation as you don't insert one row at a time.

  21. The following user says thank you to wysota for this useful post:

    steg90 (11th May 2007)

Similar Threads

  1. QTextEdit::find() backward not working
    By sukanyarn in forum Qt Programming
    Replies: 1
    Last Post: 15th November 2006, 19:33
  2. Replies: 1
    Last Post: 11th June 2006, 22:25
  3. Mac OS X UI not working
    By hvengel in forum Qt Programming
    Replies: 3
    Last Post: 1st April 2006, 01:02
  4. Signals/Slots stopped working
    By Jimmy2775 in forum Qt Programming
    Replies: 8
    Last Post: 31st March 2006, 21:11
  5. QSettings - beginReadArray not working
    By Mike in forum Qt Programming
    Replies: 7
    Last Post: 9th January 2006, 21:24

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.