1

I have 2 Qt windows namely, MainWindow and GraphWindow; The GraphWindow class has a range of QCustomPlot widgets where data from other classes will be plotted. In the MainWindow implementation file, I instantiate a GraphWindow object and pass its address to other classes, so that all plots will be generated on the same window. But my problem is, when I try to pass the address of the GraphWindow objects, Qt throws the following error;

Unknown type name GraphWindow

I have included graphwindow.h in all there classes which uses the GraphWindow object.

This is GraphWindow class declaration;

#ifndef GRAPHWINDOW_H
#define GRAPHWINDOW_H

#include <QDialog>
#include <mainwindow.h>
#include <incomestatement.h>
#include <balancesheet.h>
#include <cashflow.h>
#include <dcf.h>
#include <QList>
#include <QStringList>
#include <QString>
#include <qcustomplot.h>
#include <QSharedPointer>
#include <QHash>

namespace Ui
{
    class GraphWindow;
}



class GraphWindow : public QDialog
{
    Q_OBJECT

public:
    explicit GraphWindow(QWidget *parent = 0);
    ~GraphWindow();
    void plotData(QStringList labels, QList<double> yData,QString xLabel, QString yLabel, QString slot);

private:
    Ui::GraphWindow *ui;
    QHash<QString, QCustomPlot* > dynamicWidgetHash;
    QStringList widgetStringList;

    QVector<double> setupXAxis(QStringList labels, QString slot, QString xLabel);
    void setupYAxis(QVector<double> yData, QString slot, QString yLabel);
    void setupLegend(QString slot);
    void setHash(QStringList widgetStringList);
};

#endif // GRAPHWINDOW_H

This is mainwindow.cpp where GrapWindow object is created;

void MainWindow::on_runButton_clicked()
{
    //Create grapher object and pass the address to all sub-classes to plot on the same widget
    GraphWindow graphWindow;

    if(globalPath.isEmpty() == false)
    {

        //Create input QXlsx::Document Object
        QXlsx::Document inputDoc(globalPath);

//      QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"),
//                                   "/home/jana/untitled.png",
//                                   tr("Images (*.png *.xpm *.jpg)"));

        //Create financial statement objects and compute values
        IncomeStatement incState(inputDoc,graphWindow);
        incState.computePoints(MainWindow::pointsListINC_ST,MainWindow::incSettingList,
                               MainWindow::incItemList,inputDoc);
    }
}

This is incomestatement.h, one if the classes using GraphWindow object.

#ifndef INCOMESTATEMENT_H
#define INCOMESTATEMENT_H

#include <QString>
#include <xlsxdocument.h>
#include <xlsxformat.h>
#include <QVector>
#include <QList>
#include <pointssystem.h>
#include <graphwindow.h>

class IncomeStatement
{
    //Declare PointsSystem as FRIEND of IncomeStatement to access private members
    friend class PointsSystem;
public:
    IncomeStatement(QXlsx::Document& inputDoc, GraphWindow& grapher);

    //Public member function to be called from mainwindow.cpp
    double computePoints(QList<QList<bool> > userSettings, QList<QStringList> itemStringList,
                         QStringList comboBoxItems, QXlsx::Document& inputDoc);

private:
    int cursorRow;
    int cursorCol;
    double scoredPoints;
    double totalPoints;

    QStringList timelineList;
    QList<double> revenueGrowthArr;
    QList<double>costOfRevenGrowthArr;
    QList<double>operIncGrowthArr;
    QList<double>netIncGrowthArr;
    QList<double>totOperExpGrowthArr;
    QList<double>grossMarginArr;
    QList<double>opIncMarginArr;
    QList<double>netIncMarginArr;


    QXlsx::Format format(QXlsx::Document& inputDoc, int mode);
    bool setSheetName(QXlsx::Document& inputDoc);
    void process(QXlsx::Document& inputDoc, GraphWindow& grapher);
    int searchKey(QString key,QXlsx::Document& inputDoc);
    int findCursorPointRow(QXlsx::Document& inputDoc);
    int findCursorPointCol(QXlsx::Document& inputDoc);
    void revenueGrowth(QXlsx::Document& inputDoc);
    void growthComputation(QXlsx::Document& inputDoc, QString criterion, QList<double>&storageList);
    void marginComputation(QXlsx::Document& inputDoc, QString criterion, QList<double>&storageList);
    QStringList extractTimeline(QXlsx::Document& inputDoc);
    void writeStockAnalysis(QXlsx::Document& inputDoc, QList<QList<QVariant> > data);
    void writeStockAnalysisHeader(QXlsx::Document& inputDoc);
    void writePointsSumData(QXlsx::Document& inputDoc);

#endif // INCOMESTATEMENT_H

Whats wrong here?

Vino
  • 2,111
  • 4
  • 22
  • 42

1 Answers1

2

You should not #include <mainwindow.h> from graphwindow.h, it doesn't make sense if mainwindow shall use graphwindow.

If you need the include, something's wrong with your design. In that case you might circumvent the problem by including mainwindow.h from the graphwindow.cpp, and providing forward declarations for everything needed from mainwindow in the graphwindow.h. Anyhow, instead of circumventing the problem you might want to rethink the design...

St0fF
  • 1,553
  • 12
  • 22
  • You are correct, I don't use it, I have mistakenly copied it there; forward declaration meaning, declaring a object of `GraphWindow` in `MainWindow` declaration? Am I correct? – Vino Feb 28 '17 at 13:22
  • No, not really. 1st: I meant it the other way around - i.e. inside graphwindow.h you declare `mainWindow`. 2nd: you wouldn't declare an object, but rather the class itself, like: `class MainWindow;` – St0fF Mar 01 '17 at 12:03
  • 1
    On a second note: if your project gets rather large, forward declaring classes instead of including their headers can optimize compile time. But this only works if all you ever declare to be used of the included class are references and pointers. – St0fF Mar 01 '17 at 12:07