1

I want to draw a character aligned to the top-left corner of parent.

QGraphicsTextItem * tItem = new QGraphicsTextItem(parent);
tItem->setPlainText("a"); 
tItem->setPos(QPointF(0,0));

Picture below presents output of my code (grey rectangle is parent of QGraphicsTextItem)

Result:

Result

I want to get a result like this:

My dream result:

My dream result

I tried to use Qt::AlignLeft and Qt::AlignTop but to no avail.

4

1 回答 1

0

解决方案是使用setDocumentMargin(0)QTextDocument如果您只输入字母“a”,它似乎不是解决方案,因为似乎存在垂直偏移,但实际上它不是偏移,但大写字母具有更高的高度.

例子:

#include <QApplication>
#include <QGraphicsTextItem>
#include <QGraphicsView>
#include <QTextDocument>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene scene;
    QGraphicsView view(&scene);
    auto parent = scene.addRect(QRectF(0, 0, 100, 100),  QPen(Qt::red), QBrush(Qt::blue));

    auto * tItem = new QGraphicsTextItem(parent);
    tItem->setPlainText("aAbB");
    tItem->document()->setDocumentMargin(0);
    view.show();
    return a.exec();
}

在此处输入图像描述

于 2018-08-14T16:57:29.863 回答