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. How to truncate a string wrapped in a bounding rect in Qt?
QtWS25 Last Chance

How to truncate a string wrapped in a bounding rect in Qt?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 1.1k 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.
  • T Offline
    T Offline
    tushu
    wrote on last edited by
    #1

    I have a simple QGraphicsView with text written over it. That text is wrapped under a bounding rect. I want to truncate the leading characters of text according to width of the bounding rect.
    If my string is "I am loving Qt" and if my bounding rect is allowing 7 chars then it should show like ...g Qt. Text should not be written in multiple lines. According to width, more chars can be fit.
    Along with I am interested to know, how the text can be aligned ?

    widget.cpp

    Widget::Widget(QWidget *parent)
        : QGraphicsView(parent)
        , ui(new Ui::Widget)
    {
        ui->setupUi(this);
        scene = new QGraphicsScene(this);
        view = new QGraphicsView(this);
        view->setScene(scene);
    }  
    void Widget::on_textButton_clicked()
    {
        Text* t = new Text() ;
        QString s = "I am loving Qt";
        QGraphicsTextItem* text = t->drawText(s);
        scene->addItem(text);
    }
    

    Text.h

    class Text : public QGraphicsTextItem
    {
    
    public:
        explicit Text(const QString &text);
        Text();
        QGraphicsTextItem* drawText(QString s);
        virtual void paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) override;
    
     }
    

    Text.cpp

    QGraphicsTextItem* Text::drawText(QString s)
    {
        QGraphicsTextItem *t = new Text(s);
        t->setTextWidth(8);
        return t;
    }     
         
    void Text::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
    {
        painter->setPen(Qt::black);
        painter->setBrush(QColor(230,230,230));
        painter->drawRect(option->rect);
        QGraphicsTextItem::paint(painter, option, widget);
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Set the proper clipRect on the painter.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

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

        Hi,

        Since you want to do elision, you have to test your string's width with regard to your item bounding rect. QFontMetrics can be used to test that.

        See the Elided label example for some other inspiration.

        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
        1
        • Christian EhrlicherC Christian Ehrlicher

          Set the proper clipRect on the painter.

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

          @Christian-Ehrlicher I have added line in paint()

          painter->setClipRect(QRect(2, 2, 100, 40), Qt::ReplaceClip);    
          

          Now it is clipping the text according to size set in setTextWidth().
          But if I want to truncate starting characters and put 3 dots in final string at starting position, how to do that ?
          Is alignment (left/right/center) possible in wrapped text ?

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

            As I suggested you need to calculate the size of your string with regard to your item. Then if it does not fit, use a loop to append your string from the end to the "..." until it reaches the required size.

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

            T 1 Reply Last reply
            2
            • SGaistS SGaist

              As I suggested you need to calculate the size of your string with regard to your item. Then if it does not fit, use a loop to append your string from the end to the "..." until it reaches the required size.

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

              @SGaist Sorry but , how to know whether my string will fit into bounding rect or not ?
              One more doubt, Can bounding rect be aligned ( left/right/center ) ?

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

                As already written: QFontMetrics.

                As for the alignement, it's the responsibility of QPainter::drawText.

                That said, the bounding rect calculation through QFontMetrics also gives you the alignement as flags to be used.

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

                T 1 Reply Last reply
                2
                • T tushu

                  @SGaist Sorry but , how to know whether my string will fit into bounding rect or not ?
                  One more doubt, Can bounding rect be aligned ( left/right/center ) ?

                  JonBJ Online
                  JonBJ Online
                  JonB
                  wrote on last edited by
                  #8

                  @tushu
                  QRect QFontMetrics::boundingRect(const QRect &rect, int flags, const QString &text, int tabStops = 0, int *tabArray = nullptr) const

                  1 Reply Last reply
                  2
                  • SGaistS SGaist

                    As already written: QFontMetrics.

                    As for the alignement, it's the responsibility of QPainter::drawText.

                    That said, the bounding rect calculation through QFontMetrics also gives you the alignement as flags to be used.

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

                    @SGaist I found one method in QFontMetrics.

                    QString QFontMetrics::elidedText(const QString &text, Qt::TextElideMode mode, int width, int flags = 0) const
                    

                    but I did not get how to calculate width here ?

                    The width is specified in pixels, not characters.
                    
                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      In your case, it's the width of your item.

                      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
                      1

                      • Login

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