Results 1 to 5 of 5

Thread: How to access Main Window's members from an object belongig to a user defined class

  1. #1
    Join Date
    Jun 2007
    Location
    Plymouth, UK
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Angry How to access Main Window's members from an object belongig to a user defined class

    Hello folks,

    I'm experiencing a strange trouble with Qt 4.3.

    I created a class which inheritates from QMainWindow:

    [HTML]#include <QtGui>
    #include <QtCore/QVariant>

    #include "population.h"
    #include "myGraphicsView.h"

    class Ui_MainWindow : public QMainWindow {

    Q_OBJECT

    public:

    // (Simil)Constructor
    void setupUi(QMainWindow *MainWindow);
    ...
    Population currentPopulation;
    ...[/HTML]

    I also created a different class, called "Population", defined in the file population.h as follows:

    [HTML]#include "mainWindow.h"

    class Population : public QWidget {

    Q_OBJECT

    public:

    // Constructor
    Population(Ui_MainWindow* myWindow);
    ...[/HTML]

    The population.cpp file starts in this way:

    [HTML]#include "population.h"
    #include "mainWindow.h"

    // Constructor
    Population::Population(Ui_MainWindow* myWindow) {
    ...[/HTML]

    What I'd like to do is to be able to access the members of the Main Window inside the Population's class constructor (i.e., the statusbar, the filemenu and so on). Instead, when I try to compile, gcc gives me two times the same error: "expected ')' before '*' token", referring to the "Population(Ui_MainWindow* myWindow);" line of population.h. The error comes out when gcc tries to compile two different files, main.cpp and mainWindow.cpp: two files that have something in common with the MainWindow.

    Could someone please give me some hints on how to solve this problem?

    Thanks,
    Fabio

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to access Main Window's members from an object belongig to a user defined cla

    mainWindow.h is included multiple times, resulting in symbol redefinitions.
    Add #pragma once to the header( or ifdefs ), to make sure it will be compiled only once.

    Next, in pupulation.h don't include mainwindow.h. Instead add a forward declare to it( this is the preferred way ):
    Qt Code:
    1. class Ui_MainWindow;
    To copy to clipboard, switch view to plain text mode 

    Population.cpp should remain as it is.

    Regards

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to access Main Window's members from an object belongig to a user defined cla

    Actually, it is more like a recursive inclusion. Population includes main window, and the other way around.

    Add ifdefs to population.h too to make it compile only once.
    Something like:
    Qt Code:
    1. #ifndef POPULATION_H
    2. #define POPULATION_H
    3.  
    4. //class declarations, etc
    5.  
    6. #endif
    To copy to clipboard, switch view to plain text mode 

    Also, in Ui_mainwindow.h, if possible, store a pointer to Population and include population.h in the cpp only.

    Regards

  4. The following user says thank you to marcel for this useful post:

    fabietto (26th June 2007)

  5. #4
    Join Date
    Jun 2007
    Location
    Plymouth, UK
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to access Main Window's members from an object belongig to a user defined cla

    Hello Marcel,

    first of all, thank you very much for your quick answer. All my headers files already contains the #ifndef directive, but you're right in suggesting that because I didn't include that code portions in my post.

    I made a mistake speaking about the population object too. In fact, it was already declared, in the mainWindow.h, as a pointer (Population *current_population), and then created, in one slot function contained in mainWindow.cpp with the syntax: current_population = new Population(myMainWindow);

    I followed your tip related to the forward declaration. What I obtain now is a different error:

    [HTML]/Users/fabioruini/Documents/University/Plymouth/MAVs/Simulations/MAVs/population.h:27: error: invalid use of undefined type 'struct QWidget'
    /Library/Frameworks/QtCore.framework/Headers/qobject.h:49: error: forward declaration of 'struct QWidget'
    /Users/fabioruini/Documents/University/Plymouth/MAVs/Simulations/MAVs/population.h:27: warning: 'class Population' has virtual functions but non-virtual destructor[/HTML]

    Of course, thank you very much for your help!

    Fabio
    Last edited by fabietto; 26th June 2007 at 19:14. Reason: updated contents

  6. #5
    Join Date
    Jun 2007
    Location
    Plymouth, UK
    Posts
    36
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Smile Re: How to access Main Window's members from an object belongig to a user defined cla

    ehm... the last error I reported was simply a problem related to the missing #include <qwidget> directive. Now it works. Or, at least, it seems doing that.

    Thank you very much!!! :-)

    Fabio

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.