Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Some Questions about graphicsView

Some Questions about graphicsView

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 696 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aroc
    wrote on last edited by Aroc
    #1

    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

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        Aroc
        wrote on last edited by
        #3

        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?

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved