Results 1 to 6 of 6

Thread: Calc example throwing errors when I try it on my own

  1. #1
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Calc example throwing errors when I try it on my own

    I need help as I have no idea what I could be doing wrong. I have been trying to follow the Calc Form Example (http://doc.trolltech.com/4.7/designe...latorform.html) and can't for the life of me figure out what I am doing wrong.

    Here are the error codes that are thrown when trying to compile:
    Qt Code:
    1. ..\test\widget.cpp: In member function 'void Widget::on_def_valueChanged(int)':
    2. ..\test\widget.cpp:17: error: request for member 'total' in '((Widget*)this)->Widget::ui', which is of non-class type 'Ui::Widget*'
    3. ..\test\widget.cpp:17: error: request for member 'soc' in '((Widget*)this)->Widget::ui', which is of non-class type 'Ui::Widget*'
    To copy to clipboard, switch view to plain text mode 

    Here is widget.cpp:
    Qt Code:
    1. #include "widget.h"
    2.  
    3. Widget::Widget(QWidget *parent) :
    4. QWidget(parent),
    5. ui(new Ui::Widget)
    6. {
    7. ui->setupUi(this);
    8. }
    9.  
    10. Widget::~Widget()
    11. {
    12. delete ui;
    13. }
    14.  
    15. void Widget::on_def_valueChanged(int value)
    16. {
    17. ui.total->setText(QString::number(value * 2 + ui.soc->value()));
    18. }
    To copy to clipboard, switch view to plain text mode 

    main.cpp:
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "widget.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. Widget w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    widget.h:
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5.  
    6. #include "ui_widget.h"
    7.  
    8. namespace Ui {
    9. class Widget;
    10. }
    11.  
    12. class Widget : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit Widget(QWidget *parent = 0);
    18. ~Widget();
    19.  
    20. private slots:
    21. void on_def_valueChanged(int value);
    22.  
    23. private:
    24. Ui::Widget *ui;
    25. };
    26.  
    27. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    and test.pro
    Qt Code:
    1. #-------------------------------------------------
    2. #
    3. # Project created by QtCreator 2011-03-15T16:01:49
    4. #
    5. #-------------------------------------------------
    6.  
    7. QT += core gui
    8.  
    9. TARGET = test
    10. TEMPLATE = app
    11.  
    12.  
    13. SOURCES += main.cpp\
    14. widget.cpp
    15.  
    16. HEADERS += widget.h
    17.  
    18. FORMS += widget.ui
    To copy to clipboard, switch view to plain text mode 

    "def" and "soc" are qspinboxs, and "total" is a qlabel.

    This should be simple, right? I'm sitting here banging my head against the wall.

  2. #2
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Calc example throwing errors when I try it on my own

    shouldn't that be ui->soc instead of ui.soc? Similarly for the other items.

  3. The following user says thank you to schnitzel for this useful post:

    TomJoad (15th March 2011)

  4. #3
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calc example throwing errors when I try it on my own

    Genius!

    However, why does the example state:

    Qt Code:
    1. void CalculatorForm::on_inputSpinBox1_valueChanged(int value)
    2. {
    3. ui.outputWidget->setText(QString::number(value + ui.inputSpinBox2->value()));
    4. }
    To copy to clipboard, switch view to plain text mode 

    The example compiles and works just fine.

    Either way, thank you for your help.

  5. #4
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Calc example throwing errors when I try it on my own

    good...

    don't know, lemme check the example...

    ok, in the CalculatorForm example, ui is not a pointer. In your case it is a pointer.
    Last edited by schnitzel; 15th March 2011 at 23:58. Reason: correction

  6. #5
    Join Date
    Mar 2011
    Posts
    16
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calc example throwing errors when I try it on my own

    Now, since "ui" is a pointer, I am receiving error code -1073741819 when closing, meaning "delete ui" is not working properly (ran it through debug and that is what it highlighted). How do I fix that? I attempted to put "delete ui;" in the void Widget::answer() function, but that didn't work either. At least this is what I think is going on. The error comes up even without changing any qspinbox, which makes me think it could be something else.

    Anyways, here are the only files that have changed besides widget.ui

    widget.cpp
    Qt Code:
    1. #include "widget.h"
    2.  
    3. Widget::Widget(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. ui->setupUi(this);
    7. }
    8.  
    9. void Widget::answer()
    10. {
    11. ui->total->setText(QString::number((ui->def->value()*2)+(ui->soc->value())+(ui->she->value())+(ui->scc->value())));
    12. ui->shlabel->setText(QString::number(ui->she->value()*12));
    13. }
    14.  
    15. void Widget::on_def_valueChanged()
    16. {
    17. answer();
    18. }
    19.  
    20. void Widget::on_soc_valueChanged()
    21. {
    22. answer();
    23. }
    24.  
    25. void Widget::on_scc_valueChanged()
    26. {
    27. answer();
    28. }
    29.  
    30. void Widget::on_she_valueChanged()
    31. {
    32. answer();
    33. }
    34.  
    35.  
    36. Widget::~Widget()
    37. {
    38. delete ui;
    39. }
    To copy to clipboard, switch view to plain text mode 

    widget.h
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5. #include <QString>
    6.  
    7. #include "ui_widget.h"
    8.  
    9. namespace Ui {
    10. class Widget;
    11. }
    12.  
    13. class Widget : public QWidget
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. explicit Widget(QWidget *parent = 0);
    19. ~Widget();
    20.  
    21. private slots:
    22. void on_def_valueChanged();
    23.  
    24. void on_soc_valueChanged();
    25.  
    26. void on_scc_valueChanged();
    27.  
    28. void on_she_valueChanged();
    29.  
    30. void answer();
    31.  
    32. private:
    33. Ui::Widget *ui;
    34. };
    35.  
    36. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 


    Added after 11 minutes:


    Interesting, I modified the code a little, not exactly sure what I did, but it now works:

    widget.h
    Qt Code:
    1. #ifndef WIDGET_H
    2. #define WIDGET_H
    3.  
    4. #include <QWidget>
    5.  
    6. #include "ui_widget.h"
    7.  
    8. namespace Ui {
    9. class Widget;
    10. }
    11.  
    12. class Widget : public QWidget
    13. {
    14. Q_OBJECT
    15.  
    16. public:
    17. explicit Widget(QWidget *parent = 0);
    18. // ~Widget();
    19.  
    20. private slots:
    21. void on_def_valueChanged();
    22.  
    23. void on_soc_valueChanged();
    24.  
    25. void on_scc_valueChanged();
    26.  
    27. void on_she_valueChanged();
    28.  
    29. void answer();
    30.  
    31. private:
    32. Ui::Widget ui; //deleted * means not pointer anymore?
    33. };
    34.  
    35. #endif // WIDGET_H
    To copy to clipboard, switch view to plain text mode 

    widget.cpp
    Qt Code:
    1. #include "widget.h"
    2.  
    3. Widget::Widget(QWidget *parent) :
    4. QWidget(parent)
    5. {
    6. ui.setupUi(this); // changed all ui-> to ui. is it now a class or struct?
    7. }
    8.  
    9. void Widget::answer()
    10. {
    11. ui.total->setText(QString::number((ui.def->value()*2)+(ui.soc->value())+(ui.she->value())+(ui.scc->value())));
    12. ui.shlabel->setText(QString::number(ui.she->value()*12));
    13. }
    14.  
    15. void Widget::on_def_valueChanged()
    16. {
    17. answer();
    18. }
    19.  
    20. void Widget::on_soc_valueChanged()
    21. {
    22. answer();
    23. }
    24.  
    25. void Widget::on_scc_valueChanged()
    26. {
    27. answer();
    28. }
    29.  
    30. void Widget::on_she_valueChanged()
    31. {
    32. answer();
    33. }
    34.  
    35.  
    36. //Widget::~Widget()
    37. //{
    38. // delete ui;
    39. //}
    To copy to clipboard, switch view to plain text mode 
    Last edited by TomJoad; 16th March 2011 at 01:47.

  7. #6
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Calc example throwing errors when I try it on my own

    obviously, now ui's instantiation in your code is just like the example.
    you just changed it from a pointer to a non-pointer.
    I hope you understand this difference.

Similar Threads

  1. Replies: 5
    Last Post: 6th September 2011, 23:19
  2. Replies: 3
    Last Post: 12th May 2010, 22:42
  3. errors
    By manu in forum Newbie
    Replies: 4
    Last Post: 16th June 2008, 11:29
  4. Errors
    By hgedek in forum Newbie
    Replies: 7
    Last Post: 25th November 2007, 23:21
  5. Example with errors
    By fahmi in forum Qt Programming
    Replies: 1
    Last Post: 29th July 2007, 01:37

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.