hi friends,
i have a problem in positioning an rect item (0, 0, 20, 150) in a graphicsScene() of size (0, 0, 1024, 1024)
DiagView
::DiagView(QWidget *parent
)
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setCacheMode(CacheBackground);
setViewportUpdateMode(FullViewportUpdate);
setStyleSheet("background:#336699");
QRectF bounds
(0,
0,
1024,
1024);
setScene(scene);
item1
= new SlotItem
(QRectF(0,
0,
20,
150));
item1->setPos(10,180);
scene->addItem(item1);
}
{
fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
}
DiagView::DiagView(QWidget *parent)
: QGraphicsView(parent)
{
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setCacheMode(CacheBackground);
setViewportUpdateMode(FullViewportUpdate);
setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
setStyleSheet("background:#336699");
QRectF bounds(0, 0, 1024, 1024);
scene = new QGraphicsScene(bounds, this);
setScene(scene);
item1 = new SlotItem(QRectF(0, 0, 20, 150));
item1->setPos(10,180);
scene->addItem(item1);
}
void DiagView::resizeEvent(QResizeEvent *event)
{
QGraphicsView::resizeEvent(event);
fitInView(scene->sceneRect(), Qt::KeepAspectRatio);
}
To copy to clipboard, switch view to plain text mode
slotitem.cpp
SlotItem
::SlotItem(const QRectF &rect
){
}
{
painter
->setBrush
(QColor(0,
102,
153));
painter->drawRoundRect(rect());
}
QRectF SlotItem
::boundingRect() {
}
SlotItem::SlotItem(const QRectF &rect)
: QGraphicsRectItem(rect)
{
}
void SlotItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
painter->setBrush(QColor(0, 102, 153));
painter->drawRoundRect(rect());
}
QRectF SlotItem::boundingRect()
{
return QRectF(0,0, 50,300);
}
To copy to clipboard, switch view to plain text mode
main.cpp
int main(int argc, char *argv[])
{
DiagView *dialog = new DiagView();
dialog->setGeometry(0, 0, 1024, 1024);
dialog->show();
return app.exec();
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
DiagView *dialog = new DiagView();
dialog->setGeometry(0, 0, 1024, 1024);
dialog->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
but the SlotItem is shown in the center of the view port because of the resize event .. if i dont use resize event it positioned the item acurately ... but i need the resize event in future ... what went wrong in my code .. please any one help me ...
Bookmarks