PDA

View Full Version : QWidget in QGraphicsScene not drawing



stevel
3rd July 2008, 18:45
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:



// JSL
void QWorksheetDoc::addTable(DrawObjData *data, bool front, bool leaveSelected, char *theText)
{
if (worksheetView)
{
worksheetScene->clearSelection();
QDatatable *table = new QDatatable();
table->QDatatable::IDatatable(this);
QGraphicsProxyWidget *theProxy = worksheetView->scene()->addWidget(table);

theProxy->setFlag(QGraphicsItem::ItemIsMovable);
theProxy->setFlag(QGraphicsItem::ItemIsSelectable);

// JSL - install the common scene Event filter object.
theProxy->installSceneEventFilter(sceneDrawCommon);
theProxy->setSelected(true);

if (front)
bringToFront();
if (leaveSelected == false)
worksheetScene->clearSelection();
worksheetView->show();
}
}


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:




//
// JSL - paint
//
void QDatatable::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
//QRectF rect = frameRect();

QTableView::paintEvent(event);

painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::darkGray);
painter.drawRect(rect());
painter.fillRect(rect(), QColor(224,424,224));
}


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.

stevel
4th July 2008, 00:09
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

stevel
4th July 2008, 00:48
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,

stevel
19th September 2008, 19:19
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.