Results 1 to 5 of 5

Thread: QTextBrowser and friend function

  1. #1
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTextBrowser and friend function

    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:
    Qt Code:
    1. friend void displayMessage(QString message){
    2. textBrowser->append(message);
    3. }
    To copy to clipboard, switch view to plain text mode 

    Note that textBrowser is public in the window class.

    How can I make something like this work ?

  2. #2
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextBrowser and friend function

    How many instances of that Browser would be there ? if there is only one instance then using singleton pattern would be better.
    We can't solve problems by using the same kind of thinking we used when we created them

  3. #3
    Join Date
    Jan 2006
    Posts
    185
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextBrowser and friend function

    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.

  4. #4
    Join Date
    Jan 2006
    Location
    Kerala
    Posts
    371
    Thanks
    76
    Thanked 37 Times in 32 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextBrowser and friend function

    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
    We can't solve problems by using the same kind of thinking we used when we created them

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTextBrowser and friend function

    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.

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.