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. Limit Qstring in QrectF
Forum Updated to NodeBB v4.3 + New Features

Limit Qstring in QrectF

Scheduled Pinned Locked Moved General and Desktop
8 Posts 3 Posters 2.2k 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.
  • D Offline
    D Offline
    Durga
    wrote on last edited by
    #1

    hi, i am drawing rectangle on an item and i want to draw text inside the rectangle, but the text is coming out of the box, how can i put my text inside the rectangle(inside bounding rect of item) and below is the code .

    @void CBirdsEyeItem::paint(QPainter* f_painter, const QStyleOptionGraphicsItem* f_styleItem, QWidget* f_widget)
    {
    Q_UNUSED(f_widget);
    IBaseReferenceDataItem::paint(f_painter,f_styleItem, f_widget); bq. here i am drawing rectngle

    const qreal l_lod = f_styleItem->levelOfDetailFromTransform(f_painter->worldTransform());

    if(getPolygon().size() > 4)
    {
    QPointF l_idPnt = getPolygon().boundingRect().center(); bq. this will give center point of my item

    if(l_lod > 5.0)
    {
    QFont font("Times", 1);
    font.setStyleStrategy(QFont::ForceOutline);
    f_painter->setFont(font);
    f_painter->drawText(l_idPnt, getId());
    }
    }
    }@

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rahul Das
      wrote on last edited by
      #2

      @QFont font("times", 24);
      QFontMetrics fm(font);
      int pixelsWide = fm.width("What's the width of this text?");
      int pixelsHigh = fm.height();@

      Ref: QFontMatrics


      Declaration of (Platform) independence.

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

        Set the flag QGraphicsItem::ItemClipsToShape for your item.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          Durga
          wrote on last edited by
          #4

          hi Asperamanca,
          this flag will clip the data, so i will lost data, i don't want that and i want my text to be fit in the item .

          1 Reply Last reply
          0
          • A Offline
            A Offline
            Asperamanca
            wrote on last edited by
            #5

            In this case, you should go with Rahul's code provided above. Make the item large enough to fit the text.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Durga
              wrote on last edited by
              #6

              in my case, i should not change item size, item size is fixed here and text length is dynamic, i have to show may entire text(varible length text) on fixed size item .

              1 Reply Last reply
              0
              • D Offline
                D Offline
                Durga
                wrote on last edited by
                #7

                hi Rahul Das, could you please tell me how it will be useful for my situation ..

                1 Reply Last reply
                0
                • R Offline
                  R Offline
                  Rahul Das
                  wrote on last edited by
                  #8

                  bq. how can i put my text inside the rectangle(inside bounding rect of item)

                  Well, Obviously you need to resize it. It is your need and logic whether you'll need to wrap the text Or adjust the bounding rect Or Font Size.

                  Refer "Char-map example":http://qt-project.org/doc/qt-5.0/qtwidgets/widgets-charactermap.html
                  and this old "thread":http://qt-project.org/forums/viewthread/11674 as well


                  Declaration of (Platform) independence.

                  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