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 substring from QString by giving pixel size?
Thanks!
Yes, you can use
QFontMetrics::elidedTextto do so. For example, to cut at 200 pixels use:The second parameter indicates the cut mode (more values here).
UPDATE: I'm not sure if it is a bug in Qt (Qt 5.10.0 for me), but indeed the
Qt::ElideNonereturns the same string. Here a workaround:A whole working example can be found here (you will need to add files to a new Qt project).