You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Thursday, April 26th 2007, 9:32pm

Themed collors in QTextEdit

Hi.

I would like to know if QTextEdit can handle collored text according to a
template, like KWrite does for code editing.

If not, could some one tell me how to do such thing?

If there isnt any objects witch do this, i could write my self one,
but if it exists... Zunge raus

Thanks

2

Friday, April 27th 2007, 12:41pm

Yes QTextEdit can do text collored.

sample code

QTextCharFormat format;
format.setForeground(color);
setFormat(start, count, format);


Look more on richtext example and syntaxhighlighter example.

3

Friday, April 27th 2007, 1:19pm

Thanks :D

Yes, shure it can do, and much more.

But what i whant to know is that is there any way to
make a template or something, so QTextEdit can collorize
(technicolor? :P ) according to it.

Something like kwrite does for code editing, when is C++
it colorize in a specific way, python, other way... and so on.

Iv tryed to download kdebase source code so i can see how
it do this, but with no success (damn network admin).

Thanks

4

Friday, April 27th 2007, 1:21pm

sorry, ignore last message :P

5

Friday, April 27th 2007, 7:09pm

Damn, im runing python and PyQt4 under windows XP , and
python crashes abnormaly when trying to do

<QTextEdit>.document()

;(

Will try to run under debian box to see what happen

6

Friday, April 27th 2007, 7:14pm

Here it goes the python source

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python

import sys
from PyQt4 import QtCore, QtGui, uic

class HighLighter (QtGui.QSyntaxHighlighter):
    class HighlightingRule:
        pattern = QtCore.QRegExp()
        format = QtGui.QTextCharFormat()

    highlightingRules = []
    
    
    keywordFormat = QtGui.QTextCharFormat()
    
        
    def __init__(self, parent=None):
        QtGui.QSyntaxHighlighter.__init__(parent)

        rule = self.HighlightingRule()

        self.keywordFormat.foreground = QtCore.Qt.darkBlue
        self.keywordFormat.fontWeight = QtGui.QFont.Bold
        
        keywordPatterns = QtCore.QStringList()
        keywordPatterns += "\\bmov\\b"
        keywordPatterns += "\\badd\\b"
        keywordPatterns += "\\bjmp\\b"
        keywordPatterns += "\\bjnz\\b"
        
        for i in keywordPatterns:
            rule.pattern = QtCore.QRegExp(i)
            rule.format = self.keywordFormat
            self.highlightingRules += [rule]
        print "num eh que eu cheguei aki mesmo"

    def highlightBlock(self, text):
        print "entrei"
        for i in self.highlightingRules:
            expression = QRegExp(i.pattern)
            index = text.indexOf(expression)
            while(index >= 0):
                length = expression.matchedLength()
                self.setFormat(index, length, i.format)
                index = text.indexOf(expression, index+length)
        
        
    
class FasmIde(QtGui.QMainWindow):
    def __init__(self, *args):
        QtGui.QWidget.__init__(self, *args)
        uic.loadUi("fasmIde.ui", self)


        a = HighLighter(self.txtEdit.document())
        self.txtEdit.document()
#    @QtCore.pyqtSignature("")

app = QtGui.QApplication(sys.argv)
widget = FasmIde()
widget.show()
app.exec_()


and the ui file


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<ui version="4.0" >
 <class>frmFasmIde</class>
 <widget class="QMainWindow" name="frmFasmIde" >
  <property name="geometry" >
   <rect>
    <x>0</x>
    <y>0</y>
    <width>800</width>
    <height>853</height>
   </rect>
  </property>
  <property name="windowTitle" >
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget" >
   <layout class="QVBoxLayout" >
    <property name="margin" >
     <number>9</number>
    </property>
    <property name="spacing" >
     <number>6</number>
    </property>
    <item>
     <widget class="QTextEdit" name="txtEdit" />
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menubar" >
   <property name="geometry" >
    <rect>
     <x>0</x>
     <y>0</y>
     <width>800</width>
     <height>19</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuHelp" >
    <property name="title" >
     <string>Help</string>
    </property>
   </widget>
   <widget class="QMenu" name="menuEditar" >
    <property name="title" >
     <string>Editar</string>
    </property>
   </widget>
   <widget class="QMenu" name="menuArquivo" >
    <property name="title" >
     <string>Arquivo</string>
    </property>
    <addaction name="actionAbrir" />
    <addaction name="separator" />
    <addaction name="actionSalvar" />
   </widget>
   <addaction name="menuArquivo" />
   <addaction name="menuEditar" />
   <addaction name="menuHelp" />
  </widget>
  <widget class="QStatusBar" name="statusbar" />
  <action name="actionAbrir" >
   <property name="text" >
    <string>Abrir</string>
   </property>
  </action>
  <action name="actionSalvar" >
   <property name="text" >
    <string>Salvar</string>
   </property>
  </action>
 </widget>
 <resources/>
 <connections/>
</ui>