Page 1 of 2 12 LastLast
Results 1 to 20 of 38

Thread: A little calculator problem

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

    Exclamation A little calculator problem

    OK so I started to make a calculator program and was compiling while I was coding (that's why the code isn't complete), and it gave me these errors.
    Qt Code:
    1. error: invalid conversion from `int' to `const QObject*'
    2. error: initializing argument 3 of `static bool QObject::connect(const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType)'
    To copy to clipboard, switch view to plain text mode 

    Here is my code.
    Qt Code:
    1. #include <QApplication>
    2. #include <QPushButton>
    3. #include <QLabel>
    4. #include <QSpinBox>
    5. #include <QLCDNumber>
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10. QLabel *minus = new QLabel("-");
    11. QPushButton *equal = new QPushButton("Execute");
    12. QSpinBox *num1 = new QSpinBox;
    13. QSpinBox *num2 = new QSpinBox;
    14. QLCDNumber *show = new QLCDNumber(3);
    15. int calc1, calc2;
    16.  
    17. QObject::connect(num1, SIGNAL(valueChanged(int)), calc1, SLOT(setValue(int)));
    18.  
    19. return app.exec();
    20. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Feb 2010
    Posts
    27
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: A little calculator problem

    try:

    QObject::connect(num1, SIGNAL(valueChanged(int)), this, SLOT(setValue(int)));

  3. #3
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A little calculator problem

    You are passing an int (calc1) as third argument while the function is waiting for a QObject.

    The error message gives you the proper signature of QObject::connect .
    The parameters must be the as follow :
    (const QObject*, const char*, const QObject*, const char*, Qt::ConnectionType.

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

    Default Re: A little calculator problem

    @Dubstar_04 if i do that how will I get calc 1 to equal the value of the spinbox?

  5. #5
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: A little calculator problem

    Set the calc1 value in your slot setValue(int).

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

    Default Re: A little calculator problem

    Maybe you should tell us first what you want achieve. Because your code is nonsense! And I can't figure out why you what set an integer whenever the value of a spinbox is changed, since you can access that info via QSpinBox::value().

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

    Default Re: A little calculator problem

    yah, I know my code is nonesense, that's because it isn't finished. I'm trying to make a program that subtracts the two numbers in the spin-boxes when you press the number.

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

    Default Re: A little calculator problem

    OK, so I did this,
    Qt Code:
    1. QObject::connect(num1, SIGNAL(valueChanged(calc1)), this, SLOT(setValue(calc1)));
    2. QObject::connect(num2, SIGNAL(valueChanged(calc2)), this, SLOT(setValue(calc2)));
    To copy to clipboard, switch view to plain text mode 
    but now it says this,
    Qt Code:
    1. error: invalid use of `this' in non-member function
    To copy to clipboard, switch view to plain text mode 
    Thank you.

  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: 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.


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

    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 

  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: 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.


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

    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.

  13. #13
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    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.

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

    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.

  15. #15
    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: 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.


  16. #16
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    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.

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

    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 

  18. #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: 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.


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

    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.

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

    Default Re: A little calculator problem

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

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.