ino*_*vel 1 python pyqt qlineedit python-3.x pyqt5
我搜索了但找不到将 QLineEdit 中的文本居中对齐的解决方案
例子:
对齐:Qt::对齐
此属性保存行编辑的对齐方式
这里允许水平和垂直对齐,Qt::AlignJustify 将映射到 >Qt::AlignLeft。
默认情况下,此属性包含 Qt::AlignLeft 和 Qt::AlignVCenter 的组合。
from PyQt5 import QtWidgets, QtCore
class Widget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.line_edit = QtWidgets.QLineEdit()
self.line_edit.setAlignment(QtCore.Qt.AlignCenter) # <-----
self.line_edit.textChanged.connect(self.on_text_changed)
layout = QtWidgets.QVBoxLayout()
layout.addWidget(self.line_edit)
self.setLayout(layout)
def on_text_changed(self, text):
width = self.line_edit.fontMetrics().width(text)
self.line_edit.setMinimumWidth(width)
if __name__ == '__main__':
app = QtWidgets.QApplication([])
mw = Widget()
mw.show()
app.exec()
Run Code Online (Sandbox Code Playgroud)