我正在为我的问题寻找解决方案。我做错什么了?我从QLineEdit继承了一个名为ExtendedTruncateTextLineEdit的子类。我想要什么?嗯,当你调整窗口大小时,当变得比内容小的时候,我想在一个名为 QLineEdit 的Qwidget中截断一个文本QLineEdit。下面的代码可以工作,但是QLineEdit-widget看起来像一个QLabel。我必须做什么,让下面的代码也绘制我的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()
https://stackoverflow.com/questions/36295020
复制相似问题