Results 1 to 20 of 20

Thread: Problem with slot

  1. #1
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Problem with slot

    Hi!
    I'm trying to develop a simple calculator. I'm using Qt Creator. I've already designed a form, but now I have a problem with slot. It calls, when i push the button with digit and adds the digit to lcd. I think everything is ok, but i get compiler's errors. Here is the source code:
    Qt Code:
    1. //calc.h
    2.  
    3. #ifndef CALC_H
    4. #define CALC_H
    5.  
    6. #include <QtGui/QDialog>
    7.  
    8. namespace Ui
    9. {
    10. class Calc;
    11. }
    12.  
    13. class Calc : public QDialog
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. Calc(QWidget *parent = 0);
    19. ~Calc();
    20.  
    21. slots:
    22. void setVal(const int &val);
    23.  
    24. private:
    25. Ui::Calc *ui;
    26. };
    27.  
    28. #endif // CALC_H
    To copy to clipboard, switch view to plain text mode 


    Qt Code:
    1. //calc.cpp
    2. #include "calc.h"
    3. #include "ui_calc.h"
    4.  
    5. Calc::Calc(QWidget *parent)
    6. : QDialog(parent), ui(new Ui::Calc)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. Calc::~Calc()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void Calc::setVal(const int &val){
    17.  
    18. lcdNumber-> intValue*=10;
    19. lcdNumber -> intValue+=val;
    20. }
    To copy to clipboard, switch view to plain text mode 


    And these are the errors:
    calc.h:16: error: expected primary-expression before ‘void’
    calc.h:16: error: ISO C++ forbids declaration of ‘type name’ with no type
    calc.h:16: error: expected ‘;’ before ‘void’
    calc.cpp:11: error: no ‘void Calc::setVal(const int&)’ member function declared in class ‘Calc’
    Please help me
    Regards
    Stefek

  2. #2
    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: Problem with slot

    It should say "public slots:" and not "slots:".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with slot

    Thanks, this problem is solved, but now come other problems.
    calc.h:18: error: ISO C++ forbids declaration of ‘QLCDNumber’ with no type
    calc.h:18: error: expected ‘;’ before ‘*’ token
    So I added the declaration of QLCDNumber class: class QLCDNumber;. and now are problems with the inside of the slot:
    calc.cpp:17: error: invalid use of member (did you forget the ‘&’ ?)
    How can I solve it?

    I think i solve this problem. I add to class Calc new variable int value, which stores the actual result of calculations and use display function.

    But... I have new problem...
    Here is my source code:
    Qt Code:
    1. //calc.h
    2.  
    3. #ifndef CALC_H
    4. #define CALC_H
    5.  
    6. #include <QtGui/QDialog>
    7.  
    8. class QLCDNumber;
    9. namespace Ui
    10. {
    11. class Calc;
    12. }
    13.  
    14. class Calc : public QDialog
    15. {
    16. Q_OBJECT
    17.  
    18. private:
    19. QLCDNumber *lcdNumber;
    20. QPushButton *seventhButton;
    21. QPushButton *eighthButton;
    22. QPushButton *ninethButton;
    23. QPushButton *divisionButton;
    24. QPushButton *fourthButton;
    25. QPushButton *fifthButton;
    26. QPushButton *sixthButton;
    27. QPushButton *additionButton;
    28. QPushButton *firstButton;
    29. QPushButton *secondButton;
    30. QPushButton *thirdButton;
    31. QPushButton *subtractionButton;
    32. QPushButton *zeroButton;
    33. QPushButton *sqrtButton;
    34. QPushButton *multiplicationButton;
    35. QPushButton *moduloButton;
    36. int value;
    37.  
    38. public:
    39. Calc(QWidget *parent = 0);
    40. ~Calc();
    41.  
    42. public slots:
    43. void setVal(const int &val);
    44.  
    45.  
    46.  
    47. private:
    48. Ui::Calc *ui;
    49. };
    50.  
    51. #endif // CALC_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "calc.h"
    2. #include "ui_calc.h"
    3.  
    4. Calc::Calc(QWidget *parent)
    5. : QDialog(parent), ui(new Ui::Calc)
    6. {
    7. ui->setupUi(this);
    8. connect(zeroButton, SIGNAL(clicked()), this, SLOT(setVal(0)));
    9. }
    10.  
    11. Calc::~Calc()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void Calc::setVal(const int &val){
    17.  
    18. value*=10;
    19. value+=val;
    20. lcdNumber ->display(value);
    21. }
    To copy to clipboard, switch view to plain text mode 

    In the constructor I add the conection between zero button and LCDNumber. It should works, but in the console are errors:
    Segmentation fault
    I have no idea how to do this. Could anyone help me?
    It's harder than I thought at the beginning of lessons.

    I've read in documentation that the signal and slot parameters mustn't contain variable name, but only type, so how can I send the value, which is needed?
    Last edited by stefek; 28th February 2009 at 16:43.

  4. #4
    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: Problem with slot

    Your original problem simply required including <QLCDNumber>. Your current problem is that you didn't create an object behind the zeroButton variable.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    517
    Thanks
    21
    Thanked 66 Times in 62 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with slot

    u dont have to send any value from the slot..remove 0 from setVal(0) and it should work..the signal will IMPLICITLY send the data as required by the slot...in the case of clicked(bool), if the button is clicked, 'true' is the value sent, otherwise 'false' is sent

  6. #6
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with slot

    I've added in main function,after the Calc constructor, new procedure "connection" that connects signals with slots. The effect is the same = segfault.

    Yes, there was error,but now is:
    Qt Code:
    1. connect(this->zeroButton, SIGNAL(clicked(bool)), this, SLOT(setVal(const int &)));
    To copy to clipboard, switch view to plain text mode 
    And still is segfault.

  7. #7
    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: Problem with slot

    Did you initialize the zeroButton variable to anything?

    By the way, please read about signal-slot connections in the reference. You can't connect a bool signal to an int slot, it wouldn't make sense.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with slot

    I've read this, but I can't write anything that could work. Is it really hard or do I make it more difficult than it is? :/
    I think that actually the whole calculator is too difficult for me. I'll try to make simple dialog with one QPushButton and QLCDNumber.
    I've written a function, which sets value to qlcdnumber:
    Qt Code:
    1. void changeValue(const int &val){
    2.  
    3. lcdNumber->display(val);
    4. }
    To copy to clipboard, switch view to plain text mode 
    But no matter how I call this function I get segfaults. I don't know why.

  9. #9
    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: Problem with slot

    I will repeat this question the last time - did you initialize your variables? Do you have any knowledge of C++ at all? Do you understand the difference between a pointer and an object? You are getting a segmentation fault because you try to dereference an uninitialized pointer. The control flow doesn't even reach the connect statement because it segfaults a while earlier.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with slot

    Here is actual source code:
    Qt Code:
    1. //dialog.cpp
    2. #include "dialog.h"
    3. #include "ui_dialog.h"
    4.  
    5. Dialog::Dialog(QWidget *parent)
    6. : QDialog(parent), ui(new Ui::DialogClass)
    7. {
    8. ui->setupUi(this);
    9.  
    10. }
    11.  
    12. Dialog::~Dialog()
    13. {
    14. delete ui;
    15. }
    16.  
    17. void Dialog::changeValue(const int &val){
    18.  
    19. lcdNumber->display(val);
    20. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //dialog.h
    2. #ifndef DIALOG_H
    3. #define DIALOG_H
    4.  
    5. #include <QtGui/QDialog>
    6. class QLCDNumber;
    7. namespace Ui
    8. {
    9. class DialogClass;
    10. }
    11.  
    12. class Dialog : public QDialog
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. Dialog(QWidget *parent = 0);
    18. ~Dialog();
    19.  
    20. public slots:
    21. void changeValue(const int &val);
    22.  
    23. signals:
    24. void onClick(const int &val);
    25.  
    26.  
    27. private:
    28. QLCDNumber *lcdNumber;
    29. Ui::DialogClass *ui;
    30. };
    31.  
    32. #endif // DIALOG_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //ui_dialog.h
    2. #ifndef UI_DIALOG_H
    3. #define UI_DIALOG_H
    4.  
    5. #include <QtCore/QVariant>
    6. #include <QtGui/QAction>
    7. #include <QtGui/QApplication>
    8. #include <QtGui/QButtonGroup>
    9. #include <QtGui/QDialog>
    10. #include <QtGui/QLCDNumber>
    11. #include <QtGui/QPushButton>
    12.  
    13. QT_BEGIN_NAMESPACE
    14.  
    15. class Ui_DialogClass
    16. {
    17. public:
    18. QLCDNumber *lcdNumber;
    19. QPushButton *pushButton;
    20.  
    21. void setupUi(QDialog *DialogClass)
    22. {
    23. if (DialogClass->objectName().isEmpty())
    24. DialogClass->setObjectName(QString::fromUtf8("DialogClass"));
    25. DialogClass->resize(209, 62);
    26. lcdNumber = new QLCDNumber(DialogClass);
    27. lcdNumber->setObjectName(QString::fromUtf8("lcdNumber"));
    28. lcdNumber->setGeometry(QRect(130, 20, 64, 23));
    29. pushButton = new QPushButton(DialogClass);
    30. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    31. pushButton->setGeometry(QRect(10, 20, 105, 25));
    32. pushButton->setProperty("value", QVariant(0));
    33.  
    34. retranslateUi(DialogClass);
    35.  
    36. QMetaObject::connectSlotsByName(DialogClass);
    37. } // setupUi
    38.  
    39. void retranslateUi(QDialog *DialogClass)
    40. {
    41. DialogClass->setWindowTitle(QApplication::translate("DialogClass", "Dialog", 0, QApplication::UnicodeUTF8));
    42. pushButton->setText(QApplication::translate("DialogClass", "0", 0, QApplication::UnicodeUTF8));
    43. Q_UNUSED(DialogClass);
    44. } // retranslateUi
    45.  
    46. };
    47.  
    48. namespace Ui {
    49. class DialogClass: public Ui_DialogClass {};
    50. } // namespace Ui
    51.  
    52. QT_END_NAMESPACE
    53.  
    54. #endif // UI_DIALOG_H
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //main.cpp
    2. #include <QtGui/QApplication>
    3. #include "dialog.h"
    4.  
    5. int main(int argc, char *argv[])
    6. {
    7. QApplication a(argc, argv);
    8. Dialog w;
    9. w.show();
    10.  
    11.  
    12.  
    13. return a.exec();
    14. }
    To copy to clipboard, switch view to plain text mode 

    I'm newbie in Qt and probably not C++ pro, but I want to learn and improve my skills.No one are the best at the beginning. Could you explain me how can I initialize variables? I'm in the brain dead.

  11. #11
    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: Problem with slot

    Your problem is strictly related to C++. You have two lcdNumber member variables from different levels of inheritance and you use the one that is uninitialized. Get rid of lcdNumber from your Dialog class and use the one in ui.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  12. The following user says thank you to wysota for this useful post:

    stefek (1st March 2009)

  13. #12
    Join Date
    Oct 2008
    Posts
    70
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Default Re: Problem with slot

    I agree with wysota!

    stefek, rewrite your code as follows:

    Qt Code:
    1. void Dialog::changeValue(const int &val)
    2. {
    3. ui->lcdNumber->display(val);
    4. }
    To copy to clipboard, switch view to plain text mode 

    Also you should remove QLCDNumber *lcdNumber member from Dialog class declaration.

  14. The following user says thank you to pastor for this useful post:

    stefek (1st March 2009)

  15. #13
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with slot

    Hi there, the weekend comes so I found time to continue my "project". Slot is now working, but I have a problem with signal. I don't know how to write it and it is hard to find in the Internet practical information about it. Could you help me write signal which will be connected to changeValue slot? It emits when I push the button. Thanks

  16. #14
    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: Problem with slot

    Please make yourself familiar with the Signals and Slots documentation. It explains signals and slots better than anyone of us could.
    J-P Nurmi

  17. #15
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with slot

    I think that I've understood it. If i want to use parameterless signals as a signals with parameters, I should use QSignalMapper class. I wrote it:
    Qt Code:
    1. Dialog::Dialog(QWidget *parent)
    2. : QDialog(parent), ui(new Ui::DialogClass)
    3. {
    4. ui->setupUi(this);
    5. QSignalMapper *signalMapper = new QSignalMapper();
    6. connect(ui->pushButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    7. signalMapper->setMapping(ui->pushButton, "0");
    8. connect(signalMapper, SIGNAL(mapped(const QString &)), ui->lcdNumber, SLOT(changeValue(const QString &)));
    9. }
    10.  
    11. Dialog::~Dialog()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void Dialog::changeValue(const QString &val){
    17.  
    18. int al = val.toInt();
    19. ui->lcdNumber->display(al);
    20. }
    To copy to clipboard, switch view to plain text mode 
    It compiled, but I get errors:
    Object::connect: No such slot QLCDNumber::changeValue(const QString &) in dialog.cpp:11
    Object::connect: (receiver name: 'lcdNumber')
    Could you help me to find a mistake, which I made?

    I found that he slot changeValue should not be a Dialog's function but a member of QLCDNumber class. But i don't know how to declare this membership. Is there any posibility to do this except for creating new class, which will inherit from QLCDNumber?
    Last edited by stefek; 7th March 2009 at 22:55.

  18. #16
    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: Problem with slot

    There is no slot "changeValue" in QLCDNumber. The slot is in your class not in the lcd number object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  19. #17
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with slot

    Qt Code:
    1. #ifndef UI_DIALOG_H
    2. #define UI_DIALOG_H
    3.  
    4. #include <QtCore/QVariant>
    5. #include <QtGui/QAction>
    6. #include <QtGui/QApplication>
    7. #include <QtGui/QButtonGroup>
    8. #include <QtGui/QDialog>
    9. #include <QtGui/QLCDNumber>
    10. #include <QtGui/QPushButton>
    11.  
    12. QT_BEGIN_NAMESPACE
    13.  
    14. class LCDNumber : public QLCDNumber{
    15.  
    16. public:
    17. LCDNumber(QWidget * parent = 0);
    18. LCDNumber(uint numDigits, QWidget * parent = 0);
    19.  
    20. public slots:
    21. void changeValue(const QString &val);
    22.  
    23. };
    24.  
    25. class Ui_DialogClass
    26. {
    27. public:
    28. LCDNumber *lcdNumber;
    29. QPushButton *pushButton;
    30.  
    31. void setupUi(QDialog *DialogClass)
    32. {
    33. if (DialogClass->objectName().isEmpty())
    34. DialogClass->setObjectName(QString::fromUtf8("DialogClass"));
    35. DialogClass->resize(209, 62);
    36. lcdNumber = new LCDNumber(DialogClass);
    37. lcdNumber->setObjectName(QString::fromUtf8("lcdNumber"));
    38. lcdNumber->setGeometry(QRect(130, 20, 64, 23));
    39. pushButton = new QPushButton(DialogClass);
    40. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    41. pushButton->setGeometry(QRect(10, 20, 105, 25));
    42. pushButton->setProperty("value", QVariant(0));
    43.  
    44. retranslateUi(DialogClass);
    45.  
    46. QMetaObject::connectSlotsByName(DialogClass);
    47. } // setupUi
    48.  
    49. void retranslateUi(QDialog *DialogClass)
    50. {
    51. DialogClass->setWindowTitle(QApplication::translate("DialogClass", "Dialog", 0, QApplication::UnicodeUTF8));
    52. pushButton->setText(QApplication::translate("DialogClass", "0", 0, QApplication::UnicodeUTF8));
    53. Q_UNUSED(DialogClass);
    54. } // retranslateUi
    55.  
    56. };
    57.  
    58. namespace Ui {
    59. class DialogClass: public Ui_DialogClass {};
    60. } // namespace Ui
    61.  
    62. LCDNumber::LCDNumber(QWidget *parent) :QLCDNumber(parent){
    63.  
    64.  
    65.  
    66. }
    67. LCDNumber::LCDNumber(uint numDigits, QWidget *parent) :QLCDNumber(numDigits, parent){
    68.  
    69.  
    70. }
    71. void LCDNumber::changeValue(const QString &val){
    72.  
    73. Ui::DialogClass::lcdNumber->display(val.toInt());
    74. }
    75. QT_END_NAMESPACE
    76.  
    77. #endif
    To copy to clipboard, switch view to plain text mode 
    That what I wrote, but compilator says:
    In file included from dialog.cpp:2:
    ui_dialog.h: In member function ‘void LCDNumber::changeValue(const QString&)’:
    ui_dialog.h:37: error: object missing in reference to ‘Ui_DialogClass::lcdNumber’
    ui_dialog.h:82: error: from this location
    I think if I solve this, my program will finally work.

  20. #18
    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: Problem with slot

    Please read this topic: Using a Component in Your Application

    You shouldn't add any code to the ui_*.h files, they are generated and all your code will be overwritten next time uic runs. Instead use one of the inheritance approaches described in the manual and add your code there.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  21. #19
    Join Date
    Feb 2009
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Problem with slot

    Qt Code:
    1. #include "calc.h"
    2. #include "ui_calc.h"
    3.  
    4. Calc::Calc(QWidget *parent)
    5. : QDialog(parent), ui(new Ui::Calc)
    6. {
    7. value = 0;
    8. ui->setupUi(this);
    9. this->setWindowTitle("Calqta");
    10.  
    11. QSignalMapper *signalMapper = new QSignalMapper();
    12.  
    13. connect(ui->firstButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    14. signalMapper->setMapping(ui->firstButton, "1");
    15.  
    16. connect(ui->secondButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    17. signalMapper->setMapping(ui->secondButton, "2");
    18.  
    19. connect(ui->thirdButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    20. signalMapper->setMapping(ui->thirdButton, "3");
    21.  
    22. connect(ui->fourthButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    23. signalMapper->setMapping(ui->fourthButton, "4");
    24.  
    25. connect(ui->fifthButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    26. signalMapper->setMapping(ui->fifthButton, "5");
    27.  
    28. connect(ui->sixthButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    29. signalMapper->setMapping(ui->sixthButton, "6");
    30.  
    31. connect(ui->seventhButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    32. signalMapper->setMapping(ui->seventhButton, "7");
    33.  
    34. connect(ui->eighthButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    35. signalMapper->setMapping(ui->eighthButton, "8");
    36.  
    37. connect(ui->ninethButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    38. signalMapper->setMapping(ui->ninethButton, "9");
    39.  
    40. connect(ui->zeroButton, SIGNAL(clicked()), signalMapper, SLOT(map()));
    41. signalMapper->setMapping(ui->zeroButton, "0");
    42.  
    43.  
    44. connect(signalMapper, SIGNAL(mapped(const QString &)), this, SLOT(changeValue(const QString &)));
    45. connect(ui->sqrtButton, SIGNAL(clicked()), this, SLOT(square()));
    46.  
    47. connect(ui->clearButton, SIGNAL(clicked()), this, SLOT(clear()));
    48.  
    49.  
    50.  
    51.  
    52. }
    53.  
    54. Calc::~Calc()
    55. {
    56. delete ui;
    57. }
    58. void Calc::add(const QString &val){
    59.  
    60. str+=val;
    61.  
    62. }
    63. void Calc::changeValue(const QString &val){
    64.  
    65. Calc::add(val);
    66. value=str.toInt();
    67. display(value);
    68.  
    69.  
    70. }
    71.  
    72. void Calc::display(const int &val){
    73.  
    74. ui->lcdNumber->display(val);
    75. }
    76. void Calc::square(){ //works
    77.  
    78.  
    79. value = std::sqrt(value);
    80. display(value);
    81. str.remove(0, str.length());
    82. Calc::changeValue(QString::number(value));
    83.  
    84.  
    85. }
    86. void Calc::clear(){ //works
    87.  
    88. value = 0;
    89. str.remove(0, str.length());
    90. display(value);
    91. }
    To copy to clipboard, switch view to plain text mode 
    I managed that simple dialog, so I came back to my calculator. All numeric buttons work correctly - adding digit to number works. Square root also works correctly. Now I have a problem with 2 arguments operations, such as addition. I don't have an idea how can I store the second argument - first is in the variable "value". Could you give me handy tip?
    Regards
    Stefek

  22. #20
    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: Problem with slot

    You are currently using a postfix notation - you first store an operand and then expect an operator. Now you have to switch to an infix notation where the operator comes inbetween operands and where the second operand may be empty. You should have storage space (variables) for at least two operands and one variable for the operator. Then it will be easy to perform calculations. Consider this mockup:

    Qt Code:
    1. class SimpleCalculator {
    2. public:
    3. enum Operator { NoOp, AddOp, SubOp, MulOp, DivOp, SqrtOp, SqrOp };
    4. SimpleCalculator(){ m_left = 0; m_right = 0; m_op = NoOp; }
    5. void setLeftOperand(int v){ m_left = v; }
    6. void setRightOperand(int v){ m_right = v; }
    7. void setOperator(Operator o){ m_op = o; }
    8. int calculate(){
    9. int result = 0;
    10. switch(m_op){
    11. case AddOp: result = m_left + m_right; break;
    12. case SubOp: result = m_left - m_right; break;
    13. ...
    14. case SqrOp: result = m_left*m_left;
    15. case NoOp: default: result = m_left;
    16. }
    17. m_op = NoOp;
    18. m_left = result;
    19. m_right = 0;
    20. return result;
    21. }
    22. };
    To copy to clipboard, switch view to plain text mode 

    Your next problem will be how to execute a sequence of operations but until you reach that step, first learn this one.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  23. The following user says thank you to wysota for this useful post:

    stefek (9th March 2009)

Similar Threads

  1. How to declare SLOT as a parameter to member function?
    By QPlace in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2018, 00:41
  2. Replies: 12
    Last Post: 18th September 2008, 15:04
  3. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  4. Thread, Timer and Socket. Comuication problem
    By ^NyAw^ in forum Qt Programming
    Replies: 6
    Last Post: 17th January 2008, 16:48
  5. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45

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.