PDA

View Full Version : friend class - or what else to do?



davidlamhauge
4th May 2012, 04:32
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:

#include "welcometab.h"
class WelcomeTab;
class Lani : public QWidget
{
Q_OBJECT
from welcometab.h:

class WelcomeTab : public QWidget
{
Q_OBJECT
public:
explicit WelcomeTab(QWidget *parent = 0);

friend class Lani;
I hope you can help. Thank You!

David

zgulser
4th May 2012, 10:11
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.

davidlamhauge
4th May 2012, 10:35
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:


#include "welcometab.h"

WelcomeTab::WelcomeTab(QWidget *parent) :
QWidget(parent)
{

view = new QWebView(parent);
view->setMinimumWidth(700);
leftbox = new QVBoxLayout();
leftbox->addWidget(view);

infoLayout = new QGridLayout();
labProjName = new QLabel(tr("Project Name:"),this);
infoLayout->addWidget(labProjName);
labProjNameText = new QLabel("",this);
infoLayout->addWidget(labProjNameText);

rightbox = new QVBoxLayout();
rightbox->addLayout(infoLayout);
rightbox->addStretch();

layout = new QHBoxLayout();
layout->setSpacing(0);
layout->addLayout(leftbox);
layout->addLayout(rightbox);

setLayout(layout);
}

void WelcomeTab::setTextProjName(QString s){
labProjNameText->setText(s);
}
In the Lani.cpp constructor you'll find:


tabWidget = new QTabWidget;
tabWidget->setShortcutEnabled(0,true);

welcomeTab = new WelcomeTab();
tabWidget->addTab(welcomeTab,tr("&1 Welcome"));

In Lani.h I declare welcomeTab:


QWidget *welcomeTab;

What I can't understand is: Why can't I write

welcomeTab.setTextProjName("somestring");
It really puzzles me...

zgulser
4th May 2012, 12:32
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;



QDomDocument doc;

doc.setContent( yourProjectXMLFile->readAll(),..);



then




QString proj_name = doc.attribute(tagname);



then



welcomeTab->setProfName(prof_name);

davidlamhauge
4th May 2012, 13:11
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!)