PDA

View Full Version : Calc example throwing errors when I try it on my own



TomJoad
15th March 2011, 21:47
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/designer-calculatorform.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:


..\test\widget.cpp: In member function 'void Widget::on_def_valueChanged(int)':
..\test\widget.cpp:17: error: request for member 'total' in '((Widget*)this)->Widget::ui', which is of non-class type 'Ui::Widget*'
..\test\widget.cpp:17: error: request for member 'soc' in '((Widget*)this)->Widget::ui', which is of non-class type 'Ui::Widget*'

Here is widget.cpp:

#include "widget.h"

Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}

Widget::~Widget()
{
delete ui;
}

void Widget::on_def_valueChanged(int value)
{
ui.total->setText(QString::number(value * 2 + ui.soc->value()));
}

main.cpp:

#include <QtGui/QApplication>
#include "widget.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();

return a.exec();
}


widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

#include "ui_widget.h"

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();

private slots:
void on_def_valueChanged(int value);

private:
Ui::Widget *ui;
};

#endif // WIDGET_H

and test.pro

#-------------------------------------------------
#
# Project created by QtCreator 2011-03-15T16:01:49
#
#-------------------------------------------------

QT += core gui

TARGET = test
TEMPLATE = app


SOURCES += main.cpp\
widget.cpp

HEADERS += widget.h

FORMS += widget.ui


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

schnitzel
15th March 2011, 21:54
shouldn't that be ui->soc instead of ui.soc? Similarly for the other items.

TomJoad
15th March 2011, 22:11
Genius!

However, why does the example state:


void CalculatorForm::on_inputSpinBox1_valueChanged(int value)
{
ui.outputWidget->setText(QString::number(value + ui.inputSpinBox2->value()));
}


The example compiles and works just fine.

Either way, thank you for your help.

schnitzel
15th March 2011, 22:20
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.

TomJoad
16th March 2011, 02:47
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

#include "widget.h"

Widget::Widget(QWidget *parent) :
QWidget(parent)
{
ui->setupUi(this);
}

void Widget::answer()
{
ui->total->setText(QString::number((ui->def->value()*2)+(ui->soc->value())+(ui->she->value())+(ui->scc->value())));
ui->shlabel->setText(QString::number(ui->she->value()*12));
}

void Widget::on_def_valueChanged()
{
answer();
}

void Widget::on_soc_valueChanged()
{
answer();
}

void Widget::on_scc_valueChanged()
{
answer();
}

void Widget::on_she_valueChanged()
{
answer();
}


Widget::~Widget()
{
delete ui;
}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QString>

#include "ui_widget.h"

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
~Widget();

private slots:
void on_def_valueChanged();

void on_soc_valueChanged();

void on_scc_valueChanged();

void on_she_valueChanged();

void answer();

private:
Ui::Widget *ui;
};

#endif // WIDGET_H

Added after 11 minutes:

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

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

#include "ui_widget.h"

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
Q_OBJECT

public:
explicit Widget(QWidget *parent = 0);
// ~Widget();

private slots:
void on_def_valueChanged();

void on_soc_valueChanged();

void on_scc_valueChanged();

void on_she_valueChanged();

void answer();

private:
Ui::Widget ui; //deleted * means not pointer anymore?
};

#endif // WIDGET_H

widget.cpp

#include "widget.h"

Widget::Widget(QWidget *parent) :
QWidget(parent)
{
ui.setupUi(this); // changed all ui-> to ui. is it now a class or struct?
}

void Widget::answer()
{
ui.total->setText(QString::number((ui.def->value()*2)+(ui.soc->value())+(ui.she->value())+(ui.scc->value())));
ui.shlabel->setText(QString::number(ui.she->value()*12));
}

void Widget::on_def_valueChanged()
{
answer();
}

void Widget::on_soc_valueChanged()
{
answer();
}

void Widget::on_scc_valueChanged()
{
answer();
}

void Widget::on_she_valueChanged()
{
answer();
}


//Widget::~Widget()
//{
// delete ui;
//}

schnitzel
16th March 2011, 17:02
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.