Results 1 to 4 of 4

Thread: Object communication, signals and slots

  1. #1
    Join Date
    Feb 2015
    Posts
    21
    Thanks
    2

    Default Object communication, signals and slots

    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

    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4.  
    5. QMap<QString,double> currency_map;
    6.  
    7.  
    8. downloader d;
    9. d.Do_download();
    10. //do the downloading
    11.  
    12. MainWindow w(currency_map);
    13. w.show();
    14. //make the main window and send the map into it.
    15.  
    16. parser p;
    17. p.read_line(currency_map);
    18. //parsing the file and saving the values.
    19.  
    20.  
    21. p.print_map(currency_map);
    22. //test to see if my values are saved.
    23.  
    24. Calculator c(currency_map, w);
    25. // cannot convert parameter 1 from 'QMap<Key,T>' to 'QMap<Key,T>'* is the error i get
    26.  
    27. return a.exec();
    28. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. my calculator.h
    2.  
    3. #include <QObject>
    4. #include <QString>
    5. #include <QDebug>
    6. #include <ui_mainwindow.h>
    7. #include "MainWindow.h"
    8.  
    9. class Calculator
    10. {
    11. public:
    12. explicit Calculator(QMap <QString, double> *my_map, Ui::MainWindow &window);
    13. void get_value(QString value);
    14.  
    15. private:
    16. Ui::MainWindow *ui;
    17. QMap <QString, double> *map_pointer;
    18. double x , y;
    19.  
    20. };
    To copy to clipboard, switch view to plain text mode 

    and calculator.cpp

    Qt Code:
    1. #include "calculator.h"
    2.  
    3. Calculator::Calculator(QMap<QString, double> *my_map, Ui::MainWindow *window)
    4. {
    5. map_pointer = my_map;
    6. this->ui= window;
    7. //I get a overloaded member function not found in calculator
    8. }
    9.  
    10.  
    11. void Calculator::get_value(QString value)
    12. {
    13. for(QMap<QString, double>::Iterator i = map_pointer->begin();i != map_pointer->end();i++)
    14. {
    15. if(i.key() == value)
    16. {
    17. qDebug()<<i.key() << ": " << i.value();
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    and the MainWindow.h


    Qt Code:
    1. #include <QMainWindow>
    2. #include <ui_mainwindow.h>
    3. #include "parser.h"
    4. #include "calculator.h"
    5. namespace Ui {
    6. class MainWindow;
    7. }
    8.  
    9. class MainWindow : public QMainWindow
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. explicit MainWindow(QMap<QString, double> currency_map, QWidget *parent = 0);
    15.  
    16. QStringList retrieve_list(parser *p);
    17. ~MainWindow();
    18. private slots:
    19. void on_convert_button_clicked();
    20.  
    21. void on_from_Combox_currentIndexChanged(const QString &arg1);
    22.  
    23. void on_to_Combox_currentIndexChanged(const QString &arg1);
    24.  
    25. signals:
    26. private:
    27. Ui::MainWindow *ui;
    28. Calculator *calc; //missing type specifier
    29. parser currency_parser;
    30.  
    31. };
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2.  
    3. MainWindow::MainWindow(QMap<QString, double> currency_map, QWidget *parent) :
    4. QMainWindow(parent),
    5. ui(new Ui::MainWindow)
    6. {
    7. ui->setupUi(this);
    8.  
    9. calc = new Calculator(currency_map,ui);
    10. //missing type specifier
    11.  
    12. ui->from_Combox->addItems(retrieve_list(&currency_parser));
    13. ui->to_Combox->addItems(retrieve_list(&currency_parser));
    14.  
    15. }
    16.  
    17. QStringList MainWindow::retrieve_list(parser *p)
    18. {
    19. return p->currency_list;
    20. }
    21.  
    22. MainWindow::~MainWindow()
    23. {
    24.  
    25. delete ui;
    26. delete calc;
    27. }
    To copy to clipboard, switch view to plain text mode 

    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.

  2. #2
    Join Date
    Feb 2012
    Location
    Warsaw, Poland
    Posts
    37
    Thanks
    3
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Object communication, signals and slots

    Qt Code:
    1. int main()
    2. {
    3. MainWindow wnd;
    4. Downloader loader;
    5. Parser parser;
    6. connect(&loader, SIGNAL(loaded()), &parser, SLOT(onLoaded()));
    7. connect(&parser, SIGNAL(parsed(QMap<QString, double>)), &wnd, SLOT(onCurrencyParsed(QMap<QString, double>)));
    8. wnd.show();
    9. loader.load();
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

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

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,419
    Thanks
    37
    Thanked 1,546 Times in 1,496 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Object communication, signals and slots

    Quote Originally Posted by bikonja View Post
    Qt Code:
    1. QMap<QString,double> currency_map;
    2.  
    3. Calculator c(currency_map, w);
    4. // cannot convert parameter 1 from 'QMap<Key,T>' to 'QMap<Key,T>'* is the error i get
    To copy to clipboard, switch view to plain text mode 
    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,
    _

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,936
    Thanks
    276
    Thanked 803 Times in 792 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Object communication, signals and slots

    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.

Similar Threads

  1. Replies: 2
    Last Post: 18th April 2013, 12:15
  2. Signals and Slots
    By Maluko_Da_Tola in forum Newbie
    Replies: 3
    Last Post: 29th July 2010, 23:57
  3. about signals and slots
    By Sandip in forum Qt Programming
    Replies: 9
    Last Post: 15th July 2008, 16:02
  4. Signals and Slots
    By 83.manish in forum Qt Programming
    Replies: 3
    Last Post: 30th June 2008, 10:31
  5. QThread communication without signal and slots
    By forrestfsu in forum Qt Programming
    Replies: 16
    Last Post: 22nd May 2007, 00:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.