PDA

View Full Version : Object communication, signals and slots



bikonja
2nd July 2015, 13:27
Hello, 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.

west
2nd July 2015, 14:40
int main()
{
QApplication app;
MainWindow wnd;
Downloader loader;
Parser parser;
connect(&loader, SIGNAL(loaded()), &parser, SLOT(onLoaded()));
connect(&parser, SIGNAL(parsed(QMap<QString, double>)), &wnd, SLOT(onCurrencyParsed(QMap<QString, double>)));
wnd.show();
loader.load();
return a.exec();
}


You could move loader and parser to another thread if load/parse operations take a lot of time.

anda_skoa
2nd July 2015, 16:24
QMap<QString,double> currency_map;

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



You Calculator class expects a pointer to that map.
Either pass the address of currency_map or change the signature of the Calculator constructor to take the map as value or reference.

Cheers,
_

d_stranz
2nd July 2015, 17:18
In addition to that problem, you are passing the QMap by value into the MainWindow constructor. (Your C++ book will tell you what passing by value means).

In your case, it means that MainWindow has a *copy* of the map you create in main(). Since you create MainWindow before you operate on the map using the parser class, none of the changes you make to it will be reflected int he copy the MainWindow holds (because they are two different instances of the same class).

So, change the MainWindow contructor to take a QMap * pointer to your currency map and all will work as intended.