Я ищу решение для своей проблемы. Что я сделал? Я написал подкласс с именем ExtendedTruncateTextLineEdit, который наследуется от QLineEdit. Что я хочу? Ну, я хочу, чтобы обрезал текст в Qwidget с именем QLineEdit, когда вы изменяете размер окна, а QLineEdit становится меньше, чем контент. Следующий код действительно работает, но виджет QLineEdit выглядит как QLabel. Что я должен сделать, что следующий код рисует также мой QLineEdit?Python/PyQT: как я могу усечь текст в QLineEdit
import sys
from PyQt4.QtCore import Qt
from PyQt4.QtGui import QApplication,\
QLineEdit,\
QLabel,\
QFontMetrics,\
QHBoxLayout,\
QVBoxLayout,\
QWidget,\
QIcon,\
QPushButton,\
QToolTip,\
QBrush,\
QColor,\
QFont,\
QPalette,\
QPainter
qt_app = QApplication(sys.argv)
class Example(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setMinimumWidth(100)
self.init_ui()
def init_ui(self):
v_layout = QVBoxLayout()
v_layout.addStretch(1)
lbl = ExtendedTruncateTextLabel("This is a really, long and poorly formatted runon sentence used to illustrate a point", self)
#lbl.setText("This is a really, long and poorly formatted runon sentence used to illustrate a point")
lbl_1 = ExtendedTruncateTextLabel(self)
lbl_1.setText("Dies ist ein normaler Text")
l_text = ExtendedTruncateTextLineEdit()
l_text.setText("In the widget namend QLineEdit is also a very long text")
v_layout.addWidget(lbl)
v_layout.addWidget(lbl_1)
v_layout.addWidget(l_text)
self.setLayout(v_layout)
def run(self):
self.show()
qt_app.exec_()
class ExtendedTruncateTextLineEdit(QLineEdit):
def __init(self, parent):
QLineEdit.__init__(self, parent)
def paintEvent(self, event):
""" Handle the paint event for the title bar.
This paint handler draws the title bar text and title buttons.
"""
super(ExtendedTruncateTextLineEdit, self).paintEvent(event)
painter = QPainter(self)
metrics = QFontMetrics(self.font())
elided = metrics.elidedText(self.text(), Qt.ElideMiddle, self.width())
painter.drawText(self.rect(), self.alignment(), elided)
if __name__ == '__main__':
app = Example()
app.run()
Что такое ** ExtendedTruncateTextLabel **? – Marcus