Mi chiedo come troncare il testo in una etichetta QL in base alla larghezza/altezza massima. Il testo in arrivo può essere di qualsiasi lunghezza, ma per mantenere un layout ordinato vorrei troncare le stringhe lunghe per riempire una quantità massima di spazio (larghezza/altezza massima del widget).PySide/PyQt troncano il testo in QLabel in base a minimumSize
Es .:
'A very long string where there should only be a short one, but I can't control input to the widget as it's a user given value'
sarebbe diventato:
'A very long string where there should only be a short one, but ...'
in base allo spazio necessario alle attuali esigenze di carattere.
Come posso ottenere il massimo?
Ecco un semplice esempio di quello che sto cercando, anche se questo si basa su conteggio delle parole, non disponibile spazio:
import sys
from PySide.QtGui import *
from PySide.QtCore import *
def truncateText(text):
maxWords = 10
words = text.split(' ')
return ' '.join(words[:maxWords]) + ' ...'
app = QApplication(sys.argv)
mainWindow = QWidget()
layout = QHBoxLayout()
mainWindow.setLayout(layout)
text = 'this is a very long string, '*10
label = QLabel(truncateText(text))
label.setWordWrap(True)
label.setFixedWidth(200)
layout.addWidget(label)
mainWindow.show()
sys.exit(app.exec_())
wow, è molto bello, grazie! –
@EricHulser, questa è una risposta molto buona. Molto utile. Molte grazie! – Phil