Questions tagged [qfontmetrics]

The QFontMetrics class provides font metrics information.

QFontMetrics functions calculate the size of characters and strings for a given font.

Official documentation can be found here.

19 questions
5
votes
1 answer

QFontMetrics give strange results for monospace font

Сan someone explain to me results of this test program? #include #include #include int main(int argc, char *argv[]) { QApplication app(argc, argv); QFont font; font.fromString("Monospace,14"); …
poljak181
  • 585
  • 4
  • 12
4
votes
1 answer

Center text vertically when drawing with QPainter's drawText()

My strategy when centering text on images is to get bounding rectangle for that text and divide width or height by two. I did the same in this case. This is example I have created: void CanvasWidget::paintEvent(QPaintEvent*) { //Create image: …
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
3
votes
0 answers

Why the QFontMetrics::boundingRect() return a wrong size rect?

I'm using Qt4.7. When I use QFontMetrics to render my text in some situation, I got a wrong width. My code is like this: QFontMetrics fm(QApplication::font()); QRect rc = fm.boundingRect(str); I found that fm.boundingRect(str) always return a fixed…
2
votes
2 answers

Find the text width in QFont

I have two items in my tree model i have small difference in text alignment.Is this caused by the width of the text but i checked the width of text using QFontMetrics::width() but both text are same. Text1:111601756 Text2:999999996 As from the…
anbu selvan
  • 725
  • 3
  • 13
  • 41
2
votes
1 answer

QFontMetrics boundingRect

I'm having problems with the QFontMetrics 'boundingRect' function, it doesn't return the correct results. mfntArial = QFont("Arial", 12, QFont::Bold); QFontMetrics objMetrics(mfntArial); QRect rctLine =…
SPlatten
  • 5,334
  • 11
  • 57
  • 128
1
vote
1 answer

Why is there a mismatch in width provided by QFontMetrics and width shown by Qml Rectangle/ Text

I have written a qml and cpp file to validate and visualize QFontMetrics concept. #include #include #include int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine…
user12531295
1
vote
1 answer

Get Substring of QString by Pixels in Qt cpp

I have a QString who's pixel length I can get by QFontmetrics::width(), also I can have character length from QString by QString::length(), but here I am intended to cut QString by specific amount of pixels. Is there any way possible to have…
karan chavan
  • 215
  • 2
  • 12
1
vote
1 answer

How to set dynamic QFont size?

I came across QFontMetrics? http://doc.qt.io/qt-5/qfontmetrics.html This gives the height and width of the present font. I need to run my application with full screen mode on on different monitors for which I am using Scale class.…
Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411
0
votes
1 answer

When I use QFontMetrics in PyQt5, the numbers I get don't match the size of the text that I'm drawing with drawText

I'm trying to get the size of a text, so I can scale it accordingly to fit inside a box. But unfortunately the QFontMetrics.width() seems to give wrong outputs. Here's a code that draws a text, and uses values from QFontMetrics to draw a rect that…
Tom
  • 15
  • 5
0
votes
0 answers

QFontMetrics::horizontalAdvance() returns inconsistent results

I'm working on a function that determines the maximum number of characters that can fit in a given width (in pixels) based on the widest character in the string and then chops the string accordingly. I'm using the horizontalAdvance method of…
Nihilish
  • 105
  • 1
  • 16
0
votes
2 answers

Qt wrapping text in boundingRect

I am writing an application in PyQt and I need to wrap the text that I am painting. I use boundingRect method of QFontMetrics class to define the size and then QPainter.drawText to draw. I need the text to fit in a given rectangle. This is the line…
Vladislav
  • 35
  • 7
0
votes
1 answer

Qt/ Qml Frobihser font behaving differently in Linux and Windows environment

I have written a qtquickapplication sample program to display a text "none" within given rectangle. Rectangle border color is set to red to visualize whether text "none" is fitting properly within given width and height or not. I have compiled same…
RaviS Gupta
  • 95
  • 2
  • 11
0
votes
1 answer

Font Rendering difference in windows and Linux, Qt Cpp

I have, QFontMetrics m_fm(QFont("Arial",14)); and m_fm.width("Existing LAN IP Address from Project Network"); returns '297' (on windows) can anyone tell me what it returns on linux compiler? and if its different on linux then why so? and how…
karan chavan
  • 215
  • 2
  • 12
0
votes
1 answer

drawText on QGraphicsView::drawForeground

My code is below. void MyView::drawForeground(QPainter *painter, const QRectF &rect) { Q_UNUSED(rect); painter->save(); QRectF rt = viewport()->rect(); painter->setWorldMatrixEnabled(false); QString strInfo = "test12345"; …
0
votes
1 answer

How to interpret QFontMetrics results?

I have a problem making sense of the values I get from QFontMetrics // 43 characters 0123456789012345678901234567890123456789012 static const QString s("The quick brown fox jumps over the lazy dog"); // Hint: basically returns…
Horst Walter
  • 13,663
  • 32
  • 126
  • 228
1
2