PDA

View Full Version : QT5 / items / setPos / graphicsView



fulgor
2nd March 2017, 11:13
This is my code

I'd like to setPos() but with the ui->graphicsView->setAlignment(Qt::AlignTop|Qt::AlignLeft); all the items are at left I can only change setPos(x,y); y value

How can I do to use setPos() ?


QString txt=ui->lineEdit->text();
QGraphicsScene * sc=new QGraphicsScene();

QGraphicsTextItem * text=new QGraphicsTextItem();

text->setHtml("100");

QGraphicsTextItem * text2=new QGraphicsTextItem();

text2->setHtml("100");

QGraphicsLineItem * line=new QGraphicsLineItem();

line->setLine(0, 0, 50, 0);

QGraphicsTextItem * text3=new QGraphicsTextItem();

text3->setHtml("200");

sc->addItem(text);
sc->addItem(text2);
sc->addItem(text3);

sc->addItem(line);

text->setPos(30, 0);
text2->setPos(30, 20);
text3->setPos(30, 45);
line->setPos(30, 40);

ui->graphicsView->setScene(sc);

ui->graphicsView->setAlignment(Qt::AlignTop|Qt::AlignLeft);

ui->graphicsView->update();

Santosh Reddy
3rd March 2017, 11:01
In your code setPos() works, but QGraphicsView and QGraphicsScene just work like that. The thing is QGraphicsView by default fits the items in the scene, not the origin (0,0)

To see the difference (magic) just add a rectangle and see the change.



QGraphicsRectItem * item = new QGraphicsRectItem();
item->setRect(QRect(0, 0, 500, 500));
sc->addItem(item);


Now your line and text items will shift :)