Results 1 to 20 of 38

Thread: A little calculator problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: A little calculator problem

    Shouldn't you be connecting to the clicked() signal of the "Execute" button then?
    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 2010
    Posts
    42
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2

    Default Re: A little calculator problem

    OK, here is the entire code,
    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QLabel>
    4. #include <QSpinBox>
    5. #include <QLCDNumber>
    6. #include <QHBoxLayout>
    7. #include <QVBoxLayout>
    8.  
    9. int main(int argc, char *argv[])
    10. {
    11. QApplication app(argc, argv);
    12. QLabel *minus = new QLabel("-");
    13. QPushButton *equal = new QPushButton("Execute");
    14. QSpinBox *num1 = new QSpinBox;
    15. QSpinBox *num2 = new QSpinBox;
    16. QLCDNumber *show = new QLCDNumber(3);
    17. int calc1, calc2;
    18.  
    19. QObject::connect(num1, SIGNAL(valueChanged(calc1)), this, SLOT(setValue(calc1)));
    20. QObject::connect(num2, SIGNAL(valueChanged(calc2)), this, SLOT(setValue(calc2)));
    21. QObject::connect(equal, SIGNAL(clicked()), show, SLOT(setValue(calc1-calc2)));
    22.  
    23. QHBoxLayout *layout = new QHBoxLayout;
    24. layout->addWidget(num1);
    25. layout->addWidget(minus);
    26. layout->addWidget(num2);
    27. layout->addWidget(show);
    28. QVBoxLayout *mainLayout = new QVBoxLayout;
    29. mainLayout->addLayout(layout);
    30. mainLayout->addWidget(equal);
    31.  
    32. return app.exec();
    33. }
    To copy to clipboard, switch view to plain text mode 
    and here are the errors I receive.
    Qt Code:
    1. |19|error: invalid use of `this' in non-member function|
    2. |20|error: invalid use of `this' in non-member function|
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: A little calculator problem

    Well, there is no "this" object when you are not inside any objective code so obviously it won't work. Your connect statements still don't make sense anyway. You can't do mathematical operations there and you can't pass variable names there.

    Here is something more reasonable:
    Qt Code:
    1. class Calculator : public QWidget {
    2. Q_OBJECT
    3. public:
    4. Calculator(QWidget *parent = 0) : QWidget(parent){
    5. minus = new QLabel("-");
    6. equal = new QPushButton("Execute");
    7. num1 = new QSpinBox;
    8. num2 = new QSpinBox;
    9. show = new QLCDNumber(3);
    10.  
    11. QHBoxLayout *layout = new QHBoxLayout;
    12. layout->addWidget(num1);
    13. layout->addWidget(minus);
    14. layout->addWidget(num2);
    15. layout->addWidget(show);
    16. QVBoxLayout *mainLayout = new QVBoxLayout(this);
    17. mainLayout->addLayout(layout);
    18. mainLayout->addWidget(equal);
    19. connect(equal, SIGNAL(clicked()), show, SLOT(calculate()));
    20. }
    21. public slots:
    22. void calculate() {
    23. show->setValue(num1->value()-num2->value());
    24. }
    25. private:
    26. QLabel *minus;
    27. QPushButton *equal;
    28. QSpinBox *num1;
    29. QSpinBox *num2;
    30. QLCDNumber *show;
    31. };
    32.  
    33. #include "main.moc"
    34.  
    35. int main(int argc, char **argv){
    36. QApplication app(argc, argv);
    37. Calculator w;
    38. w.show();
    39. return app.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 
    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. #4
    Join Date
    Feb 2010
    Posts
    42
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2

    Default Re: A little calculator problem

    now it says
    Qt Code:
    1. |error: 'class QLCDNumber' has no member named 'setValue'
    To copy to clipboard, switch view to plain text mode 


    P.S
    Sorry this is taking this long, I am a complete beginner.

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: A little calculator problem

    It's because it has to be
    Qt Code:
    1. show->display(num1->value()-num2->value());
    To copy to clipboard, switch view to plain text mode 
    . A quick look it the documentation helps in such cases.

  6. #6
    Join Date
    Feb 2010
    Posts
    42
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2

    Default Re: A little calculator problem

    sorry, I didn't post these errors and I thought the others would go away but here are all the errors.
    Qt Code:
    1. |40|error: `QLCDNumber*calculator::show' is private|
    2. |48|error: within this context|
    3. |48|error: `w.calculator::show' cannot be used as a function|
    To copy to clipboard, switch view to plain text mode 

    again, sorry if I'm beginning to be a pain.

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

    Default Re: A little calculator problem

    Call the variable differently. It clashes with the method name (QWidget::show()).
    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
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Wiki edits
    5

    Default Re: A little calculator problem

    Seems to be a name clash, so rename your QLCDNumber to lcdnumber or something else and recompile.

  9. #9
    Join Date
    Feb 2010
    Posts
    42
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2

    Default Re: A little calculator problem

    OK so everything compiled, BUT when it is linking to the .EXE it gives me this error
    Qt Code:
    1. C:\Program Files\CodeBlocks\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\include\c++\3.4.5\bits\stl_algobase.h:(.text$_ZN10calculatorD1Ev[calculator::~calculator()]+0xb)||undefined reference to `vtable for calculator'|
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: A little calculator problem

    Did you remember to run qmake after making changes I initially suggested to the code?
    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.


  11. #11
    Join Date
    Feb 2010
    Posts
    42
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2

    Default Re: A little calculator problem

    I'm sorry, I don't know how to make a .moc file, I was playing around in the QT command prompt but I can't get a .moc file.

    I've treid qmake, qmake -o file, qmake -v.

  12. #12
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 348 Times in 333 Posts

    Default Re: A little calculator problem

    Run QMake and then rebuild. That'll create your moc files and even run moc on them

  13. #13
    Join Date
    Feb 2010
    Posts
    42
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    2

    Default Re: A little calculator problem

    ok i ran qmake in the folder of main.cpp then I pressed rebuild in Code::Blocks and it gave me the same error.

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. Calculator Builder Example
    By janus in forum Newbie
    Replies: 1
    Last Post: 19th April 2008, 09:23
  3. From calculator: how to draw
    By mattia in forum Newbie
    Replies: 1
    Last Post: 26th October 2007, 11:22
  4. Need help with a basic calculator
    By Morea in forum Qt Programming
    Replies: 7
    Last Post: 25th March 2006, 19:18

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.