Results 1 to 5 of 5

Thread: friend class - or what else to do?

  1. #1
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    25
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default friend class - or what else to do?

    I have a QTabWidget with four tabs. The only one enabled to start with is the first, called Welcometab.
    All tabs are QWidgets, with different content.
    The Welcome widget holds som QLabels, with Project-info.
    When you press Ctrl+N, you get a dialog where you can fill in Project data. If data are accepted, the are transferred to a QDomDocument and saved. This works fine.
    My problem is to transfer the Project data to the labels on the front Tab. How do I do that?
    Right now I am trying with friend classes.
    Lani is the clas called from main().
    I include "welcometab.h" and have and empty declaration of the Welcometab class. In the Welcometab class I declare Lani as a friend. Is this not right?
    From Lani.h:
    Qt Code:
    1. #include "welcometab.h"
    2. class WelcomeTab;
    3. class Lani : public QWidget
    4. {
    5. Q_OBJECT
    To copy to clipboard, switch view to plain text mode 
    from welcometab.h:
    Qt Code:
    1. class WelcomeTab : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit WelcomeTab(QWidget *parent = 0);
    6.  
    7. friend class Lani;
    To copy to clipboard, switch view to plain text mode 
    I hope you can help. Thank You!

    David

  2. #2
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: friend class - or what else to do?

    This is not a Qt question as far as I understand.

    1. use QFile to store the project data(as xml file)
    2. or hold your project data as a member of your tab's parent widget.

  3. #3
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    25
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: friend class - or what else to do?

    I have used QFile, and store the project data as xml. It works fine.

    Besides that I want to write Project details om the Welcome-tab
    Here is the "welcometab.cpp" file:
    Qt Code:
    1. #include "welcometab.h"
    2.  
    3. WelcomeTab::WelcomeTab(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6.  
    7. view = new QWebView(parent);
    8. view->setMinimumWidth(700);
    9. leftbox = new QVBoxLayout();
    10. leftbox->addWidget(view);
    11.  
    12. infoLayout = new QGridLayout();
    13. labProjName = new QLabel(tr("Project Name:"),this);
    14. infoLayout->addWidget(labProjName);
    15. labProjNameText = new QLabel("",this);
    16. infoLayout->addWidget(labProjNameText);
    17.  
    18. rightbox = new QVBoxLayout();
    19. rightbox->addLayout(infoLayout);
    20. rightbox->addStretch();
    21.  
    22. layout = new QHBoxLayout();
    23. layout->setSpacing(0);
    24. layout->addLayout(leftbox);
    25. layout->addLayout(rightbox);
    26.  
    27. setLayout(layout);
    28. }
    29.  
    30. void WelcomeTab::setTextProjName(QString s){
    31. labProjNameText->setText(s);
    32. }
    To copy to clipboard, switch view to plain text mode 
    In the Lani.cpp constructor you'll find:
    Qt Code:
    1. tabWidget = new QTabWidget;
    2. tabWidget->setShortcutEnabled(0,true);
    3.  
    4. welcomeTab = new WelcomeTab();
    5. tabWidget->addTab(welcomeTab,tr("&1 Welcome"));
    To copy to clipboard, switch view to plain text mode 
    In Lani.h I declare welcomeTab:
    Qt Code:
    1. QWidget *welcomeTab;
    To copy to clipboard, switch view to plain text mode 
    What I can't understand is: Why can't I write
    Qt Code:
    1. welcomeTab.setTextProjName("somestring");
    To copy to clipboard, switch view to plain text mode 
    It really puzzles me...

  4. #4
    Join Date
    Dec 2008
    Location
    Istanbul, TURKEY
    Posts
    537
    Thanks
    14
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows Android

    Default Re: friend class - or what else to do?

    Ok but, where you think you read the project name? From the file you created right?

    If so, then you need to open that file and gather it's contents very much like;

    Qt Code:
    1.  
    2. doc.setContent( yourProjectXMLFile->readAll(),..);
    To copy to clipboard, switch view to plain text mode 

    then

    Qt Code:
    1. QString proj_name = doc.attribute(tagname);
    To copy to clipboard, switch view to plain text mode 

    then

    Qt Code:
    1. welcomeTab->setProfName(prof_name);
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to zgulser for this useful post:

    davidlamhauge (4th May 2012)

  6. #5
    Join Date
    Sep 2010
    Location
    Denmark
    Posts
    25
    Thanks
    10
    Thanked 3 Times in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: friend class - or what else to do?

    Thanks for the suggestions, and your help.

    I've just discovered the mistake that has been haunting me the last three days.
    I declared welcomeTab as a QWidget?!
    It is not a QWidget - it is a WelcomeTab...

    Thanks again, and excuse my sillyness (Arghhhhhhhhhh!)

Similar Threads

  1. Problem in updating listwidget of friend class
    By dipeshtech in forum Qt Programming
    Replies: 19
    Last Post: 25th March 2011, 15:18
  2. friend function in QT
    By aj2903 in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2009, 14:59
  3. friend class in TC++ book
    By mickey in forum General Programming
    Replies: 11
    Last Post: 25th August 2008, 22:08
  4. QTextBrowser and friend function
    By probine in forum Qt Programming
    Replies: 4
    Last Post: 14th December 2006, 17:03
  5. declaring a friend
    By jayw710 in forum Qt Tools
    Replies: 2
    Last Post: 18th April 2006, 04:12

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.