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. Text is not appearing at specified position in Qt
Forum Updated to NodeBB v4.3 + New Features

Text is not appearing at specified position in Qt

Scheduled Pinned Locked Moved Unsolved General and Desktop
16 Posts 5 Posters 853 Views 1 Watching
  • 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.
  • T tushu

    @JonB Yes you are right. I tried width / 2 but text position was not correct. So I tried width/4 and the position got correct.
    Am I right ?

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #7

    @tushu said in Text is not appearing at specified position in Qt:

    Am I right ?

    Logically, not to my understanding...

    So I tried width/4 and the position got correct.

    ...but I cannot argue with empirical evidence!

    :)

    Do you want to paste what your GetMidPosition() is actually doing...?

    T 1 Reply Last reply
    0
    • JonBJ JonB

      @tushu said in Text is not appearing at specified position in Qt:

      Am I right ?

      Logically, not to my understanding...

      So I tried width/4 and the position got correct.

      ...but I cannot argue with empirical evidence!

      :)

      Do you want to paste what your GetMidPosition() is actually doing...?

      T Offline
      T Offline
      tushu
      wrote on last edited by
      #8

      @JonB Thank you for your concern and help.
      I would love to paste the code, but it will be very hard to understand as it is not straightforward. It uses tuple in c++, other class's function. And main thing is I have not written that code ( that's routing part and I am on rendering side) I am just using that function so It will be very hard to explain you.

      Sorry for this doubt but I have to ask.
      Currently in my design, there are multiple polylines and rectangles. But some of the polylines are such that, If I try to select any polyline, I could not select them but some other polyline gets selected. But If I remove that other selected polyline then I can select my desired polyline. ( I have a hide polyline feature )

      Now I do not know how to proceed with this let alone finding solution. I am clueless. How to think ? How to debug this issue ?
      Can you help me ?

      JonBJ 1 Reply Last reply
      0
      • T tushu

        @JonB Thank you for your concern and help.
        I would love to paste the code, but it will be very hard to understand as it is not straightforward. It uses tuple in c++, other class's function. And main thing is I have not written that code ( that's routing part and I am on rendering side) I am just using that function so It will be very hard to explain you.

        Sorry for this doubt but I have to ask.
        Currently in my design, there are multiple polylines and rectangles. But some of the polylines are such that, If I try to select any polyline, I could not select them but some other polyline gets selected. But If I remove that other selected polyline then I can select my desired polyline. ( I have a hide polyline feature )

        Now I do not know how to proceed with this let alone finding solution. I am clueless. How to think ? How to debug this issue ?
        Can you help me ?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #9

        @tushu

        • Without seeing snippets of relevant code how can anybody help you?
        • Step through the code in a debugger to see how it is behaving.
        T 1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #10

          Hi,

          Any chances you have a scale factor applied to the screen you are working on ?

          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
          • JonBJ JonB

            @tushu

            • Without seeing snippets of relevant code how can anybody help you?
            • Step through the code in a debugger to see how it is behaving.
            T Offline
            T Offline
            tushu
            wrote on last edited by tushu
            #11

            @JonB @SGaist @jsulm It's a continuation of my last question to you (i.e. If I try to select a particular polyline , it does not get select but some other polyline gets selected.
            Here is my design
            Pro1.png

            If I click on position A , B, C , D orange color polyline gets selected. If I try to select polyline inside U - shaped orange polyline, they never get selected. Only the orange color polyline gets selected.

            If I click on position E, F, G a blue color polyline gets selected. I can not select anything which is inside of these U - shaped polylines. If I try to select anything inside these U - shaped polylines, only U shaped polyline gets selected.

            There are many polylines and rectangles in the design. But this problem arises with only U - shaped polylines. Other polylines ( which are not U shape, and which are not inside U - shaped polylines get selected properly. )

            Now come to the coding part:
            There is a by default Qt's selection method of any QGraphicsItem. If you click on them, it creates a dotted rectangle around that item. So, in U shape polylines, it creates a rectangle and if you click inside that rectangle, that polyline gets selected.
            In paint() method I have changed the selection method of Qt, but I think there is still a problem

            void myPoly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                                         QWidget *widget)
            {
                auto copied_option = *option;
                copied_option.state &= ~QStyle::State_Selected;
                auto selected = option->state & QStyle::State_Selected;
                QGraphicsPathItem::paint(painter, &copied_option, widget);
                if (selected) {
                    painter->save();
                    painter->setBrush(Qt::NoBrush);
                    painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine));
                    painter->drawPath(shape());
                    painter->restore();
                }
            }
            
            T 1 Reply Last reply
            0
            • T tushu

              @JonB @SGaist @jsulm It's a continuation of my last question to you (i.e. If I try to select a particular polyline , it does not get select but some other polyline gets selected.
              Here is my design
              Pro1.png

              If I click on position A , B, C , D orange color polyline gets selected. If I try to select polyline inside U - shaped orange polyline, they never get selected. Only the orange color polyline gets selected.

              If I click on position E, F, G a blue color polyline gets selected. I can not select anything which is inside of these U - shaped polylines. If I try to select anything inside these U - shaped polylines, only U shaped polyline gets selected.

              There are many polylines and rectangles in the design. But this problem arises with only U - shaped polylines. Other polylines ( which are not U shape, and which are not inside U - shaped polylines get selected properly. )

              Now come to the coding part:
              There is a by default Qt's selection method of any QGraphicsItem. If you click on them, it creates a dotted rectangle around that item. So, in U shape polylines, it creates a rectangle and if you click inside that rectangle, that polyline gets selected.
              In paint() method I have changed the selection method of Qt, but I think there is still a problem

              void myPoly::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
                                           QWidget *widget)
              {
                  auto copied_option = *option;
                  copied_option.state &= ~QStyle::State_Selected;
                  auto selected = option->state & QStyle::State_Selected;
                  QGraphicsPathItem::paint(painter, &copied_option, widget);
                  if (selected) {
                      painter->save();
                      painter->setBrush(Qt::NoBrush);
                      painter->setPen(QPen(option->palette.windowText(), 0, Qt::SolidLine));
                      painter->drawPath(shape());
                      painter->restore();
                  }
              }
              
              T Offline
              T Offline
              tushu
              wrote on last edited by
              #12

              @JonB @SGaist @jsulm Is my reasoning correct ? Is my logic of selection of items through paint() is incomplete ?

              1 Reply Last reply
              0
              • C Offline
                C Offline
                ChrisW67
                wrote on last edited by
                #13

                @tushu said in Text is not appearing at specified position in Qt:

                Name should be centrally aligned i.e. half of the string name characters should be left side of mid position and half of the characters should be right side of mid position.

                The width of each rendered character is variable unless you are using a monospace font (not normally the case). That is, an "e" takes fewer pixels horizontally than a "C". The Qt functions for sizing text work in the rendered pixel width not characters. You can position text to balance the number of pixels trivially (see examples below)

                To get an even number of characters left/right of a line you would need to handle odd/even string lengths differently.
                In the even case, divide the string into two, size and position each separately adjacent to the centre line.
                In the odd case, split the string into three parts; left, right, and the centre character. Size and place the centre character (by pixel?), then size and place left/right parts to adjoin the centre character's bounding box.
                This approach may have potential oddities associated with the whitespace between characters.

                include <QApplication>
                #include <QPixmap>
                #include <QPainter>
                #include <QFont>
                #include <QFontMetrics>
                #include <QDebug>
                
                int main(int argc, char **argv) {
                        QApplication app(argc, argv);
                
                        const QString text("Checker");
                
                        QPixmap pixmap(799, 799);
                        pixmap.fill();
                
                        QPainter painter(&pixmap);
                        QFont font("times", 20);
                        painter.setFont(font);
                
                
                // Box we will centre on
                        QRect tile(100, 100, 599, 599);
                        painter.setPen(Qt::blue);
                        painter.drawRect(tile);
                        painter.setPen(Qt::gray);
                        painter.drawLine(tile.center().x(), tile.top(), tile.center().x(), tile.bottom());
                
                // text centred one way
                        QFontMetrics f(font);
                        QRect r = f.boundingRect(text);
                        r.translate(tile.center().x() - r.width()/2, tile.top());
                        painter.setPen(Qt::red);
                        painter.drawRect(r);
                        painter.setPen(Qt::black);
                        painter.drawText(r, text);
                
                // Using the painter to do the work
                        painter.drawText(tile, Qt::AlignHCenter|Qt::AlignTop, text, &r);
                        painter.setPen(Qt::green);
                        painter.drawRect(r);
                
                        painter.end();
                
                        pixmap.save("result.png");
                
                        return 0;
                }
                
                T 1 Reply Last reply
                0
                • C ChrisW67

                  @tushu said in Text is not appearing at specified position in Qt:

                  Name should be centrally aligned i.e. half of the string name characters should be left side of mid position and half of the characters should be right side of mid position.

                  The width of each rendered character is variable unless you are using a monospace font (not normally the case). That is, an "e" takes fewer pixels horizontally than a "C". The Qt functions for sizing text work in the rendered pixel width not characters. You can position text to balance the number of pixels trivially (see examples below)

                  To get an even number of characters left/right of a line you would need to handle odd/even string lengths differently.
                  In the even case, divide the string into two, size and position each separately adjacent to the centre line.
                  In the odd case, split the string into three parts; left, right, and the centre character. Size and place the centre character (by pixel?), then size and place left/right parts to adjoin the centre character's bounding box.
                  This approach may have potential oddities associated with the whitespace between characters.

                  include <QApplication>
                  #include <QPixmap>
                  #include <QPainter>
                  #include <QFont>
                  #include <QFontMetrics>
                  #include <QDebug>
                  
                  int main(int argc, char **argv) {
                          QApplication app(argc, argv);
                  
                          const QString text("Checker");
                  
                          QPixmap pixmap(799, 799);
                          pixmap.fill();
                  
                          QPainter painter(&pixmap);
                          QFont font("times", 20);
                          painter.setFont(font);
                  
                  
                  // Box we will centre on
                          QRect tile(100, 100, 599, 599);
                          painter.setPen(Qt::blue);
                          painter.drawRect(tile);
                          painter.setPen(Qt::gray);
                          painter.drawLine(tile.center().x(), tile.top(), tile.center().x(), tile.bottom());
                  
                  // text centred one way
                          QFontMetrics f(font);
                          QRect r = f.boundingRect(text);
                          r.translate(tile.center().x() - r.width()/2, tile.top());
                          painter.setPen(Qt::red);
                          painter.drawRect(r);
                          painter.setPen(Qt::black);
                          painter.drawText(r, text);
                  
                  // Using the painter to do the work
                          painter.drawText(tile, Qt::AlignHCenter|Qt::AlignTop, text, &r);
                          painter.setPen(Qt::green);
                          painter.drawRect(r);
                  
                          painter.end();
                  
                          pixmap.save("result.png");
                  
                          return 0;
                  }
                  
                  T Offline
                  T Offline
                  tushu
                  wrote on last edited by
                  #14

                  @ChrisW67 Thank you for your response.
                  There is one more question I have asked just 2nd response above your post.
                  Can you look into it ?

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    ChrisW67
                    wrote on last edited by
                    #15

                    I assume your "polyline" is a custom derivative of QGraphicsItem. What happens if you setBoundingRegionGranularity() to something other than 0 so that your item returns something better than the default bounding box.

                    T 1 Reply Last reply
                    0
                    • C ChrisW67

                      I assume your "polyline" is a custom derivative of QGraphicsItem. What happens if you setBoundingRegionGranularity() to something other than 0 so that your item returns something better than the default bounding box.

                      T Offline
                      T Offline
                      tushu
                      wrote on last edited by
                      #16

                      @ChrisW67
                      I tried what you said, but it did not work.
                      Here is my implementation for polyline.

                      QPolygonF net;
                          net << QPointF(67,202);
                          net << QPointF(139,198);
                         
                          QPainterPath pPath;
                          pPath.addPolygon(net);
                          MyPoly* _poly = new MyPoly();
                          _poly->DrawPolyline(pPath);
                          scene->addItem(static_cast<QGraphicsPathItem*>(_poly));
                      
                      void MyPoly::DrawPolyline(QPainterPath pPath)
                      {
                          //this->setPen(QPen(QColor("red", 1));
                          this->setPath(pPath);
                          this->setFlag(QGraphicsItem::ItemIsSelectable);
                          this->setBoundingRegionGranularity(1);
                      }
                      
                      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