Page 2 of 3 FirstFirst 123 LastLast
Results 21 to 40 of 43

Thread: QTableWidget issues

  1. #21
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget issues

    Thanx for replies and sorry for stupid questions . What methods I need to reimplement in order to get my QItemDelegate to paint only 6th colum? Secondly, the painting depends on data (3 integer variables), as you can see on picture. How can I send those integers to QItemDelegate so the delegate can paint cell properly? Is there another way?

  2. #22
    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: QTableWidget issues

    Quote Originally Posted by Djony View Post
    What methods I need to reimplement in order to get my QItemDelegate to paint only 6th colum?
    paint() and/or drawDisplay(), depending on which class you subclass. It also depends how you install the delegate. You can install it for a single column only or for all columns (then you have to reimplement paint() to call the base class implementation for columns other then 6th.

    Secondly, the painting depends on data (3 integer variables), as you can see on picture. How can I send those integers to QItemDelegate so the delegate can paint cell properly? Is there another way?
    There are different ways to do this. I'd suggest using different item roles for each "colour" or setting a list of values as the data for the display role (setData(6, Qt::DisplayRole, QList<int>() << 1 << 2 << 3)) and then retrieve each value from within the delegate.

    And I think it would be easier for you to subclass QItemDelegate and reimplement its paint() method than to go for drawDisplay().

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

    Djony (4th December 2006)

  4. #23
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget issues

    Quote Originally Posted by wysota View Post
    paint() and/or drawDisplay(), depending on which class you subclass. It also depends how you install the delegate. You can install it for a single column only or for all columns (then you have to reimplement paint() to call the base class implementation for columns other then 6th.
    How can I install delegate for just single column? I have decided to use QItemDelegate and to reimplement it's paint metod.

    Quote Originally Posted by wysota View Post
    There are different ways to do this. I'd suggest using different item roles for each "colour" or setting a list of values as the data for the display role (setData(6, Qt:isplayRole, QList<int>() << 1 << 2 << 3)) and then retrieve each value from within the delegate.
    Maybe I've explained it wrong in previous post The data shown in that "colored column" will not be changed by the user. It should be shown and "colored" by the delegate based upon the values that some other class reads from SQLITE 3 database. If you mean't the same thing, which methods from QItemDelegate I need to reimplement and how (in which direction, I don't have any idea)? How can I "send" those integers to delegate so he can color cells?

  5. #24
    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: QTableWidget issues

    Quote Originally Posted by Djony View Post
    How can I install delegate for just single column?
    First thing to do is to browse QAbstractItemView docs and find the answer there.


    If you mean't the same thing,
    I did.

    which methods from QItemDelegate I need to reimplement and how (in which direction, I don't have any idea)? How can I "send" those integers to delegate so he can color cells?
    My previous answer still stands. Please read about item roles and Qt MVC implementation.

  6. #25
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget issues

    Thank you for replies. I've read documentation about item roles, models, views, delegates etc. If I understood you right, you are suggesting me to create model and reimplement it's setData() method and to store list of values in it (I am wondering at the moment which model class should I use, considering the fact that I am using QTableWidget. Should it be QStandardItemModel or I need to subclass QAbstractItemModel class?). After that, my delegate class will with data() get list of values and based on those values color the cell. That's cool.
    But, when reimplementing paint() method I need to know cordinates (and dimensions) of whole cell, so I can paint properly. How can I retrieve cordinates (and dimensions) of whole cell in delegate class? (Maybe I've asked this before, but I don't get it...)
    Again, thanx for replies

  7. #26
    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: QTableWidget issues

    If you use QTableWidget, then you don't need to touch the model, as QTableWidget already implements one for you. Just use QTableWidgetItem::setData() to store data and implement a custom delegate to display it.

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

    Djony (14th December 2006)

  9. #27
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget issues

    OK, but can you please tell me how can I retrieve width of table cell in my delegate class?

  10. #28
    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: QTableWidget issues

    QStyleOptionViewItem::rect

  11. #29
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget issues

    Thanx
    I have subclassed QTableWidgetItem and implemented setData() and data() methods. But, I have now problem with storing data (sending those 3 ints). Don't know how to store a list of ints a pass them as QVariant. This is my code:
    Qt Code:
    1. listVariant2.clear();
    2.  
    3. listVariant2.append(QVariant(countOfFail));
    4.  
    5. //countOfFail is int, listVariant is QList <QVariant> type
    6.  
    7. listVariant2.append(QVariant(countOfPass)); //countOfPass is int
    8. listVariant2.append(QVariant(countOfNA)); //countOfNA is int
    9.  
    10. tableItem->setData(Qt::DisplayRole,QVariant(listVariant2)); /*tableItem is type of class which inherits QTableWidgetItem class
    11. */
    To copy to clipboard, switch view to plain text mode 
    I have debugged and discovered that code crashes when calling last instruction (I got segmentation fault, SIGSEV). Probably because of that "QVariant thing"...
    This is probably wrong way of thinking (I am talking about my code), but the best I could think of right now. Could you please give me instructions where to look to find better way to pass those 3 integers ? How to store them in a list as QVariant? Or some example/solution? Thanx for any reply
    Last edited by Djony; 15th December 2006 at 14:13.

  12. #30
    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: QTableWidget issues

    Quote Originally Posted by Djony View Post
    I have subclassed QTableWidgetItem and implemented setData() and data() methods.
    What for? What was wrong with them?

    But, I have now problem with storing data (sending those 3 ints). Don't know how to store a list of ints a pass them as QVariant.
    Qt Code:
    1. QVariantList lst;
    2. lst << 1 << 2 << 3;
    3. tableItem->setData(QVariant(lst), Qt::DisplayRole);
    To copy to clipboard, switch view to plain text mode 

  13. #31
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget issues

    Quote Originally Posted by wysota View Post
    What for? What was wrong with them?
    Aren't they virtual?

  14. #32
    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: QTableWidget issues

    If a function is virtual it doesn't mean you have to reimplement it but that you may do it if you need to.

  15. #33
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget issues

    Thank you, you are right. But, now, that I've solved this, paint method of my delegate's class is never called (code never enters in that function). I set delegate for whole table like this:
    Qt Code:
    1. tableTestSuits->setItemDelegate(new TableDelegate (this));
    2. //tableSuits is QTableWidget type and TableDelegate inherits QItemDelegate
    To copy to clipboard, switch view to plain text mode 

    What can be wrong?

  16. #34
    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: QTableWidget issues

    The signature of the function must match exactly, including return type, argument types, constness and such:
    void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
    J-P Nurmi

  17. #35
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget issues

    Thank you, you are right (const at the end was missing). But now I have weird behaviour of application. All cells are empty (before implementing delegate there were strings shown in cells). Here is the body of paint method of my delegate's class:
    Qt Code:
    1. QRect tempRect,rectangleCell=option.rect;
    2.  
    3. int widthCell=rectangleCell.width();
    4.  
    5. if (index.column()==4)
    6. {
    7. //painting only for 5th column
    8.  
    9.  
    10. tempVariant=index.model()->data(index,Qt::DisplayRole);
    11.  
    12. //retrieving data from appropriate model
    13.  
    14. listOfVariants=tempVariant.toList();
    15. numberOfFails=listOfVariants.at(0).toInt(); //listOfVariants is QList <QVariant>
    16. numberOfPass=listOfVariants.at(1).toInt();
    17. numberOfNa=listOfVariants.at(2).toInt();
    18.  
    19. //retrieving numbers to color in proper way
    20.  
    21. .....
    22.  
    23. //piece of code which fills rectangle with adequate color, manipulation with values
    24. // that are stored in rectangleCell
    25.  
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 
    Do you have any idea what is wrong? Thanx for reply

  18. #36
    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: QTableWidget issues

    As if I understood, you are painting only some specific column(s). Other colums don't get painted at all. Maybe you want something like this:
    Qt Code:
    1. if (index.column()==4)
    2. {
    3. // your custom painting for the 5th column
    4. }
    5. else
    6. {
    7. // let QItemDelegate paint when other columns in question
    8. QItemDelegate::paint(painter, option, index);
    9. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  19. The following user says thank you to jpn for this useful post:

    Djony (16th December 2006)

  20. #37
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget issues

    You rock! Now I have problem with retrieving integers from QVariant that I've previosly stored via setData() (look above how wysota showed me to store integers in qvariantlist). I am getting very wierd values for integers. Here is my code where I try to retrieve integers:
    Qt Code:
    1. tempVariant=index.model()->data(index,Qt::DisplayRole);
    2. listOfVariants=tempVariant.toList();
    3. numberOfFails=listOfVariants.at(0).toInt(); //numberOfFails is int
    4. numberOfPass=listOfVariants.at(1).toInt(); //numberOfPass is int
    5. numberOfNa=listOfVariants.at(2).toInt(); //numberOfNa is int
    6. //those 3 integers I later use for painting my cell
    To copy to clipboard, switch view to plain text mode 
    I don't know what I am doing wrong. Is there better way to retrieve integers?

  21. #38
    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: QTableWidget issues

    I can't spot anything particular in the code above that could be causing the problem. The bug must be elsewhere. I suggest you to double check the part of the code which sets the values.

    Nevertheless you could check if the conversion from variant to int succeeds:
    Qt Code:
    1. bool ok = false;
    2. numberOfFails=listOfVariants.at(0).toInt(&ok);
    3. if (!ok)
    4. // failed
    To copy to clipboard, switch view to plain text mode 

    By the way,
    Qt Code:
    1. tempVariant=index.model()->data(index,Qt::DisplayRole);
    2. // could be simply written as
    3. tempVariant=index.data(Qt::DisplayRole);
    4. // or even as
    5. tempVariant=index.data();
    To copy to clipboard, switch view to plain text mode 
    It actually ends up calling the same what you have written but is more readable, isn't it..?
    J-P Nurmi

  22. #39
    Join Date
    Nov 2006
    Posts
    72
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QTableWidget issues

    You are right, values are OK. Can you tell me, when I do inside my delegate:
    Qt Code:
    1. QRect rectangleCell=option.rect;
    To copy to clipboard, switch view to plain text mode 
    Does rectangleCell then holds the dimensions of whole table or just current cell?

  23. #40
    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: QTableWidget issues

    It contains the geometry of the index to be painted, so the current cell.
    J-P Nurmi

Similar Threads

  1. print QTableWidget
    By chak_med in forum Qt Programming
    Replies: 3
    Last Post: 4th November 2006, 18:46
  2. parsing QtableWidget from a QStackedWidget
    By rosmarcha in forum Qt Programming
    Replies: 10
    Last Post: 13th October 2006, 14:36
  3. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 18:46
  4. Replies: 6
    Last Post: 5th March 2006, 21:05
  5. How to obtain the width of a QTableWidget?
    By Giel Peters in forum Qt Programming
    Replies: 3
    Last Post: 9th January 2006, 22:34

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.