Results 1 to 6 of 6

Thread: QSyntaxHighligher with QTextEdit

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2010
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default QSyntaxHighligher with QTextEdit

    Hi,

    First of all I need to say that I'm very new to Qt and C++ so there is a lot I still don't understand with the language.
    Anyhow I'm trying to insert a syntax highlighter for QTextEdit and it works if I follow this tutorial:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. QTextEdit editor;
    5. HtmlSyntaxHighlighter highlighter(editor.document()); // my subclass
    6. editor.show();
    7. return app.exec();
    8. }
    To copy to clipboard, switch view to plain text mode 

    However I want to apply this highlighter to my QTextEdit in my QTagWidget and the only way I can find to insert a QTextEdit is like this:
    Qt Code:
    1. QTextEdit *myTextEdit = new QTextEdit();
    2. HtmlSyntaxHighlighter highlighter(myTextEdit->document()); // doesn't do anything
    3. myTabWidget->addTab(myTextEdit, tr("foobar"));
    To copy to clipboard, switch view to plain text mode 

    So whats the difference between between these two declarations?
    Qt Code:
    1. QTextEdit *myTextEdit = new QTextEdit(); // now I need to use: myTextEdit->publicFunction()
    2.  
    3. QTextEdit editor; // now I need to use: myTextEdit.publicFunction()
    To copy to clipboard, switch view to plain text mode 

    And why doesn't the the syntax highlighter work?

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QSyntaxHighligher with QTextEdit

    Quote Originally Posted by dennis View Post
    So whats the difference between between these two declarations?
    Most likely the scope. It might be that your highlighter doesn't exist anymore.
    But to be sure of this, can you post the complete code where you add the text edit to the tab?

  3. #3
    Join Date
    Jun 2010
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSyntaxHighligher with QTextEdit

    Aha, thanks for pointing that out, I had to use:

    Qt Code:
    1. HtmlSyntaxHighlighter *highlighter = new HtmlSyntaxHighlighter(myTextEdit->document());
    To copy to clipboard, switch view to plain text mode 

    although I don't quite understand what a scope is and when to use what.
    Is the scope the .h file?

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QSyntaxHighligher with QTextEdit

    No, the scope is what is between { and }

    Example:
    If you have a function like this:
    Qt Code:
    1. void myFunction()
    2. {
    3. int i = 1;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Then i will only exist in the scope of the function myFunction(), thus between the { and }

    Similar for your highlighter. You defined it inside the scope of a function, not outside. Therefor it stops existing at the end of that function.
    To solve this, you can, like you did, use a pointer. Or define your highlighter in the class definition.

  5. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: QSyntaxHighligher with QTextEdit

    To clarify something:
    When your objects need to exist beyond the scope of a function, both the following declarations are wrong!

    Qt Code:
    1. void myFunction()
    2. {
    3. MyClass1 nameOfTheObject;
    4. MyClass2 *nameOfTheSecondObject = new MyClass;
    5. }
    To copy to clipboard, switch view to plain text mode 

    While the memory of the second object will still exist after the scope of your function, it goes out of scope, meaning that you don't have any control over it anymore. To be 100% correct, you should delete the pointer before going out of scope.

    To solve this, do this:
    Qt Code:
    1. class MySuperClass : public QObject
    2. {
    3. ...
    4. private:
    5. MyClass1 nameOfTheFirstObject;
    6. MyClass2 *nameOfTheSecondObject;
    7. };
    To copy to clipboard, switch view to plain text mode 

    Depending on how the pointer is managed, it needs to be deleted in the destructor of your class.

  6. #6
    Join Date
    Jun 2010
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSyntaxHighligher with QTextEdit

    Thanks for all the help, I didn't know about the scope/pointer thing.

Similar Threads

  1. QTextEdit
    By qoo in forum Newbie
    Replies: 6
    Last Post: 28th July 2009, 18:05
  2. QTextEdit
    By ocean in forum Qt Programming
    Replies: 4
    Last Post: 13th September 2008, 12:19
  3. QTextEdit
    By veda in forum Qt Programming
    Replies: 4
    Last Post: 11th September 2007, 07:30
  4. Replies: 1
    Last Post: 16th February 2007, 07:22
  5. 4.1 to 4.1.1, qtextedit
    By jey in forum Qt Programming
    Replies: 5
    Last Post: 9th March 2006, 12:48

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.