Pass data from a new class to main: return type specification for constructor invalid



  • Hi all,

    Short story: I wish to pass data from a class to the mainwindow.cpp.

    Long story:
    To make my project a bit more clear I decided to create a new class. This class needs to search data in an xml file and pass them back to the mainwindow.

    In my mainwindows I call the following function:

    void MainWindow::on_pushButton_clicked()
    {
        qDebug()<<"clicked";
        ReadProgData(ui->currentProgCombo->currentText());
    }
    

    So I pass the current text of combobox "currentProgCombo" into the ReadProgData class. The following code is the code in the ReadProgData.h file:

    #ifndef READPROGDATA_H
    #define READPROGDATA_H
    
    #include <QDir>
    #include <QDomDocument>
    #include <QDomElement>
    #include <QFile>
    #include <QTextStream>
    #include <QFileDialog>
    #include <QDebug>
    #include <mainwindow.h>
    
    class ReadProgData
    {
    public:
        ReadProgData(QString machinename);
        ~ReadProgData();
    
        int readMachine(QDomElement root, QString tagname, QString Attribute, QString compare);
        int readMachineSettings(QDomElement root, QString tagname, QString Attribute, int fo);
        void readXML(QString machine);
    
    };
    
    #endif // READPROGDATA_H
    
    

    The following code is in ReadProgData.cpp

    #include "readprogdata.h"
    
    QString progsettings[50][2];
    
    int ReadProgData::readMachine(QDomElement root, QString tagname, QString Attribute,QString compare)
    {
        QDomNodeList items = root.elementsByTagName(tagname);
        qDebug()<< "total items = "  << items.count();
        int ID;
        for(int i=0; i<items.count();i++)
        {
            QDomNode itemnode = items.at(i);
    
            if(itemnode.isElement())
            {
                QDomElement itemele = itemnode.toElement();
                qDebug()<<itemele.attribute(Attribute);
    
                if(itemele.attribute(Attribute)==compare)
                {
                    ID=itemele.attribute("ID").toInt();
                }
    
    
            }
        }
    
        return ID;
    }
    
    int ReadProgData::readMachineSettings(QDomElement root, QString tagname, QString Attribute, int fo)
    {
        QDomNodeList items = root.elementsByTagName(tagname);
        qDebug()<< "total items = "  << items.count();
        int ID;
        for(int i=0; i<items.count();i++)
        {
            QDomNode itemnode = items.at(i);
    
            if(itemnode.isElement())
            {
                if(fo==0)
                {
                    QDomElement itemele = itemnode.toElement();
                    qDebug()<<itemele.attribute(Attribute);
                    progsettings[i][0]=itemele.attribute(Attribute);
    
                }
                else if(fo==1)
                {
                    QDomElement itemele = itemnode.toElement();
                    qDebug()<<itemele.attribute(Attribute);
                    progsettings[i][1]=itemele.attribute(Attribute);
    
                }
    
            }
        }
    
        return ID;
    }
    
    
    void ReadProgData::readXML(QString machine)
    {
        // DOM reading xml file
        QDomDocument document("MachineSetting");
    
        //load xml file
    
        QFile file("MyXML.xml");
        if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
        {
            qDebug() <<"Failed to open file";
            return;
        }
        else{
    
            if(!document.setContent(&file))
            {
                qDebug()<<"Failed to load document";
                return;
            }
            file.close();
         }
    
    
        // get the root element
        QDomElement root = document.firstChildElement();
    
        //List the element
    
        int mID = readMachine(root,"Machinename","Name",machine);
        qDebug()<<mID;
        QDomNodeList Machines = root.elementsByTagName("Machinename");
        QDomNode machinenode = Machines.at(mID);
        if(machinenode.isElement())
        {
            QDomElement Machine = machinenode.toElement();
            int oID = readMachineSettings(Machine, "Function", "Name",0);
            QDomNodeList machineSettings = root.elementsByTagName("Function");
    
                QDomNode outputs = machineSettings.at(oID);
                if(outputs.isElement())
                {
                    QDomElement value = outputs.toElement();
                    readMachineSettings(Machine, "Output", "Name",1);
                }
        }
    
    
    
    
    
    
        qDebug()<<"Function 1:" + progsettings[0][0] + " output:" + progsettings[0][1];
        qDebug()<<"Function 2:" + progsettings[1][0] + " output:" + progsettings[1][1];
    
    }
    
    
    
    ReadProgData::ReadProgData(QString machinename)
    {
        qDebug()<<machinename;
        readXML(machinename);
    
    }
    
    ReadProgData::~ReadProgData()
    {
    
    }
    

    So Like this everything works fine. I get the output:

    "Function 1: Clock output:1" 
    "Function 2: Output output:2" 
    

    Now I would like to copy this data into my mainwindow.cpp. So I should return the following variable:

     progsettings
    

    No I just learned that it isn't possible to return this variable from ReadProgData to mainwindow. When I try this I get the following error:

     error: return type specification for constructor invalid
    

    IF this isn't possible, how should I copy this variable from ReadProgData to mainwindow?

    As you can see I'm not a pro, So if you see stupid things in my code, pleas feel free to give constructive criticism. I do this because I want to learn!

    Kind regards,
    TMJJ


  • Lifetime Qt Champion

    Hi
    Its very close to working
    But you ran into a tech limitation. Constructors are not allow to return anything.
    So all that you need is just a new processing function

    void MainWindow::on_pushButton_clicked()
    {
    ReadProgData Data(ui->currentProgCombo->currentText()); // init it
    progsettings result = Data.ProcessIt(); // do the work and return
    }

    So process it should just do the same as the constructor and then return your progsettings class/struct.



  • @mrjj
    Hi,

    Thanks for the quick reply.
    I don't understand it completely.

    So I added to my ReadProgData.h

    QString Process();
    

    Then I added the following to ReadProgData.cpp

    QString ReadProgData::Process()
    {
        return progsettings;
    }
    

    And to the mainwindows.cpp:

    void MainWindow::on_pushButton_clicked()
    {
    
        qDebug()<<"clicked";
        ReadProgData Data(ui->currentProgCombo->currentText());
        progsettings result = Data.Process();
    }
    

    Then I get the following error. Which I understand. So I probably don't get you completely.

     'progsettings' was not declared in this scope
     error: expected ';' before 'result'
    

    So I'm doing something very stupid I guess.
    Thanks for helping,
    Kind regards


  • Lifetime Qt Champion

    Hi
    the progsettings you return is
    QString progsettings[50][2]; ?

    Should it not return one string from the list ?

    QString ReadProgData::Process()
    {
        return progsettings[0][0]; // one string like you return type says.
    }
    

    Do you mean to return the whole of the array ?

    Anyway, the error
    "progsettings' was not declared in this scope"

    Means "i have not seen how it was declared and dont know it.

    This is due to it lives in readprogdata.cpp and mainwindow do no know it.

    So do you need
    a 2d array of QString or just a SINGLE QString in mainwindow?



  • @mrjj

    Hi,

    I'm trying to get the 2D array of QString.
    So indeed what I try is not possible. But I had no clue on how to do this.

    Kind regards


  • Lifetime Qt Champion

    Hi

    @TMJJ said in Pass data from a new class to main: return type specification for constructor invalid:

    progsettings result = Data.Process();

    You are using progsettings like it's a type hence the error.



  • change progsettings result = Data.Process(); to auto result = Data.Process();

    I'm trying to get the 2D array of QString.

    change:

    • QString progsettings[50][2]; to std::array<std::array<QString,2>,50> progsettings; (you need #include <array>)
    • QString Process(); to const std::array<std::array<QString,2>,50>& Process();
    • QString ReadProgData::Process() to const std::array<std::array<QString,2>,50>& ReadProgData::Process()

Log in to reply