Hello, I am relatively new to python and I am tryin to create, or better yet, find a text editor/box which displays simple html text/formatting and allows me to modify it (without having to deal with the html directly). For example in the text box you might find this:

      1. Hello World
         a. Hello
         b. World
      2. Hello again
         a. Hello
         b. Again    # i hit enter here
         c.          # and the c automatically pops up
      3.             # I hit backspace and it goes to the 3.

Does anyone know of anything open source like this or any suggestions on how to get started?

Thank you,

PS

I do not understand.

Where is the html? What is a,b,c supposed to mean?

Thank you for replying. I have found what I have been looking for - QtextEdit.

What I am trying to do is create a simple texteditor that keeps everything in an ordered list and prevents me from hitting backspace and getting out of the ordered list (if that makes sense?). As of now I have run into another problem which is I can't get my tabPressed function to get called. Maybe you can help me out with that?

This is what I have so far...

import sys
from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
from PyQt4 import *


class textEditor(QtGui.QTextEdit):
    def __init__(self):
        super(textEditor, self).__init__()        
        self.initUI()

        #connections
        self.connect(self, SIGNAL("tabPressed"),
                     self.tabPressed)

    def initUI(self):
        self.insertHtml("""
        <ol>
        <li>Start here...</li>
        </ol>""")        

    def tabPressed(self):
        self.insertHtml("""
        <ul>
        <li>Add some more...</li>
        </ul>""")    

class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)
        self.layout = QtGui.QVBoxLayout(self)
        self.layout.addWidget(textEditor())

def main():
    app = QtGui.QApplication(sys.argv)
    main = MyWindow()
    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

Thank you,
PS

I cannot test the code here, but my guess is that setfocus consumes the event.
You should implement the event method in texteditor.

        def event(self, event):
            if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Tab:
                 self.insertHtml("""
                <ul>
                <li>Add some more...</li>
                </ul>""") 
                #I think it should return False if the event was processed, and do not want further processing
                return False
            return QWidget.event(self, event)

Yup. That did the trick.

Now my next task is just to make it so I can change the the ordered list style. I am trying to do this simply by adding some more html but I have not been having any luck. I tried finding the cursor position to add html after some tags but it only gives me the position in the data seen in the text editor, not the html document. Just as a note, i am using the key sequence CTRL-SHIFT-X to insert the Html.

As a separate question, do you know how to prevent QTextEdit from preventing any formatting?

import sys
from PyQt4.QtCore import * 
from PyQt4.QtGui import * 
from PyQt4 import *
from HTMLParser import HTMLParser

class cardMaker():
    pass

class module(QtGui.QTextDocument):

    def __init__(self):
        super(module, self).__init__()
        self.setHtml("""<ol><li>Start here...</li></ol>""")

class textEditor(QtGui.QTextEdit):
    tabLevel = 0

    def __init__(self):
        super(textEditor, self).__init__()        
        self.initUI()
        txtCursor = self.textCursor()

        #connections
        self.connect(self, SIGNAL(""), self.event)

        #shortcuts
        self.shcut1 = QtGui.QShortcut(self)
        self.shcut1.setKey("CTRL+SHIFT+X")
        self.connect(self.shcut1, QtCore.SIGNAL("activated()"), self.displayHTML)

    def initUI(self):
        pass 
        # self.insertHtml("""
        # <ol>
        # <li>Start here...</li>
        # </ol>""")      

    def event(self, event):
        if event.type() == QEvent.KeyPress:
            if event.key() == Qt.Key_Tab:
                self.setTextCursor(cursor)
                self.insertHtml("""
                <ul>
                <li>&#8593;</li>
                </ul>""") 
                return False    
        return QWidget.event(self, event)

    def displayHTML(self):

        txtCursor = self.textCursor()
        print txtCursor.position()
        self.moveCursor(QTextCursor.EndOfLine, QTextCursor.MoveAnchor)
        self.insertPlainText("""
        </ol>
        <li>Start here...</li>
        <ol>
        """)    
        rawHtml = self.toHtml()
        print rawHtml
        # print textstuff
        # parser = MyHTMLParser()
        # print parser.handle_data(textstuff)


class MyWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyWindow, self).__init__(parent)

        # button1 = QtGui.QPushButton("test button", self)

        self.layout = QtGui.QVBoxLayout(self)
        Module = module()
        Editor = textEditor()
        Editor.setDocument(Module)
        self.layout.addWidget(Editor)
        # self.layout.addWidget(button1)

def main():
    app = QtGui.QApplication(sys.argv)
    main = MyWindow()
    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.