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. QLabel with custom word wrap mode
Forum Updated to NodeBB v4.3 + New Features

QLabel with custom word wrap mode

Scheduled Pinned Locked Moved Unsolved General and Desktop
qlabelwordwrappainteevent
5 Posts 2 Posters 196 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.
  • V Offline
    V Offline
    voltron
    wrote 12 days ago last edited by
    #1

    I need to display text (path or connection string) without whitespaces in a QLabel, something like this for example.

    contextualWMSLegend=0&crs=EPSG:4326&dpiMode=7&featureCount=10&format=image/gif&tilePixelRatio=0&url=https://geo-netinfo.trafikverket.se/MapService/wms.axd/NetInfo_1_5&layers=ADT_axelpar&layers=ADT_total&layers=ADT_tungtrafik&layers=ATK_Stracka&layers=ATK_Matplats&layers=Barighet&layers=BegransadBruttovikt&layers=BegransadFordonsbredd&layers=BegransadFordonslangd&layers=BegransatAxelBoggitryck&layers=BroTunnel&layers=BrunnSlamsugning&layers=BullerskyddVag&layers=C_Rekbilvagcykeltrafik&layers=Cirkulationsplats&layers=DriftbidragStatligt&layers=Driftomrade&layers=Farjeled&layers=Farligtgods_rekvag&layers=Farthinder&layers=ForbudMotTrafik&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=&styles=
    

    If the text gets long the widget containing label can easily go out of the screen borders. Setting wordWrap property on label does not help as labels only support wrapping on word-breaks.

    My first idea was to create a QLabel subclass, override the paintEvent() method and draw text with the Qt::TextWrapAnywhere flag. Unfortunately, in this case I lost ability to render rich text and instead of nice link I get raw HTML. I was also thinking about drawing elided text instead, but this approach has the same issue with broken rich text support.

    Is it possible to customize word wrap and/or elide mode without losing rich text rendering and without re-implementing most of the QLabel internals?

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote 11 days ago last edited by
      #2

      Since QLabel accepts a subset of html you can use html for your link: "<a href=\"www.google.de\">very long long text</a>"

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

      V 1 Reply Last reply 8 days ago
      0
      • C Christian Ehrlicher
        11 days ago

        Since QLabel accepts a subset of html you can use html for your link: "<a href=\"www.google.de\">very long long text</a>"

        V Offline
        V Offline
        voltron
        wrote 8 days ago last edited by
        #3

        @Christian-Ehrlicher said in QLabel with custom word wrap mode:

        Since QLabel accepts a subset of html you can use html for your link: "<a href=\"www.google.de\">very long long text</a>"

        Yes, I know. But unfortunately, this does not work with overridden paintEvent(), as I wrote in the first message. The text() contains raw HTML and as I understand there is no easy way to convert it into rich text without re-implementing a lot of code from QLabel. Am I miss something?

        For example, how I can get HTML rendering with this simple override?

        void TestLabel::paintEvent(QPaintEvent *)
        {
          QFontMetrics fm = fontMetrics();
          QString t = fm.elidedText(text(), Qt::TextElideMode::ElideRight, textRect.width(), Qt::TextFlag::TextShowMnemonic);
          QPainter painter(this);
          painter.drawText(contentsRect(), QStyle::visualAlignment(layoutDirection(), alignment()) | Qt::TextFlag::TextSingleLine, t);
        
        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote 8 days ago last edited by
          #4

          You have to find out by yourself. Search for href and parse it. Or since you have a custom label already - define some new functions to pass the url and use this later on. I would not override the paint event. Don't see why it would be needed.

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

          V 1 Reply Last reply a day ago
          0
          • C Christian Ehrlicher
            8 days ago

            You have to find out by yourself. Search for href and parse it. Or since you have a custom label already - define some new functions to pass the url and use this later on. I would not override the paint event. Don't see why it would be needed.

            V Offline
            V Offline
            voltron
            wrote a day ago last edited by
            #5

            @Christian-Ehrlicher said in QLabel with custom word wrap mode:

            I would not override the paint event. Don't see why it would be needed.

            Maybe I miss something, but how then I can make QLabel support wordwrap not only on word-breaks or add elided text support?

            1 Reply Last reply
            0

            1/5

            22 May 2025, 11:51

            • Login

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