PDA

View Full Version : QSyntaxHighligher with QTextEdit



dennis
8th June 2010, 19:01
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 (http://doc.trolltech.com/qq/qq21-syntaxhighlighter.html):

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QTextEdit editor;
HtmlSyntaxHighlighter highlighter(editor.document()); // my subclass
editor.show();
return app.exec();
}


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:


QTextEdit *myTextEdit = new QTextEdit();
HtmlSyntaxHighlighter highlighter(myTextEdit->document()); // doesn't do anything
myTabWidget->addTab(myTextEdit, tr("foobar"));


So whats the difference between between these two declarations?


QTextEdit *myTextEdit = new QTextEdit(); // now I need to use: myTextEdit->publicFunction()

QTextEdit editor; // now I need to use: myTextEdit.publicFunction()


And why doesn't the the syntax highlighter work?

Thanks in advance.

tbscope
8th June 2010, 19:06
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?

dennis
8th June 2010, 19:16
Aha, thanks for pointing that out, I had to use:


HtmlSyntaxHighlighter *highlighter = new HtmlSyntaxHighlighter(myTextEdit->document());

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

tbscope
8th June 2010, 19:25
No, the scope is what is between { and }

Example:
If you have a function like this:

void myFunction()
{
int i = 1;
}


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.

tbscope
8th June 2010, 19:40
To clarify something:
When your objects need to exist beyond the scope of a function, both the following declarations are wrong!


void myFunction()
{
MyClass1 nameOfTheObject;
MyClass2 *nameOfTheSecondObject = new MyClass;
}

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:

class MySuperClass : public QObject
{
...
private:
MyClass1 nameOfTheFirstObject;
MyClass2 *nameOfTheSecondObject;
};

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

dennis
8th June 2010, 19:46
Thanks for all the help, I didn't know about the scope/pointer thing.