Results 1 to 6 of 6

Thread: How to paint a table from an item delegate?

  1. #1
    Join Date
    Nov 2010
    Posts
    22
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default How to paint a table from an item delegate?

    I have a table where I display mostly text, however in some cases some cells have a list of values.

    I wanted to render those lists as a one column table within the cell. For that reason I made my own item delegate and overloaded the QitemDelegate : : paint() function.

    Inside, if I do not detect a list I just pass the flow :

    Qt Code:
    1. QItemDelegate::paint( painter, option, index );
    To copy to clipboard, switch view to plain text mode 

    However, if I have a list I want to build that table. For testing I have:
    Qt Code:
    1. QTableWidget* tableWidget = new QTableWidget(0);
    2. tableWidget->setRowCount(4);
    3. tableWidget->setColumnCount(1);
    4.  
    5. tableWidget->setItem(0, 0, new QTableWidgetItem("x"));
    6. tableWidget->setItem(1, 0, new QTableWidgetItem("x"));
    7. tableWidget->setItem(2, 0, new QTableWidgetItem("x"));
    8. tableWidget->setItem(3, 0, new QTableWidgetItem("x"));
    To copy to clipboard, switch view to plain text mode 

    Now my idea was to use that QTableWidget to paint the table, an so far the only way I saw to pass a QPainter parameter was with the render function:

    Qt Code:
    1. tableWidget->render(painter);
    To copy to clipboard, switch view to plain text mode 

    That does not work and the cell looks empty (with/within saving/restoring the state before using render() ). Any idea about how could I reuse the painter of QTableWidget to draw that table???

  2. #2
    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: How to paint a table from an item delegate?

    Why don't you just draw the items directly?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2010
    Posts
    22
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to paint a table from an item delegate?

    I want to have have the list separated by lines, later I might need to add a second column. I did not like the idea of reimplementing the table widget/view painter... plus, I would like to learn how to reuse the painter of other widgets

  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: How to paint a table from an item delegate?

    Quote Originally Posted by frank100 View Post
    I did not like the idea of reimplementing the table widget/view painter...
    Creating a new widget every time some other widget is painted is a very bad idea.

    plus, I would like to learn how to reuse the painter of other widgets
    When painting yourself, you'd be doing the exact same thing when it comes to "reusing the painter of another widget".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Nov 2010
    Posts
    22
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to paint a table from an item delegate?

    Thanks for the help, I realize I did not make a clear question :-(

    Quote Originally Posted by wysota View Post
    Creating a new widget every time some other widget is painted is a very bad idea.
    That was not my intention. Although I do that in my example, actually I defined one static QTableWidget instance inside the constructor of my item delegate. I rewrote that part within the paint() function of the question to make it less cluttered with code.

    Quote Originally Posted by wysota View Post
    When painting yourself, you'd be doing the exact same thing when it comes to "reusing the painter of another widget".
    That is what I want. For drawing a table within a cell I must align the text and draw lines for the grid. I expected that if the QTableView/QTableWidget have a painter doing what I want, there should be a way to reuse it.

    Somehow I suppose I can give them the QPainter I receive from one of the parameters and let them do the drawing for me.

    I could also make the question more generic. I have a table with a custom item delegate and I want this delegate to draw tables within some of the cells. I thought that painting myself was the way to go

  6. #6
    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: How to paint a table from an item delegate?

    Quote Originally Posted by frank100 View Post
    That was not my intention. Although I do that in my example, actually I defined one static QTableWidget instance inside the constructor of my item delegate. I rewrote that part within the paint() function of the question to make it less cluttered with code.
    But you still have to repopulate the widget each time you paint a cell.

    That is what I want. For drawing a table within a cell I must align the text and draw lines for the grid. I expected that if the QTableView/QTableWidget have a painter doing what I want, there should be a way to reuse it.
    I think you misinterpret what "painter" is.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 0
    Last Post: 10th March 2011, 12:44
  2. Delegate paint event bug?
    By jerry7 in forum Qt Programming
    Replies: 2
    Last Post: 20th October 2010, 03:14
  3. Paint in Custom Delegate
    By Nightfox in forum Qt Programming
    Replies: 4
    Last Post: 7th October 2010, 21:54
  4. Replies: 5
    Last Post: 10th August 2009, 11:50
  5. Delegate paint optimisation
    By bunjee in forum Qt Programming
    Replies: 11
    Last Post: 21st April 2008, 20:48

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.