PDA

View Full Version : Problem with type casting



qtDave
8th November 2009, 15:42
Dear fellow Qt progammers,

I am rather new to C++/Qt and programming in general (although I have quite abit bit of experience in scripting languages as bash, Matlab etc.)

As a start, I am writing a program that does conversions between units (e.g. length (km to miles), area (sq m to sq ft) etc.)

1. I have created a small widget that has four fields:
- inputLine (QLineEdit) : input value field
- sourceUnit (QComboBox) : input unit

- resultLine (QLineEdit) : target value field
- targetUnit (QComboBox) : target unit

The goal is that the end user selects input & target unit, and the input value is converted to the target as it is typed (I used a QDoubleValidator to check for a double, and a simple SIGNAL/SLOT to make appear the input immedately in the target value field (without conversion)). At the moment, the units are only placeholders. This worked fine.


#ifndef DCONVERT_H
#define DCONVERT_H

#include <QWidget>

class QLineEdit;
class QComboBox;

class Dconvert : public QWidget
{
Q_OBJECT

public:
Dconvert();

public slots:
void convert();

private:
QLineEdit *inputLine;
QLineEdit *resultLine;
QComboBox *sourceUnit;
QComboBox *targetUnit;
};
#endif // DCONVERT_H


#include <QtGui>
#include "dconvert.h"

Dconvert::Dconvert()
{
QGroupBox *sourceBox = new QGroupBox(tr("From..."));
QLabel *sourceVal = new QLabel(tr("Value:"));
inputLine = new QLineEdit;
sourceUnit = new QComboBox;

sourceUnit->addItem(tr("km"));
sourceUnit->addItem(tr("Nautical mile"));

QGroupBox *targetBox = new QGroupBox(tr("To..."));
QLabel *targetVal = new QLabel(tr("Value:"));
resultLine = new QLineEdit;
targetUnit = new QComboBox;

targetUnit->addItem(tr("km"));
targetUnit->addItem(tr("Nautical mile"));

QPushButton *quitButton = new QPushButton(tr("Quit"));

connect(inputLine,SIGNAL(textChanged(QString)),thi s,SLOT(convert()));

QHBoxLayout *inputLayout = new QHBoxLayout;
inputLayout->addWidget(sourceVal);
inputLayout->addWidget(inputLine);
inputLayout->addWidget(sourceUnit);

QHBoxLayout *targetLayout = new QHBoxLayout;
targetLayout->addWidget(targetVal);
targetLayout->addWidget(resultLine);
targetLayout->addWidget(targetUnit);

QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addLayout(inputLayout);
mainLayout->addLayout(targetLayout);


setLayout(mainLayout);


setWindowTitle(tr("DConvert!"));


}
void Dconvert::convert()
{
resultLine->setText(inputLine->text());
}


Next, I want the input string from inputLine to be regarded as a double, to be processed and returned to the resultLine.

Now, here the problems begin:

From QString to double seems to work, but after processing it doesnt.... This must be a very basic problem but I just don't find the solution... help please!



...
connect(inputLine,SIGNAL(textChanged(QString)),thi s,SLOT(convert()));
...
...
void Dconvert::convert()
{

double *resultaat = new double;
resultaat = (double*) &inputLine->text();
resultLine->setText(QString (*resultaat));
}

After compiling, this is the error I receive:


dconvert.cpp:58: error: conversion from `double' to `QChar' is ambiguous

Lykurg
8th November 2009, 16:16
Hi,

fist please, please don't create a double on the heap! Second have a look at QString::number() to convert a number to a QString.


And keep on going!

EDIT: And QString::toDouble() could be also worth to look at.

qtDave
8th November 2009, 18:40
Thank you for the advise. I will look into this and post my proceedings.


please, please don't create a double on the heap!
I have no idea what you are talking aoubt...
Where and how did I create a double on the heap, and, what is the alternative and how should I have done this?

Lykurg
8th November 2009, 19:01
I have no idea what you are talking aoubt...
Where and how did I create a double on the heap

Ehm, here:


double *resultaat = new double;


, and, what is the alternative and how should I have done this?

Create int, double etc. on the stack:


double resultaat;

qtDave
8th November 2009, 19:22
I need a bit of reading. I'll do that.

I taken both your advice and produced this, which works very well. I can continue working with this.


void Dconvert::convert()
{

double resultaat;
resultaat = (inputLine->text().toDouble());

QString resultstring = QString::number(resultaat, 'g',10);


resultLine->setText(resultstring);
}

Lykurg
8th November 2009, 19:56
Ah, you are an user that reads the docs! That's great :D

Some point on your code: the enclosing brackets are not needed and if you are not going to alter resultstring (I suppose so), then avoid creating a temporary variable. Just pass it to your function. And if you put a variable to your slots, you don't have to request the value of your lineedit, because it is already send by the signal.


connect(inputLine,SIGNAL(textChanged(const QString &)),this,SLOT(convert(const QString &)));
//...
void Dconvert::convert(const QString &value)
{
double resultaat = value.toDouble();
// convert an alter resultaat
resultLine->setText(QString::number(resultaat, 'g',10));
}

qtDave
9th November 2009, 19:10
Ah, you are an user that reads the docs! That's great :D


Thanks for your latest comments about my code. I appreciate such small corrections a lot.

I did my homework.

The stack is the 'regular' memory where variables are stored. The heap is the memory between the program code and the stack. It is FINITE in size. That is why you mentioned not to create variables in the heap?
I saw the difference and learned that created variables in heap must also be destroyed using delete.

Is there any other reason why not to use the heap for regular variables?

drhex
9th November 2009, 20:57
Is there any other reason why not to use the heap for regular variables?

Allocating memory on the stack simply implies subtracting a value from the stack pointer; allocating on the heap involves a more complicated function call with higher overhead which only makes sense for larger datastructures.

The stack and heap are both finite in size, but there is typically more memory available on the heap.