<div dir="ltr"><div dir="ltr"><div dir="ltr">Il giorno ven 22 feb 2019 alle ore 02:37 John F Sturtz <<a href="mailto:john@sturtz.org">john@sturtz.org</a>> ha scritto:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div id="gmail-m_1289321739311075745__MailbirdStyleContent" style="font-size:12pt;font-family:Candara;color:rgb(0,0,0)"><div><span style="font-size:12pt"><span style="color:rgb(54,96,146)">This is a surprisingly hot-button issue!  I posted a question to stackoverflow that involved this issue, and someone grilled me quite extensively on my use of <font face="Courier New">QLabel</font> in this way.<br></span></span></div></div></blockquote><div><br></div><div>Well, that's understandable: as I pointed out (which is probably what that user told you about), that's not the purpose of a label. While it "can" work for simple scenarios, it's not intuitive from the user perspective, and can be an issue whenever the size of the cell doesn't allow to show the complete text the user is typing.</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div id="gmail-m_1289321739311075745__MailbirdStyleContent"><div>It may be that I could make it work fine using a QLineEdit. I haven't played with it much, and I probably should. There are two things I am doing with cell input that I think would be complicated using a QLineEdit:<br><br>Validating cell input character by character. Each column in the table view has regular expression validation. I catch a character when it is typed, determine what the cell contents would be with that character added, and disallow that input right at that moment if it would be invalid.<br>(I think this might be workable with a QLineEdit; I haven't messed with it ...)<br><br>There are cases where I want to have different text style for different parts of the cell contents (for example, some part of the text in italic and the rest normal).  This is quite easy to do with a QLabel because it can contain rich text -- e.g.,<br>setText('foo<span style="font-style: italic;">bar</span>')<br>This doesn't appear to work for the contents of a QLineEdit widget.<br>I did find this stackoverflow post regarding styling of text within a QLineEdit:<br><a href="https://stackoverflow.com/questions/14417333/how-can-i-change-color-of-part-of-the-text-in-qlineedit">https://stackoverflow.com/questions/14417333/how-can-i-change-color-of-part-of-the-text-in-qlineedit</a><br></div></div></blockquote><div><br></div><div></div></div><div>Since you need inline text formatting, implementing QLineEdit can be an issue as you'd probably need to do all the painting by yourself.<br><br></div><div>I'd suggest another approach instead: use a QTextEdit. It's implementation is a bit harder to get, but allows a better way to do what you need, while giving the user a standard and much more comfortable text editing UX.</div><div><br>Here's a small example I made which also uses QSyntaxHighlighter to do the formatting (in this case, it makes all occurrences of the word "red" in italic red).</div><div>It doesn't count in the "unallowed" text input you wrote about, but that can be done inside the keyPressEvent() method (but the release could need some checking too); you could also validate the input by connecting the textChanged() signal, to inhibit invalid clipboard pasting (like new line characters).</div><div><br></div><div><br></div><div><div><font face="monospace, monospace">class MyHighlighter(QtGui.QSyntaxHighlighter):</font></div><div><font face="monospace, monospace">    def __init__(self, *args, **kwargs):</font></div><div><font face="monospace, monospace">        QtGui.QSyntaxHighlighter.__init__(self, *args, **kwargs)</font></div><div><font face="monospace, monospace">        self.myFormat = QtGui.QTextCharFormat()</font></div><div><font face="monospace, monospace">        self.myFormat.setFontItalic(True)</font></div><div><font face="monospace, monospace">        self.myFormat.setForeground(QtCore.Qt.red)</font></div><div><font face="monospace, monospace">        <a href="http://self.re">self.re</a> = re.compile(r'(red)+')</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    def highlightBlock(self, text):</font></div><div><font face="monospace, monospace">        for match in self.re.finditer(text):</font></div><div><font face="monospace, monospace">            self.setFormat(match.start(), match.end() - match.start(), self.myFormat)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">class MyEditor(QtWidgets.QTextEdit):</font></div><div><font face="monospace, monospace">    submit = QtCore.pyqtSignal()</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    def __init__(self, *args, **kwargs):</font></div><div><font face="monospace, monospace">        QtWidgets.QTextEdit.__init__(self, *args, **kwargs)</font></div><div><span style="font-family:monospace,monospace">        self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)</span><br></div><div><font face="monospace, monospace">        self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)</font></div><div><font face="monospace, monospace">        self.setWordWrapMode(QtGui.QTextOption.NoWrap)</font></div><div><font face="monospace, monospace">        self.highlighter = MyHighlighter(self.document())</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    def keyPressEvent(self, event):</font></div><div><font face="monospace, monospace">        if event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return):</font></div><div><font face="monospace, monospace">            self.submit.emit()</font></div><div><font face="monospace, monospace">        elif event.key() == QtCore.Qt.Key_Tab:</font></div><div><font face="monospace, monospace">            # Ignore tab by letting the parent(s) handle it, as you are </font></div><div><font face="monospace, monospace">            # probably not interested in the tab character.</font></div><div><font face="monospace, monospace">            # Since you also want to prevent leaving the cell editing </font></div><div><font face="monospace, monospace">            # if the content is not valid, here you can also choose to</font></div><div><font face="monospace, monospace">            # accept the event, inhibiting cell departure.</font></div><div><font face="monospace, monospace">            # Please note that the shift-tab is intercepted by the view.</font></div><div><font face="monospace, monospace">            event.ignore()</font></div><div><font face="monospace, monospace">        else:</font></div><div><font face="monospace, monospace">            QtWidgets.QTextEdit.keyPressEvent(self, event)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">class MyDelegate(QtWidgets.QStyledItemDelegate):</font></div><div><font face="monospace, monospace">    def createEditor(self, parent, option, index):</font></div><div><font face="monospace, monospace">        editor = MyEditor(parent)</font></div><div><font face="monospace, monospace">        editor.setPlainText(index.data())</font></div><div><font face="monospace, monospace">        editor.submit.connect(lambda: self.submit(editor, index))</font></div><div><font face="monospace, monospace">        return editor</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    def submit(self, editor, index):</font></div><div><font face="monospace, monospace">        # You can also check for contents before proceeding with this</font></div><div><font face="monospace, monospace">        self.setModelData(editor, index.model(), index)</font></div><div><font face="monospace, monospace">        self.closeEditor.emit(editor, self.EditNextItem)</font></div><div><font face="monospace, monospace"><br></font></div><div><font face="monospace, monospace">    def setModelData(self, editor, model, index):</font></div><div><font face="monospace, monospace">        # This is required, otherwise you'll get the rich text contents as XHTML</font></div><div><font face="monospace, monospace">        model.setData(index, editor.toPlainText(), QtCore.Qt.DisplayRole)</font></div><br><br></div><div>Now you only have to setItemDelegate() on your view, and eventually set the highlighting [validation] by checking the index.column() argument in the createEditor() method according to your needs (or just create different delegates for each column).</div><div><br></div><div>Cheers!</div><div><br></div><div>Maurizio</div><div><br></div>-- <br><div dir="ltr" class="gmail_signature">È difficile avere una convinzione precisa quando si parla delle ragioni del cuore. - "Sostiene Pereira", Antonio Tabucchi<br><a href="http://www.jidesk.net" target="_blank">http://www.jidesk.net</a></div></div></div>