Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Syntax highlighting in a QLineEdit
Forum Updated to NodeBB v4.3 + New Features

Syntax highlighting in a QLineEdit

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 4 Posters 1.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Caeden
    wrote on last edited by
    #1

    Is there a way to have syntax highlighting in a QLineEdit? Some markdown thing
    When I say syntax highling I mean something like

    class X:
    

    in a QLineEdit. I have given up on trying to do this in a QTextEdit because the markdown worked but didnt show the colours.
    QTextEdit also doesn't have the QCompleter which is necessary for me.
    So I have resorted to chaining lots of QLineEdits without the border and padding so it looks like one QTextEdit. Again, probably with scrolling but it will have to do.
    I say this incase anyone has any answers to these problems with QTextEdit.

    Chris KawaC 1 Reply Last reply
    1
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #4

      QSyntaxHighlighter is an abstract class that provides the highlighting interface. You can't instantiate it.
      You need to inherit it and provide your implementation of that interface.

      Took me 5s of googling to find an implementation for Python: MarkdownHighlighter. It even supports custom themes.

      1 Reply Last reply
      3
      • C Caeden

        Is there a way to have syntax highlighting in a QLineEdit? Some markdown thing
        When I say syntax highling I mean something like

        class X:
        

        in a QLineEdit. I have given up on trying to do this in a QTextEdit because the markdown worked but didnt show the colours.
        QTextEdit also doesn't have the QCompleter which is necessary for me.
        So I have resorted to chaining lots of QLineEdits without the border and padding so it looks like one QTextEdit. Again, probably with scrolling but it will have to do.
        I say this incase anyone has any answers to these problems with QTextEdit.

        Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by
        #2

        @Caeden said:

        So I have resorted to chaining lots of QLineEdits without the border and padding so it looks like one QTextEdit

        That's like having an array of one letter strings instead of a single string. That's horrible, don't do that!

        Is there a way to have syntax highlighting?

        There's a QSyntaxHighlighter that is used to color text documents. There's no built-in markdown highlighter provided by Qt though, so you have to implement one yourself. Example: Syntax Highlighter Example. There's probably some existing 3rd party implementation too. Just google it.

        QTextEdit also doesn't have the QCompleter which is necessary for me.

        It doesn't use one out of the box, but nothing is stopping you from adding it. Example: Custom Completer Example.

        Or you can go the lazy way and just use some 3rd party widget like this one.

        C 1 Reply Last reply
        2
        • Chris KawaC Chris Kawa

          @Caeden said:

          So I have resorted to chaining lots of QLineEdits without the border and padding so it looks like one QTextEdit

          That's like having an array of one letter strings instead of a single string. That's horrible, don't do that!

          Is there a way to have syntax highlighting?

          There's a QSyntaxHighlighter that is used to color text documents. There's no built-in markdown highlighter provided by Qt though, so you have to implement one yourself. Example: Syntax Highlighter Example. There's probably some existing 3rd party implementation too. Just google it.

          QTextEdit also doesn't have the QCompleter which is necessary for me.

          It doesn't use one out of the box, but nothing is stopping you from adding it. Example: Custom Completer Example.

          Or you can go the lazy way and just use some 3rd party widget like this one.

          C Offline
          C Offline
          Caeden
          wrote on last edited by Caeden
          #3

          @Chris-Kawa These seem very useful but they're for C++ as far as I am aware

          For example;
          When I use self.highlighter = QtGui.QSyntaxHighlighter(self.code_input.document())
          Gives me an error of
          TypeError: PyQt5.QtGui.QSyntaxHighlighter represents a C++ abstract class and cannot be instantiated

          And I can't see a lazy 3rd party widget for PyQt

          1 Reply Last reply
          0
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by
            #4

            QSyntaxHighlighter is an abstract class that provides the highlighting interface. You can't instantiate it.
            You need to inherit it and provide your implementation of that interface.

            Took me 5s of googling to find an implementation for Python: MarkdownHighlighter. It even supports custom themes.

            1 Reply Last reply
            3
            • C Offline
              C Offline
              Caeden
              wrote on last edited by
              #5

              After further research on the markdown highlighter I seem to have a way to highlight my code. Thank you so much for this, you're a hero.

              O RokeJulianLockhartR 2 Replies Last reply
              0
              • C Caeden

                After further research on the markdown highlighter I seem to have a way to highlight my code. Thank you so much for this, you're a hero.

                O Offline
                O Offline
                OAnimal
                wrote on last edited by OAnimal
                #6
                This post is deleted!
                1 Reply Last reply
                0
                • C Caeden

                  After further research on the markdown highlighter I seem to have a way to highlight my code. Thank you so much for this, you're a hero.

                  RokeJulianLockhartR Offline
                  RokeJulianLockhartR Offline
                  RokeJulianLockhart
                  wrote last edited by RokeJulianLockhart
                  #7

                  @Caeden, is your implementation available anywhere? It being in Python is valuable for me. BTW, for anyone else who sees this in the future, there is also stackoverflow.com/q/53127561.

                  When using a forum, remember to tag the person you are responding to, in case they are not subscribed to the thread.

                  1 Reply Last reply
                  0

                  • Login

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved