2

これは、テキスト編集から単語をコピーし、それをテーブルビューの新しい行に設定する方法です。私が必要としているのは: テキスト編集で選択した単語の色を変更するにはどうすればよいですか? 私のテキスト編集の名前は「editor」です。単語をコピーすると、この単語の色を変更する必要があり、その方法がわかりません。助けてください :)。例をお願いします~~

 def addLineTable(self):

    row = self.model.rowCount()   #create a line into my tableview
    self.model.insertRows(row)
    column = 0
    index = self.model.index(row, column)        
    tableView = self.TABLE            
    tableView.setFocus()
    tableView.setCurrentIndex(index)
    cursor = self.editor.textCursor()
    textSelected = cursor.selectedText()  #set text to cursor
    self.model.setData(index, QVariant(textSelected)) #set text to new tableview line
4

2 に答える 2

3

あなたはすでにQTextCursor. このカーソルにフォーマット ( ) を適用するだけQTextCharFormatで、選択したテキストがそれに応じてフォーマットされます。

def addLineTable(self):

    row = self.model.rowCount()   #create a line into my tableview
    self.model.insertRows(row)
    column = 0
    index = self.model.index(row, column)        
    tableView = self.TABLE            
    tableView.setFocus()
    tableView.setCurrentIndex(index)
    cursor = self.editor.textCursor()

    # get the current format
    format = cursor.charFormat()
    # modify it
    format.setBackground(QtCore.Qt.red)
    format.setForeground(QtCore.Qt.blue)
    # apply it
    cursor.setCharFormat(format)

    textSelected = cursor.selectedText()  #set text to cursor
    self.model.setData(index, QVariant(textSelected)) #set text to new tableview line
于 2013-02-04T22:09:47.323 に答える
3

私があなたの質問を正しく理解できれば、テキストの色を変更したいだけですよね? StyleSheetscss をQWidgetsドキュメントに割り当てることで、これを行うことができます

以下のサンプル:

from PyQt4 import QtGui, QtCore

class Window(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self._offset = 200
        self._closed = False
        self._maxwidth = self.maximumWidth()
        self.widget = QtGui.QWidget(self)
        self.listbox = QtGui.QListWidget(self.widget)
        self.editor = QtGui.QTextEdit(self)
        self.editor.setStyleSheet("QTextEdit {color:red}")
        layout = QtGui.QHBoxLayout(self)
        layout.addWidget(self.widget)
        layout.addWidget(self.editor)

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.move(500, 300)
    window.show()
    sys.exit(app.exec_())

編集

または、StyleSheet をすべての に設定できますQTextEdit。これを試してください。

......

app = QtGui.QApplication(sys.argv)
app.setStyleSheet("QTextEdit {color:red}")
......
于 2013-02-04T18:34:08.137 に答える