Some Questions about graphicsView

  • Hello

    Im currently working on a small tool, that should just have a grid, clickable labels/buttons in each grid, and small picture on those and text in front of it.

    I got everything to work, which looks good. The Code looks like this:

        auto scene2 = new QGraphicsScene(ui->graphicsView_2); 
        auto document2 = new QTextDocument; 
        QTextCharFormat charFormat2; 
        charFormat2.setFont(QFont("Arial", 50));
        QPen outlinePen2 = QPen (QColor(0, 0, 0), 1, Qt::SolidLine);
        QTextCursor cursor2 = QTextCursor(document2); 
        cursor2.insertText("TEST", charFormat2);
        auto textItem2 = new QGraphicsTextItem();

    On first load, the background image is there, and the Text is centert. Now the Problem:
    When i change the Text with the following code, it wont re-center the Text, which looks werid then.;
    	QTextCharFormat charFormat;
    	charFormat.setFont(QFont("Arial", 50));
    	QPen outlinePen = QPen(QColor(0, 0, 0), 1, Qt::SolidLine);
    	cursor2.insertText(NewText, charFormat);

    This looks like that in the Grid:
    alt text

    How can i re-center the text?

    Another Question would then be, how can i make my Text Multiline, but both lines are centerd themself? Currently, when i insert the text with "TEXTTEXT\nTEXT", then there are 2 lines, but the second one starts on the same point as the first one, resulting in not-looking-good.

    To the Question why i used a graphicsview and not just a qtlabel or something else - I want to have that text-outline. And on google i found only this. If there is another way for that, i would be happy to change it.

    Hope someone can help ;)


  • Lifetime Qt Champion


    For your alignment question, shouldn't you just set the alignment of the QTextBlockFormat to [Qt::AlignJustified]( ?

    As for your primary problem, do you have any spaces around there ?

  • Hello

    Thanks for Reply and the point to QTextBlockFormat. It now does center it perfectly in the horizonale. but not in the vertical one. i tried to change the alignment to Qt::AlignCenter (which should be horizonal and vertical together), and also just Qt::AlignVCenter didnt help.

    Anything i did wrong?

Log in to reply