Results 1 to 4 of 4

Thread: QWidget in QGraphicsScene not drawing

  1. #1
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default QWidget in QGraphicsScene not drawing

    I am trying to embed a QWidget into a QGraphicsScene using addWidget, and it seems to be embedding successfully, but it is not drawing at all, and I am not sure why. Here's the code where I embed the QWidget:

    Qt Code:
    1. // JSL
    2. void QWorksheetDoc::addTable(DrawObjData *data, bool front, bool leaveSelected, char *theText)
    3. {
    4. if (worksheetView)
    5. {
    6. worksheetScene->clearSelection();
    7. QDatatable *table = new QDatatable();
    8. table->QDatatable::IDatatable(this);
    9. QGraphicsProxyWidget *theProxy = worksheetView->scene()->addWidget(table);
    10.  
    11. theProxy->setFlag(QGraphicsItem::ItemIsMovable);
    12. theProxy->setFlag(QGraphicsItem::ItemIsSelectable);
    13.  
    14. // JSL - install the common scene Event filter object.
    15. theProxy->installSceneEventFilter(sceneDrawCommon);
    16. theProxy->setSelected(true);
    17.  
    18. if (front)
    19. bringToFront();
    20. if (leaveSelected == false)
    21. worksheetScene->clearSelection();
    22. worksheetView->show();
    23. }
    24. }
    To copy to clipboard, switch view to plain text mode 

    QDatatable descends from QTableView.

    Here's the code where QDatatable paints. I added the frame, and the fill to try to see anything on the screen:

    Qt Code:
    1. //
    2. // JSL - paint
    3. //
    4. void QDatatable::paintEvent(QPaintEvent *event)
    5. {
    6. QPainter painter(this);
    7. //QRectF rect = frameRect();
    8.  
    9. QTableView::paintEvent(event);
    10.  
    11. painter.setRenderHint(QPainter::Antialiasing);
    12. painter.setPen(Qt::darkGray);
    13. painter.drawRect(rect());
    14. painter.fillRect(rect(), QColor(224,424,224));
    15. }
    To copy to clipboard, switch view to plain text mode 

    If anyone has any idea why this is not working I would appreciate hearing about it. What I see is a white rectangle that seems to obey z ordering, but is not selectable, or movable, and doesn't render anything.

  2. #2
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Unhappy Re: QWidget in QGraphicsScene not drawing

    I tried changing the widget to a QCheckbox, and it does render, so part of the problem must have to do with the QTableView object. Perhaps I just haven't initialized the data, although this doesn't explain why my code in paintEvent doesn't draw anything.

    Also, even with a checkbox, I can't move or select the widget. I thought with the proxy, it was supposed to behave like a QGraphicsItem. If anyone has worked with a widget in a QGraphicsScene and has any hints I would appreciate hearing them.

    Thanks,

    -stevel

  3. #3
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Smile Re: QWidget in QGraphicsScene not drawing

    The reason my QTableView wasn't rendering was that I didn't have a data model attached to it. I've added code to do this, and it's rendering fine now. I still have the issue with making a widget that's added into a QGraphicsScene movable and selectable, and if anyone has a suggestion about this, I would appreciate hearing it.

    Thanks,

  4. #4
    Join Date
    Apr 2008
    Posts
    29
    Thanks
    5
    Thanked 1 Time in 1 Post

    Question Re: QWidget in QGraphicsScene not drawing

    No one has a suggestion as to how to make a QGraphicsProxyWidget in a GraphicsView selectable, movable, and resizable? I am still trying to figure out how to do this. It looks like it is not default behavior, and I may have to override QGraphicsProxyWidget.

Similar Threads

  1. Drawing grids efficiently in QGraphicsScene
    By Gopala Krishna in forum Qt Programming
    Replies: 16
    Last Post: 7th June 2012, 14:59
  2. When to use QGraphicsScene or QWidget
    By fossill in forum Qt Programming
    Replies: 2
    Last Post: 9th February 2007, 23:58
  3. Drawing on QWidget - strech & resize
    By kemp in forum Qt Programming
    Replies: 5
    Last Post: 22nd January 2007, 14:39
  4. QWidget item in QGraphicsScene
    By IUnknown in forum Qt Programming
    Replies: 4
    Last Post: 4th November 2006, 00:05
  5. Replies: 4
    Last Post: 17th January 2006, 17:46

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.