Results 1 to 4 of 4

Thread: Error format a cell in QTableView

  1. #1
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Exclamation Error format a cell in QTableView

    Hi guys,

    I have a QTableView with four columns. I reimplement the virtual method data( ) to align text the cells. So far, so good, but I also want to replace the cells where the value is 0 (zero) for a dash ("-"). While trying to do this, my application compiles correctly but gives an error of segmentation fault when I open the window.
    See the code:

    Qt Code:
    1. QVariant TableViewQryModel::data(const QModelIndex &idx, int role) const
    2. {
    3. QVariant v = QSqlQueryModel::data(idx, role);
    4.  
    5. if (!idx.isValid())
    6. return QVariant( );
    7.  
    8. if (role == Qt::DisplayRole && idx.column() > 0 &&
    9. index(idx.row(), idx.column(), idx.parent()).data().toString() == "0")
    10. {
    11. return QVariant("-");
    12. }
    13. else if (role == Qt::TextAlignmentRole)
    14. {
    15. return int(Qt::AlignCenter | Qt::AlignVCenter);
    16. }
    17.  
    18. return (v);
    19. }
    To copy to clipboard, switch view to plain text mode 

    Thanks,

    Marcelo E. Geyer

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Error format a cell in QTableView

    Quote Originally Posted by estanisgeyer View Post
    if (role == Qt::DisplayRole && idx.column() > 0 &&
    index(idx.row(), idx.column(), idx.parent()).data().toString() == "0")
    This causes an infinite recursion.

    Use:
    Qt Code:
    1. if( role == Qt::DisplayRole && idx.column() > 0 && QSqlQueryModel::data( idx ).toString() == "0" ) {
    2. ...
    To copy to clipboard, switch view to plain text mode 
    or, since you call data() in the first line:
    Qt Code:
    1. if( role == Qt::DisplayRole && idx.column() > 0 && v.toString() == "0" ) {
    2. ...
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Thanks
    18
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Error format a cell in QTableView

    Ok, it's works!
    I do not understand where this happens recursion, could you explain?

    Thanks,
    Marcelo E. Geyer

  4. #4
    Join Date
    Jul 2008
    Location
    Germany
    Posts
    509
    Thanks
    11
    Thanked 76 Times in 74 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Error format a cell in QTableView

    Hi,
    you call index() with the row, column and parent of the index you are testing, ergo get the same index, and then call data() (the method you are already in), which will calls index() with the row, column and parent of the index you are testing, ... until infinity.

    Ginsengelf
    Last edited by Ginsengelf; 4th September 2008 at 14:36.

Similar Threads

  1. how to color a cell in a QTableView
    By JeanC in forum Qt Programming
    Replies: 13
    Last Post: 9th September 2015, 10:08
  2. cell selection in QTableView
    By user in forum Qt Programming
    Replies: 3
    Last Post: 26th May 2008, 01:01
  3. How to select a cell in a QTableView
    By JeanC in forum Qt Programming
    Replies: 6
    Last Post: 6th February 2008, 13:20
  4. Copying contents of QTableView cell to clipboard
    By Conel in forum Qt Programming
    Replies: 2
    Last Post: 18th April 2006, 15:50
  5. QTableView change color of current Cell
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 4th March 2006, 11:22

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.