Results 1 to 20 of 20

Thread: Problem with slot

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  2. #2
    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

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 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.


  4. 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
  •  
Qt is a trademark of The Qt Company.