Results 1 to 7 of 7

Thread: Draw on a QTableWidget

  1. #1
    Join Date
    Jan 2006
    Location
    England
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question Draw on a QTableWidget

    I'm reletively new to Qt so I'm still learning the best ways to do thing. Quite often I guess there's a simple solution which I don't know about.

    Anyway, I have a QTableWidget which I've set up so that each cell is square. However, I want to delineate blocks of cells to make it clearer. I want blocks of 3x3 cells to have a darker border around them as in the picture attached.

    As far as I can tell, to do this I will have to subclass QTableWidget and reimplement its paintevent() function and draw the lines there. However, if I do this I don't know how to make it draw the table, text, etc as usual. The lines are static and just have to sit atop the table so is there any easier way do do this?

    Many thanks
    Attached Images Attached Images

  2. #2
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Draw on a QTableWidget

    You can overwrite paintEvent and call the old paintEvent in your new paintEvent using QTableWidget:aintEvent, e.g:

    Qt Code:
    1. void MyTableWidget::paintEvent(QPaintEvent* event) {
    2. // first call the old paintEvent to draw text and lines and stuff:
    3. QTableWidget::paintEvent(event);
    4.  
    5. // Now add your own drawing code here:
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by axeljaeger; 6th January 2006 at 20:40.
    It's nice to be important but it's more important to be nice.

  3. #3
    Join Date
    Jan 2006
    Location
    England
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draw on a QTableWidget

    I used the code you gave me and just to try it I used an example given in the QPainter documentation:
    Qt Code:
    1. void TableGrid::paintEvent(QPaintEvent* event)
    2. {
    3. QTableWidget::paintEvent(event);
    4.  
    5. QPainter paint(this);
    6. paint.setPen(Qt::blue);
    7. paint.drawText(rect(), Qt::AlignCenter, "The Text");
    8. }
    To copy to clipboard, switch view to plain text mode 
    However, this gives me run-time warnings saying "QPainter::begin: Widget painting can only begin as a result of a paintEvent" yet it compiles just fine. This is the only place I have implemented any painting functions so this must be the cause. However, the error doesn't make any sense as the call is from within the paintEvent function.

    It is not a problem with the code you gave me as it gives the same errors if I reduce the code to:
    Qt Code:
    1. void TableGrid::paintEvent(QPaintEvent* event)
    2. {
    3. QPainter paint(this);
    4. }
    To copy to clipboard, switch view to plain text mode 
    So it is obviously being caused by the begin() call in the constructor which for some reason thinks it's not being called from a paintEvent function. Any ideas?

    I don't know if this makes any difference but I'm using Qt 4.1 under Linux.

  4. #4
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Draw on a QTableWidget

    How do you trigger the paintEvent? By calling update? Or dont't you trigger it by yourself and only let Qt trigger it when neccessary?
    It's nice to be important but it's more important to be nice.

  5. #5
    Join Date
    Jan 2006
    Location
    England
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draw on a QTableWidget

    I'm letting Qt call the event whenever it wants to. It seems to do it whenever a cell is selected or deselected and when it is moved. The function is definitely being called though.

    I can even reproduce the problem in a minimal program where the only thing I do is create the custom table and try to draw in it's paintEvent function.

  6. #6
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: Draw on a QTableWidget

    From http://doc.trolltech.com/4.1/qabstra...tml#paintEvent
    void QAbstractScrollArea:aintEvent ( QPaintEvent * event ) [virtual protected]

    This event handler can be reimplemented in a subclass to receive paint events (passed in event), for the viewport() widget.

    Note: If you open a painter, make sure to open it on the viewport().

    Reimplemented from QWidget.

    See also QWidget:aintEvent().
    Maybe this helps
    It's nice to be important but it's more important to be nice.

  7. The following user says thank you to axeljaeger for this useful post:

    Corran (30th December 2006)

  8. #7
    Join Date
    Jan 2006
    Location
    England
    Posts
    20
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Draw on a QTableWidget

    That sorted it, thanks a lot. I'd only read the QWidget paintEvent documentation. It didn't occur to me that it would be subclassed halfway through the system like that.

Similar Threads

  1. want to draw points in QGraphicsScene
    By ntp in forum Qt Programming
    Replies: 2
    Last Post: 10th April 2008, 19:14
  2. QComboBox in QTableWidget : display troubles.
    By Nyphel in forum Qt Programming
    Replies: 2
    Last Post: 14th October 2007, 00:29
  3. QTableWidget issues
    By Djony in forum Qt Programming
    Replies: 42
    Last Post: 20th December 2006, 00:27
  4. QTableWidget editing question
    By Trasmeister in forum Qt Programming
    Replies: 1
    Last Post: 20th September 2006, 19:46
  5. Replies: 6
    Last Post: 5th March 2006, 22:05

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.