PDA

View Full Version : QTextBrowser and friend function



probine
14th December 2006, 15:54
I have a class that creates a window and a text browser. The text browser is displayed in the window.

I want other classes to be able to print something in that text browser, therefore I have decided to declare it "friend". With this idea, any class can just "include" the window class an be able to print something.

The window class has this function:


friend void displayMessage(QString message){
textBrowser->append(message);
}


Note that textBrowser is public in the window class.

How can I make something like this work ?

sunil.thaha
14th December 2006, 16:20
How many instances of that Browser would be there ? if there is only one instance then using singleton pattern would be better.

probine
14th December 2006, 16:28
Just one instance.

I am not going to instantiate the class again, I just want to use the textBrowser from the other classes, though not of them will instantiate it.

The issue is not with the instances (I think), in stead, the issue is with how friend functions access public members.

sunil.thaha
14th December 2006, 16:53
For the friend function to access the data-members, it must have an instance of the TextBrowser, the class in which it is declared as friend.

I your case I am getting soln. like
1. Implement a singleton pattern
2. Create a slot in MainWindow and let other emit a signal
3. Pass a pointer to of the TextBrowser to the classes that wants access to the TextBrowser
4. Have a function in the MainWindow that returns pointer to he textBrower. Other classes use this function to handle theBrowser

wysota
14th December 2006, 17:03
If the text browser is public, then what sense does it make to declare the displayMessage() a friend function? You can call the text browser directly, without being friends with the class.