I have a similar issue as in this question, but the answer to that does not work well for me.
I have a QLineEdit element with a short piece of text, and I want this element to be adjusted in width to the contents, so that the text fits right into the element, without extra spaces or hidden letters. I use QFontMetrics for that, but the result is as if there is a bit of shift, so that leftmost letter is partially hidden, while there is a bit of space on the right:
My code is the following:
#include <QtWidgets>
int main ( int argc, char** argv) {
QApplication app (argc, argv);
QLineEdit *lineEdit = new QLineEdit;
QString text = "Hello, world!";
lineEdit->setText(text);
QFontMetrics fm = lineEdit->fontMetrics();
int w = fm.boundingRect(text).width();
// int w = fm.width(text);
lineEdit->setFixedWidth(w);
lineEdit->show();
return app.exec();
}
Playing with setAlignment made no any difference.