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>
{
Q_OBJECT
public:
Dconvert();
public slots:
void convert();
private:
};
#endif // DCONVERT_H
#include <QtGui>
#include "dconvert.h"
Dconvert::Dconvert()
{
sourceUnit->addItem(tr("km"));
sourceUnit->addItem(tr("Nautical mile"));
targetUnit->addItem(tr("km"));
targetUnit->addItem(tr("Nautical mile"));
connect(inputLine,
SIGNAL(textChanged
(QString)),
this,
SLOT(convert
()));
inputLayout->addWidget(sourceVal);
inputLayout->addWidget(inputLine);
inputLayout->addWidget(sourceUnit);
targetLayout->addWidget(targetVal);
targetLayout->addWidget(resultLine);
targetLayout->addWidget(targetUnit);
mainLayout->addLayout(inputLayout);
mainLayout->addLayout(targetLayout);
setLayout(mainLayout);
setWindowTitle(tr("DConvert!"));
}
void Dconvert::convert()
{
resultLine->setText(inputLine->text());
}
#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)),this,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());
}
To copy to clipboard, switch view to plain text mode
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)),
this,
SLOT(convert
()));
...
...
void Dconvert::convert()
{
double *resultaat = new double;
resultaat = (double*) &inputLine->text();
resultLine
->setText
(QString (*resultaat
));
}
...
connect(inputLine,SIGNAL(textChanged(QString)),this,SLOT(convert()));
...
...
void Dconvert::convert()
{
double *resultaat = new double;
resultaat = (double*) &inputLine->text();
resultLine->setText(QString (*resultaat));
}
To copy to clipboard, switch view to plain text mode
After compiling, this is the error I receive:
dconvert.cpp:58: error: conversion from `double' to `QChar' is ambiguous
dconvert.cpp:58: error: conversion from `double' to `QChar' is ambiguous
To copy to clipboard, switch view to plain text mode
Bookmarks