<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Is posible to get somehow line height in QTextEdit]]></title><description><![CDATA[<p dir="auto">Hi, i have question.<br />
<strong>Is possible to get height of line in QTextEdit with word wrap (eventually with letter wrap)?.</strong></p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/bba33f8c-2d9a-4e9f-b411-7cea13aac7f4.png" alt="318e25c5-3e39-4e46-9dbd-8a8a145bfe0c-obrazek.png" class=" img-fluid img-markdown" /></p>
<p dir="auto">In web / html i simply add custom &lt;el&gt; tag and then get position from top but i dont have any idea how to do it in QT.</p>
]]></description><link>https://forum.qt.io/topic/157394/is-posible-to-get-somehow-line-height-in-qtextedit</link><generator>RSS for Node</generator><lastBuildDate>Fri, 24 Apr 2026 20:35:32 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/157394.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 23 Jun 2024 17:32:50 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Is posible to get somehow line height in QTextEdit on Tue, 25 Jun 2024 07:55:27 GMT]]></title><description><![CDATA[<p dir="auto">For anybody who tryes something similar.</p>
<pre><code>text-&gt;setFrameStyle(QFrame::NoFrame);
</code></pre>
<p dir="auto">This removes the border and boundingRect returns acurate values.</p>
<p dir="auto">If someboady has other solution without removing all styles from text area i would be glad to hear that.<br />
Thanks for responses.</p>
]]></description><link>https://forum.qt.io/post/803427</link><guid isPermaLink="true">https://forum.qt.io/post/803427</guid><dc:creator><![CDATA[tolomaj]]></dc:creator><pubDate>Tue, 25 Jun 2024 07:55:27 GMT</pubDate></item><item><title><![CDATA[Reply to Is posible to get somehow line height in QTextEdit on Wed, 26 Jun 2024 07:01:44 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/803367">Is posible to get somehow line height in QTextEdit</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jeremy_k">@<bdi>jeremy_k</bdi></a><br />
I am only trying to help the OP, this is not my area so I don't know what is available.</p>
<p dir="auto">Is it/is it not the case that the text shown by a <code>QTextEdit</code> goes into/comes from the <code>QTextDocument *document()</code></p>
</blockquote>
<p dir="auto">Yes, there is a QTextDocument, available via QTextEdit::document().<br />
QTextDocument in turn make a QAbstractTextLayout available via QTextDocument::layout(). As a QTextDocument can be shared between multiple QTextEdit instances, I wasn't sure that the layout would be useful for this purpose. A quick test suggests that it can be.</p>
<p dir="auto">QAbstractTextLayout::blockBoundingRect() takes a QTextBlock and returns a QRectF. If the text in question can be isolated as an individual QTextBlock,  this appears to be a viable option. The floating point use warrants the <a href="https://dl.acm.org/doi/10.1145/103162.103163" target="_blank" rel="noopener noreferrer nofollow ugc">usual</a> consideration.</p>
]]></description><link>https://forum.qt.io/post/803529</link><guid isPermaLink="true">https://forum.qt.io/post/803529</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Wed, 26 Jun 2024 07:01:44 GMT</pubDate></item><item><title><![CDATA[Reply to Is posible to get somehow line height in QTextEdit on Wed, 26 Jun 2024 06:31:38 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tolomaj">@<bdi>tolomaj</bdi></a> said in <a href="/post/803414">Is posible to get somehow line height in QTextEdit</a>:</p>
<blockquote>
<p dir="auto">I used QFontMetrics::boundingRect()  as you sugest.</p>
<p dir="auto">But i encountred a problem that the point where text wraps(in boundingRect) dont match the visible text wrap when resizing.<br />
[...]</p>
<pre><code>QRect rct = text-&gt;geometry();
</code></pre>
<p dir="auto">[...]<br />
I asume that the 4px border can be diferent on other machines/stylings.<br />
<strong>So My qestion is if there is way to get size of text without the border?</strong><br />
<strong>Or if there is way to remove the border?</strong></p>
</blockquote>
<p dir="auto">The <a href="https://doc.qt.io/qt-6/qabstractscrollarea.html#viewport" target="_blank" rel="noopener noreferrer nofollow ugc">viewport</a> should provide correct geometry for the area the text is rendered into.<br />
Eg: <code>text-&gt;viewport()-&gt;rect();</code></p>
]]></description><link>https://forum.qt.io/post/803520</link><guid isPermaLink="true">https://forum.qt.io/post/803520</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Wed, 26 Jun 2024 06:31:38 GMT</pubDate></item><item><title><![CDATA[Reply to Is posible to get somehow line height in QTextEdit on Tue, 25 Jun 2024 12:33:23 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tolomaj">@<bdi>tolomaj</bdi></a><br />
I don't know whether it works, but <a class="plugin-mentions-user plugin-mentions-a" href="/user/mpergand">@<bdi>mpergand</bdi></a> code above uses the approach I suggested might be needed, i.e. it queries the underlying structured <code>QTextDocument</code> about the actual layout of its block elements.  <a class="plugin-mentions-user plugin-mentions-a" href="/user/jeremy_k">@<bdi>jeremy_k</bdi></a> may know more than I, but I don't see that just calling <code>QFontMetrics::boundingRect()</code> will begin to take into account the actual layout in the <code>QTextEdit</code> you are wanting.</p>
]]></description><link>https://forum.qt.io/post/803447</link><guid isPermaLink="true">https://forum.qt.io/post/803447</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Tue, 25 Jun 2024 12:33:23 GMT</pubDate></item><item><title><![CDATA[Reply to Is posible to get somehow line height in QTextEdit on Tue, 25 Jun 2024 12:30:50 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/tolomaj">@<bdi>tolomaj</bdi></a> said in <a href="/post/803427">Is posible to get somehow line height in QTextEdit</a>:</p>
<blockquote>
<p dir="auto">If someboady has other solution without removing all styles from text area i would be glad to hear that.</p>
</blockquote>
<p dir="auto">Maybe this:</p>
<pre><code>QTextEdit edit;
edit.append("1---------");
edit.append("2--------------------------------------------------");
edit.append("3-----------------------------------------------------------------------");
edit.append("1--------------");

edit.setFixedWidth(200);
edit.show();

QTextBlock block=edit.document()-&gt;begin();
while(block.isValid())
	{
	auto rect=block.layout()-&gt;boundingRect();
	qDebug()&lt;&lt;rect&lt;&lt;block.text().left(3);
	block=block.next();
	}
</code></pre>
<p dir="auto">QRectF(0,0 192x16) "1--"<br />
QRectF(0,0 192x32) "2--"<br />
QRectF(0,0 192x48) "3--"<br />
QRectF(0,0 192x16) "1--"</p>
<blockquote>
<p dir="auto">I asume that the 4px border can be diferent on other machines/stylings.</p>
</blockquote>
<p dir="auto">You can retrieve document margin value with:</p>
<pre><code>edit.document()-&gt;documentMargin()
</code></pre>
]]></description><link>https://forum.qt.io/post/803446</link><guid isPermaLink="true">https://forum.qt.io/post/803446</guid><dc:creator><![CDATA[mpergand]]></dc:creator><pubDate>Tue, 25 Jun 2024 12:30:50 GMT</pubDate></item><item><title><![CDATA[Reply to Is posible to get somehow line height in QTextEdit on Tue, 25 Jun 2024 07:55:27 GMT]]></title><description><![CDATA[<p dir="auto">For anybody who tryes something similar.</p>
<pre><code>text-&gt;setFrameStyle(QFrame::NoFrame);
</code></pre>
<p dir="auto">This removes the border and boundingRect returns acurate values.</p>
<p dir="auto">If someboady has other solution without removing all styles from text area i would be glad to hear that.<br />
Thanks for responses.</p>
]]></description><link>https://forum.qt.io/post/803427</link><guid isPermaLink="true">https://forum.qt.io/post/803427</guid><dc:creator><![CDATA[tolomaj]]></dc:creator><pubDate>Tue, 25 Jun 2024 07:55:27 GMT</pubDate></item><item><title><![CDATA[Reply to Is posible to get somehow line height in QTextEdit on Tue, 25 Jun 2024 03:56:46 GMT]]></title><description><![CDATA[<p dir="auto">I used QFontMetrics::boundingRect()  as you sugest.</p>
<p dir="auto">But i encountred a problem that the point where text wraps(in boundingRect) dont match the visible text wrap when resizing.<br />
I removed all css styles of text and all tags in text area without any sucess.</p>
<p dir="auto">There is code taht create widgets:</p>
<pre><code>//shared
QTextEdit * text;
QFont font; //I use the same font everywhere to be 100% sure that there is not an error

//QTextEdit setup
font = QFont("Helvetica", 15);
text = new QTextEdit();
text-&gt;setPlainText("- helo this is and long text helo");
text-&gt;setFont(font);
</code></pre>
<p dir="auto">And there is code that calculate width of line on resize:</p>
<pre><code>QRect rct = text-&gt;geometry();
rct.setWidth(rct.width() - 8 ); // Compensation for border
QRect rect = text-&gt;fontMetrics().boundingRect(rct, Qt::AlignLeft | Qt::TextWordWrap , "- helo this is and long text helo");

//print out 
std::cout &lt;&lt;"out_rect:"&lt;&lt; rect.height() &lt;&lt; "-"&lt;&lt; rect.width() &lt;&lt; "," &lt;&lt; "in_rect:"&lt;&lt; text-&gt;geometry().width() &lt;&lt; "-" &lt;&lt; text-&gt;geometry().height() &lt;&lt; std::endl;
</code></pre>
<p dir="auto">I got a theory why this code doesn't work.</p>
<p dir="auto">After some pixel counting I find that QTextEdit has probably some margin around it (in my case 4px)( the 8px in code) (red area on the image).</p>
<p dir="auto"><img src="https://ddgobkiprc33d.cloudfront.net/480759e6-b25a-430f-8242-285d123d5a03.png" alt="obrazek.png" class=" img-fluid img-markdown" /><br />
The 8px compensation works, and wraps show right.</p>
<p dir="auto">I asume that the 4px border can be diferent on other machines/stylings.<br />
<strong>So My qestion is if there is way to get size of text without the border?</strong><br />
<strong>Or if there is way to remove the border?</strong></p>
]]></description><link>https://forum.qt.io/post/803414</link><guid isPermaLink="true">https://forum.qt.io/post/803414</guid><dc:creator><![CDATA[tolomaj]]></dc:creator><pubDate>Tue, 25 Jun 2024 03:56:46 GMT</pubDate></item><item><title><![CDATA[Reply to Is posible to get somehow line height in QTextEdit on Mon, 24 Jun 2024 10:28:09 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jeremy_k">@<bdi>jeremy_k</bdi></a><br />
I am only trying to help the OP, this is not my area so I don't know what is available.</p>
<p dir="auto">Is it/is it not the case that the text shown by a <code>QTextEdit</code> goes into/comes from the <code>QTextDocument *document()</code> and/or a <code>QTextDocumentLayout</code>?  Are the items shown as bullet-list-items present as "paragraphs" in the document?  Then can the user query from that via the document/layout to find the laid-out size of a particular paragraph, which would take everything into account?  Or am I off-center here?</p>
]]></description><link>https://forum.qt.io/post/803367</link><guid isPermaLink="true">https://forum.qt.io/post/803367</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 24 Jun 2024 10:28:09 GMT</pubDate></item><item><title><![CDATA[Reply to Is posible to get somehow line height in QTextEdit on Mon, 24 Jun 2024 08:46:13 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jonb">@<bdi>JonB</bdi></a> said in <a href="/post/803356">Is posible to get somehow line height in QTextEdit</a>:</p>
<blockquote>
<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jeremy_k">@<bdi>jeremy_k</bdi></a><br />
In that function the docs state</p>
<blockquote>
<p dir="auto">The drawing, and hence the bounding rectangle, is constrained to the rectangle <em>rect</em>.</p>
</blockquote>
<p dir="auto">In the OP's case what should they pass into <code>QFontMetrics::boundingRect(const QRect &amp;rect, ...)</code> for <code>rect</code>?</p>
</blockquote>
<p dir="auto">The full paragraph of the quoted documentation is:<br />
<code>Returns the bounding rectangle of the characters in the string specified by text, which is the set of pixels the text would cover if drawn at (0, 0). The drawing, and hence the bounding rectangle, is constrained to the rectangle rect.</code></p>
<p dir="auto">The important detail is "if drawn at (0,0)", or rephrased, the upper left corner of the rasterized text. It's also worth heeding the warning slightly further down:<br />
<code> Note that the bounding rectangle may extend to the left of (0, 0), e.g. for italicized fonts, and that the text output may cover all pixels in the bounding rectangle.</code></p>
<blockquote>
<p dir="auto">How will that correspond to each item in a rich-text list in the <code>QTextEdit</code>, to include the leading "bullet point marker" plus any indentation which might be in effect at that point in the rich text document?</p>
</blockquote>
<p dir="auto">That's up to the caller. Bullet points and indentation are characters. Figure out what those are, and include them in the input string, or measure them independently by determining the bounding rectangle of empty list items.</p>
]]></description><link>https://forum.qt.io/post/803359</link><guid isPermaLink="true">https://forum.qt.io/post/803359</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Mon, 24 Jun 2024 08:46:13 GMT</pubDate></item><item><title><![CDATA[Reply to Is posible to get somehow line height in QTextEdit on Mon, 24 Jun 2024 08:16:30 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/jeremy_k">@<bdi>jeremy_k</bdi></a><br />
In that function the docs state</p>
<blockquote>
<p dir="auto">The drawing, and hence the bounding rectangle, is constrained to the rectangle <em>rect</em>.</p>
</blockquote>
<p dir="auto">In the OP's case what should they pass into <code>QFontMetrics::boundingRect(const QRect &amp;rect, ...)</code> for <code>rect</code>?  How will that correspond to each item in a rich-text list in the <code>QTextEdit</code>, to include the leading "bullet point marker" plus any indentation which might be in effect at that point in the rich text document?</p>
]]></description><link>https://forum.qt.io/post/803356</link><guid isPermaLink="true">https://forum.qt.io/post/803356</guid><dc:creator><![CDATA[JonB]]></dc:creator><pubDate>Mon, 24 Jun 2024 08:16:30 GMT</pubDate></item><item><title><![CDATA[Reply to Is posible to get somehow line height in QTextEdit on Mon, 24 Jun 2024 06:32:12 GMT]]></title><description><![CDATA[<p dir="auto">Use <a href="https://doc.qt.io/qt-6/qfontmetrics.html#boundingRect-3" target="_blank" rel="noopener noreferrer nofollow ugc">QFontMetrics::boundingRect(const QRect &amp;rect, int flags, const QString &amp;text, int tabStops, int *tabArray)</a> to determine the rendered size of the line of text.</p>
]]></description><link>https://forum.qt.io/post/803352</link><guid isPermaLink="true">https://forum.qt.io/post/803352</guid><dc:creator><![CDATA[jeremy_k]]></dc:creator><pubDate>Mon, 24 Jun 2024 06:32:12 GMT</pubDate></item></channel></rss>