jeudi 2 juillet 2015

Qt, object communication

i making a simple currency converter and i ran into some trouble with class communication.

I have a class for downloading the exchange rates which saves the values into a text file, and another class to parse the file and save the values in a QMap which i declated in my main. i wont post the downloader and parser classes since they work as intented

int main(int argc, char *argv[])
{
QApplication a(argc, argv);

QMap<QString,double> currency_map;


downloader d;
d.Do_download();
//do the downloading

MainWindow w(currency_map);
w.show();
//make the main window and send the map into it.

parser p;
p.read_line(currency_map);
//parsing the file and saving the values.


p.print_map(currency_map);
//test to see if my values are saved.

Calculator c(currency_map, w);
// cannot convert parameter 1 from 'QMap<Key,T>' to 'QMap<Key,T>'* is the error i get

return a.exec();
}

my calculator.h

#include <QObject>
#include <QString>
#include <QDebug>
#include <ui_mainwindow.h>
#include "MainWindow.h"

class Calculator
{
public:
    explicit Calculator(QMap <QString, double> *my_map, Ui::MainWindow &window);
    void get_value(QString value);

private:
    Ui::MainWindow *ui;
    QMap <QString, double> *map_pointer;
    double x , y;

};

and calculator.cpp

#include "calculator.h"

Calculator::Calculator(QMap<QString, double> *my_map, Ui::MainWindow *window)
{
    map_pointer = my_map;
    this->ui= window;  
//I get a overloaded member function not found in calculator
}


void Calculator::get_value(QString value)
{
    for(QMap<QString, double>::Iterator i = map_pointer->begin();i != map_pointer->end();i++)
        {
        if(i.key() == value)
            {
            qDebug()<<i.key() << ": " << i.value();
        }
    }
}

and the MainWindow.h

#include <QMainWindow>
#include <ui_mainwindow.h>
#include "parser.h"
#include "calculator.h"
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QMap<QString, double> currency_map, QWidget *parent = 0);

    QStringList retrieve_list(parser *p);
    ~MainWindow();
private slots:
    void on_convert_button_clicked();

    void on_from_Combox_currentIndexChanged(const QString &arg1);

    void on_to_Combox_currentIndexChanged(const QString &arg1);

signals:
private:
    Ui::MainWindow *ui;
    Calculator *calc;  //missing type specifier
    parser currency_parser;

};

mainwindow.cpp

#include "mainwindow.h"

MainWindow::MainWindow(QMap<QString, double> currency_map, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    calc = new Calculator(currency_map,ui);
    //missing type specifier

    ui->from_Combox->addItems(retrieve_list(&currency_parser));
    ui->to_Combox->addItems(retrieve_list(&currency_parser));

}

QStringList MainWindow::retrieve_list(parser *p)
{
    return p->currency_list;
}

MainWindow::~MainWindow()
{

    delete ui;
    delete calc;
}

The idea was to have a map in main for the exchange rates, fill it with data in the parser class.

and then use the calculator class to calculate the values needed. I had a "plan" to send the map into the class, along with a pointer on the mainwindow object, so the calculator class could display the result in the display widgets in the mainwindow object, but i cant figure out what im missing here.

If someone could make me and example of how to use signals and slots for the communication i need here, that would be very much appreciated.

Aucun commentaire:

Enregistrer un commentaire