PDA

View Full Version : How to access Classes from another Classes



revellix
4th August 2011, 13:47
I have a basic c++ question and I cant find the answer, so I decided to ask you guys.

I am building a Qt Application with the following hierarchy:

1. MainWindow
2. SubWindow in mdiArea of MainWindow
3. Adding QTabWidget to the SubWindow
4. UserInputWidget as first Tab

Now in the UserInputWidget I am calling a function that computes a QVector. With the QVector I am building a QwtPlotCurve... from here on I want to add a Tab to the QTabWidget calling a Function in the UserInput.cpp that is declared in the SubWindow.cpp

But I have to set a reference to the SubWindow ... how can I do that ?

code till here:
Header

class SubWindow : public QMdiSubWindow
{
Q_OBJECT
public:
explicit SubWindow(QWidget *parent, int Form);

signals:

public slots:

public:
void addCurve(QWidget *curve);

CPP


void SubWindow::addCurve(QWidget *curve)
{
subWindowTabWidget.addTab(curve, curve->windowTitle());
}


UserInput calling the function


curvefhPlot = new curvefh;
curvefhPlot->fhPlotCurve->setSamples(calculationAlgoClass->fhCurvePointVector);
curvefhPlot->setWindowTitle(tr("F-h - Diagramm"));
SubWindow::addCurve(curvefhPlot); <---- SubWindow is not a declared Object...



I am building the subWindow over a function in the MainWindow...


SubWindow *MainWindow::createSubWindow()
{
SubWindow *subWindow = new SubWindow(this, 0);
mdiArea.addSubWindow(subWindow);
subWindow->showMaximized();
return subWindow;
}


any suggestions ?

yeye_olive
4th August 2011, 15:39
The problem is that SubWindow::addCurve() is not a static method, meaning you have to call it for a specific SubWindow instance.

You could have the UserInputWidget know about the SubWindow instance (say add a SubWindow* field) but it may not be the best solution as UserInputWidget would not be self-contained any more, and would be relying on the SubWindow instance not being destroyed.

Therefore it may be better to move the code adding the tab to a context which naturally knows of both the SubWindow and the UserInputWidget, like a slot in MainWindow. You can then have the UserInputWidget emit a signal with the appropriate data and connect it to that slot.