Results 1 to 2 of 2

Thread: How to access Classes from another Classes

  1. #1
    Join Date
    Jul 2011
    Qt products

    Question How to access Classes from another Classes

    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:
    Qt Code:
    1. class SubWindow : public QMdiSubWindow
    2. {
    3. Q_OBJECT
    4. public:
    5. explicit SubWindow(QWidget *parent, int Form);
    7. signals:
    9. public slots:
    11. public:
    12. void addCurve(QWidget *curve);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void SubWindow::addCurve(QWidget *curve)
    2. {
    3. subWindowTabWidget.addTab(curve, curve->windowTitle());
    4. }
    To copy to clipboard, switch view to plain text mode 

    UserInput calling the function
    Qt Code:
    1. curvefhPlot = new curvefh;
    2. curvefhPlot->fhPlotCurve->setSamples(calculationAlgoClass->fhCurvePointVector);
    3. curvefhPlot->setWindowTitle(tr("F-h - Diagramm"));
    4. SubWindow::addCurve(curvefhPlot); <---- SubWindow is not a declared Object...
    To copy to clipboard, switch view to plain text mode 

    I am building the subWindow over a function in the MainWindow...
    Qt Code:
    1. SubWindow *MainWindow::createSubWindow()
    2. {
    3. SubWindow *subWindow = new SubWindow(this, 0);
    4. mdiArea.addSubWindow(subWindow);
    5. subWindow->showMaximized();
    6. return subWindow;
    7. }
    To copy to clipboard, switch view to plain text mode 

    any suggestions ?

  2. #2
    Join Date
    Oct 2009
    Thanked 96 Times in 93 Posts
    Qt products
    Qt4 Qt5
    Unix/X11 Windows

    Default Re: How to access Classes from another Classes

    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.

Similar Threads

  1. Replies: 7
    Last Post: 11th April 2013, 11:55
  2. Replies: 3
    Last Post: 16th January 2011, 04:59
  3. construction of classes within classes
    By mobucl in forum Newbie
    Replies: 8
    Last Post: 10th January 2011, 14:51
  4. Access variables of other classes
    By przemek in forum Newbie
    Replies: 8
    Last Post: 2nd August 2010, 12:32
  5. fax classes in qt
    By dyams in forum Qt Programming
    Replies: 5
    Last Post: 7th September 2007, 10:14


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.