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); 
        ui->graphicsView_2->setScene(scene2); 
        auto document2 = new QTextDocument; 
        QTextCharFormat charFormat2; 
        charFormat2.setFont(QFont("Arial", 50));
        QPen outlinePen2 = QPen (QColor(0, 0, 0), 1, Qt::SolidLine);
        charFormat2.setTextOutline(outlinePen2);
        QTextCursor cursor2 = QTextCursor(document2); 
        cursor2.insertText("TEST", charFormat2);
        auto textItem2 = new QGraphicsTextItem();
        textItem2->setDefaultTextColor(Qt::white);
        textItem2->setDocument(document2);
        textItem2->setTextInteractionFlags(Qt::TextEditable);
        scene2->addItem(textItem2);
        ui->graphicsView_2->setStyleSheet("background-image:url('test.jpg')");
    

    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.

    	cursor2.select(QTextCursor::Document);
    	cursor2.removeSelectedText();
    	cursor2.clearSelection();
    	cursor2.movePosition(QTextCursor::Start);
    	QTextCharFormat charFormat;
    	charFormat.setFont(QFont("Arial", 50));
    	QPen outlinePen = QPen(QColor(0, 0, 0), 1, Qt::SolidLine);
    	charFormat.setTextOutline(outlinePen);
    	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 ;)

    Regards


  • Lifetime Qt Champion

    Hi,

    For your alignment question, shouldn't you just set the alignment of the QTextBlockFormat to [Qt::AlignJustified](http://doc.qt.io/qt-5/qt.html#AlignmentFlag-enum) ?

    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
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.