QLabel with custom word wrap mode
-
wrote 12 days ago last edited by
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 thepaintEvent()
method and draw text with theQt::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? -
Since QLabel accepts a subset of html you can use html for your link:
"<a href=\"www.google.de\">very long long text</a>"
-
Since QLabel accepts a subset of html you can use html for your link:
"<a href=\"www.google.de\">very long long text</a>"
wrote 8 days ago last edited by@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. Thetext()
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);
-
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.
-
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.
@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/5