I want to center the text of my QTextEdit horizontally and vertically.
I tried this, but it didn't work.
m_myTextEdit = new QTextEdit("text edit", m_ui->centralWidget);
m_myTextEdit->setGeometry(5, 50, 400, 250);
m_myTextEdit->setReadOnly(true);
m_myTextEdit->setAlignment(Qt::AlignCenter);
Is there a opportunity to set it centered with a StyleSheet?
If you only need one line, you can use a
QLineEdit
instead:If you only want to display the text, not allow the user to edit it, you can use a
QLabel
instead. This works with line wrapping, too:Here is code from PySide that I use for this, for those that need to use QTextEdit rather than QLineEdit. It is based on my answer here: https://stackoverflow.com/a/34569735/1886357
Here is the code, but the explanation is at the link: