[SOLVED]Why QGraphicsScene does not add the QGraphicsRect?
-
Hello, and happy tomorrow new year! I have a simple code, I must draw a rect in QGraphicScene but it`s not displayed. Here is the code:
@
myScene = new QGraphicsScene;
myView = new QGraphicsView(myScene);
myView->setMaximumWidth(width);
myView->setMaximumHeight(height);
myScene->setBackgroundBrush(QBrush(Qt::red));
myScene->setSceneRect(QRectF(0,0,width-(width/40),
height-(height/40)) );
screenItem = new ActionRectItem(100, 100, 100, 100,
"Screen", 1, NULL,
myScene,
new Callbacks(dummy3,
dummy1,
dummy2,
doer));((QGraphicsRectItem*)screenItem)->setBrush(QBrush(Qt::green)); myScene->addItem((QGraphicsRectItem*)screenItem); myView->show();
@
Where ActionRectItem is subclass of QGraphicsRect item. Ive tried with a pure QGraphicsRectItem, it
s not added aswell.
[EDIT] Ill add the QGraphicsRectItem is not displayed, not the scene. [EDIT] It appeared that I
ve forgot to implement my custom overriden paint() from QGraphicsRectItem:
@
void ActionRectItem::paint(QPainter painter, const QStyleOptionGraphicsItem option, QWidget widget)
{
functors->onPaint(painter, option, widget, (ActionRectItem)this);
}
@
And when I implement:
@
static inline void customOnPaint(QPainter qp,
const QStyleOptionGraphicsItem qsi,
QWidget* wd, QGraphicsItem* ari) {QBrush Brush(Qt::green); ((QGraphicsRectItem*)ari)->setBrush(QBrush(Qt::red)); ((QGraphicsRectItem*)ari)->setRect( ((QGraphicsRectItem*)ari)->boundingRect() ); qp->fillRect(((QGraphicsRectItem*)ari)->boundingRect() , Brush);
}
@Its working now, it was my mistake. Sorry.
-
I think there are some wrong size calculations.
The example below shows green square on red view.
@
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);QGraphicsScene* myScene = new QGraphicsScene(this); QGraphicsView* myView = new QGraphicsView(myScene, ui->centralWidget); myView->setScene(myScene); int width = this->width(); int height = this->height(); myView->setMaximumWidth(width); myView->setMaximumHeight(height); myScene->setBackgroundBrush(QBrush(Qt::red)); myScene->setSceneRect(QRectF(0, 0, width, height)); QGraphicsRectItem* screenItem = new QGraphicsRectItem(100, 100, 100, 100);
// screenItem = new ActionRectItem(100, 100, 100, 100,
// "Screen", 1, NULL,
// myScene,
// new Callbacks(dummy3,
// dummy1,
// dummy2,
// doer));((QGraphicsRectItem*)screenItem)->setBrush(QBrush(Qt::green)); myScene->addItem((QGraphicsRectItem*)screenItem);
// myView->show();
}
@ -
You are right. But why is that... Kind of messy it got here :S
-
OK, so I have QGraphicsItem, with overriden methods, paint, advance and onMousePressEvent. So, how I add the QGraphicsRectItem to be below the invisible QGraphicsItem, who is overriden, or how do I pass these functions to QGraphicsRectItem?
-
I
ve solved it :( Appeared to be entirely my mistake, since I was working with it and I
ve forgot an important override.