Results 1 to 5 of 5

Thread: QItemDelegate and Qtablewidget : where to destroy pointer ???

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default QItemDelegate and Qtablewidget : where to destroy pointer ???

    Hy all,
    i've this class to change rows color of cells in table according their background.

    I use this class in a slot from another class (main program qwidget) called every time a user clicks on a rows of a table

    in the slot i've this simple code :

    Qt Code:
    1. ui.mytable->setItemDelegate(new MyItemDelegate(this, CurrSelRow));
    To copy to clipboard, switch view to plain text mode 

    All works well, and the cells' background is changed according to paint method.

    but i'm wondering :

    Every time the user clicks on a row, the slot is invoked and a new delegate is instanced.

    And to destroy ??
    I think a lot of memory is used if the user clicks many times on a row...

    Is this right ??
    Is there a way to destroy the istance ??



    Qt Code:
    1. class MyItemDelegate: public QItemDelegate
    2. {
    3.  
    4.  
    5. private:
    6. int myRow;
    7. public:
    8.  
    9.  
    10.  
    11. MyItemDelegate(QObject* pParent = 0, int rowitem = 0) : QItemDelegate(pParent)
    12. {
    13.  
    14. myRow = rowitem;
    15. }
    16.  
    17.  
    18. void paint(QPainter* pPainter, const QStyleOptionViewItem& rOption, const QModelIndex& rIndex) const
    19. {
    20.  
    21. if(rIndex.isValid())
    22. {
    23.  
    24. QStyleOptionViewItem ViewOption(rOption);
    25. QColor ItemBackgroundColor = rIndex.data(Qt::BackgroundRole).value<QColor>();
    26. QString ColorName = ItemBackgroundColor.name();
    27.  
    28. QString val;
    29.  
    30. if (qVariantCanConvert<QString>(rIndex.data()))
    31. val = qVariantValue<QString>(rIndex.data());
    32.  
    33. if (ItemBackgroundColor.isValid())
    34. {
    35.  
    36. if(myRow > 0)
    37. {
    38. if (rIndex.row() == myRow)
    39. {
    40. if (ItemBackgroundColor == ClassColor::getRed())
    41. {
    42. ViewOption.palette.setColor(QPalette::HighlightedText, ClassColor::getRed());
    43.  
    44. }
    45. }
    46.  
    47. }
    48.  
    49. }
    50. QItemDelegate::paint(pPainter, ViewOption, rIndex);
    51.  
    52.  
    53. }
    54.  
    55. }
    56. };
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QItemDelegate and Qtablewidget : where to destroy pointer ???

    Why don't you just keep a pointer to the delegate and call a method that sets the new CurrSelRow value?

    Cheers,
    _

  3. #3
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default Re: QItemDelegate and Qtablewidget : where to destroy pointer ???

    Yes i try in this way too...i keep a pointer and a use a set CurrSellRow method,
    but i notice that when i click on a row...the colors change not immediatly...but after some seconds...
    I dont' know how to call paint method on delegate immediatly...

    Using instead a NEW for every click, works well and the colour change immediatly...so i think that when the new object is created the paint method is called immediatly...

    Any helps ??
    Thanks !!!
    Last edited by gab74; 30th September 2013 at 10:34.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QItemDelegate and Qtablewidget : where to destroy pointer ???

    Hmm, I see.

    Have you tried if calling update() on the widget helps?

    Cheers,
    _

  5. #5
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default Re: QItemDelegate and Qtablewidget : where to destroy pointer ???

    I used
    a pointer initialized in 2 points of my software, and use a single delete for the pointer in a closing method.

Similar Threads

  1. QTablewidget and QItemDelegate?
    By whitefurrows in forum Qt Programming
    Replies: 3
    Last Post: 28th April 2010, 15:33
  2. QItemDelegate use in QTableWidget
    By arpspatel in forum Qt Programming
    Replies: 2
    Last Post: 27th October 2009, 22:18
  3. Return a pointer from a QItemdelegate
    By SailinShoes in forum Qt Programming
    Replies: 5
    Last Post: 12th March 2008, 20:07
  4. Replies: 6
    Last Post: 22nd June 2007, 22:20
  5. Replies: 1
    Last Post: 24th June 2006, 20:55

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.