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
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 ):
Code:
class Ui_MainWindow;
Population.cpp should remain as it is.
Regards
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:
Code:
#ifndef POPULATION_H
#define POPULATION_H
//class declarations, etc
#endif
Also, in Ui_mainwindow.h, if possible, store a pointer to Population and include population.h in the cpp only.
Regards
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
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