Results 1 to 8 of 8

Thread: Need help with a basic calculator

  1. #1
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Need help with a basic calculator

    I have attached the code, the problem is: it doesn't show anything. I can't figure out why. I think I've done it right, but obviously I haven't.
    Can you see why I don't get any out put?
    Attached Files Attached Files

  2. #2
    Join Date
    Jan 2006
    Location
    Ukraine,Lviv
    Posts
    454
    Thanks
    9
    Thanked 27 Times in 27 Posts
    Qt products
    Qt3
    Platforms
    Unix/X11 Windows

    Default Re: Need help with a basic calculator

    Yes you forgot set main widget for your app..look in next code
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "mainwindow.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. MainWindow *MainForm = new MainWindow;
    9. app.setMainWidget(MainForm);
    10. MainForm->show();
    11. return app.exec();
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 
    a life without programming is like an empty bottle

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need help with a basic calculator

    Quote Originally Posted by Morea
    I have attached the code, the problem is: it doesn't show anything. I can't figure out why. I think I've done it right, but obviously I haven't.
    Can you see why I don't get any out put?
    You can't add parameter values to signal-slot connections.
    In the constructor of MainWindow:

    change:
    Qt Code:
    1. connect(ui.siffra9,SIGNAL(clicked()),&C,SLOT(addToCurrentNumber(9)));
    2. [...]
    3. connect(ui.siffra0,SIGNAL(clicked()),&C,SLOT(addToCurrentNumber(0)));
    To copy to clipboard, switch view to plain text mode 

    to:
    Qt Code:
    1. #include <QSignalMapper>
    2.  
    3. QSignalMapper* mapper = new QSignalMapper(this);
    4.  
    5. connect(ui.siffra9, SIGNAL(clicked()), mapper, SLOT(map()));
    6. mapper->setMapping(ui.siffra9, 9);
    7. [...]
    8. connect(ui.siffra0, SIGNAL(clicked()), mapper, SLOT(map()));
    9. mapper->setMapping(ui.siffra0, 0);
    10.  
    11. connect(mapper, SIGNAL(mapped(int)), &C, SLOT(addToCurrentNumber(int)));
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help with a basic calculator

    Quote Originally Posted by zlatko
    Yes you forgot set main widget for your app..look in next code
    Qt Code:
    1. #include <QApplication>
    2.  
    3. #include "mainwindow.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication app(argc, argv);
    8. MainWindow *MainForm = new MainWindow;
    9. app.setMainWidget(MainForm);
    10. MainForm->show();
    11. return app.exec();
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 
    Strange, I've managed without that before.

  5. #5
    Join Date
    Feb 2006
    Posts
    209
    Thanks
    13
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Need help with a basic calculator

    Quote Originally Posted by jpn
    You can't add parameter values to signal-slot connections.
    In the constructor of MainWindow:

    change:
    Qt Code:
    1. connect(ui.siffra9,SIGNAL(clicked()),&C,SLOT(addToCurrentNumber(9)));
    2. [...]
    3. connect(ui.siffra0,SIGNAL(clicked()),&C,SLOT(addToCurrentNumber(0)));
    To copy to clipboard, switch view to plain text mode 

    to:
    Qt Code:
    1. #include <QSignalMapper>
    2.  
    3. QSignalMapper* mapper = new QSignalMapper(this);
    4.  
    5. connect(ui.siffra9, SIGNAL(clicked()), mapper, SLOT(map()));
    6. mapper->setMapping(ui.siffra9, 9);
    7. [...]
    8. connect(ui.siffra0, SIGNAL(clicked()), mapper, SLOT(map()));
    9. mapper->setMapping(ui.siffra0, 0);
    10.  
    11. connect(mapper, SIGNAL(mapped(int)), &C, SLOT(addToCurrentNumber(int)));
    To copy to clipboard, switch view to plain text mode 
    I will try that, thank you.
    Are there any other way of getting the same functionality? I mean, a different design, or you will most likely need to use the mapper unless you wish to create 10 different slots, one for each digit?

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need help with a basic calculator

    Quote Originally Posted by Morea
    I will try that, thank you.
    Are there any other way of getting the same functionality? I mean, a different design, or you will most likely need to use the mapper unless you wish to create 10 different slots, one for each digit?
    The signal mapper is intended for situations like yours.
    Yes, one approach could be to create a bunch of slots, a different slot for each digit.. Not very handy, right

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Need help with a basic calculator

    Quote Originally Posted by zlatko
    Yes you forgot set main widget for your app..look in next code
    Isn't application's main widget obsolete in Qt 4..

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Need help with a basic calculator

    One could use a QButtonGroup instead of a signal mapper too. It would be more natural than using QSignalMapper.

Similar Threads

  1. amscalc - calculator for ammoniumsulfate precipitation
    By nikau in forum Qt-based Software
    Replies: 1
    Last Post: 30th October 2008, 05:55
  2. Need immediate help on basic cpp
    By Sandip in forum General Programming
    Replies: 10
    Last Post: 21st September 2008, 10:33
  3. Where can I get basic WebKit software?
    By crazymoonboy in forum General Discussion
    Replies: 1
    Last Post: 4th September 2008, 14:15
  4. Need Basic html Browser
    By awalesminfo in forum Newbie
    Replies: 6
    Last Post: 21st March 2006, 17:14
  5. Basic question on new and delete
    By jcr in forum General Programming
    Replies: 25
    Last Post: 14th February 2006, 15:09

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.