Results 1 to 8 of 8

Thread: Problem with type casting

  1. #1
    Join Date
    Nov 2009
    Location
    Belgium
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem with type casting

    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.

    Qt Code:
    1. #ifndef DCONVERT_H
    2. #define DCONVERT_H
    3.  
    4. #include <QWidget>
    5.  
    6. class QLineEdit;
    7. class QComboBox;
    8.  
    9. class Dconvert : public QWidget
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. Dconvert();
    15.  
    16. public slots:
    17. void convert();
    18.  
    19. private:
    20. QLineEdit *inputLine;
    21. QLineEdit *resultLine;
    22. QComboBox *sourceUnit;
    23. QComboBox *targetUnit;
    24. };
    25. #endif // DCONVERT_H
    26.  
    27.  
    28. #include <QtGui>
    29. #include "dconvert.h"
    30.  
    31. Dconvert::Dconvert()
    32. {
    33. QGroupBox *sourceBox = new QGroupBox(tr("From..."));
    34. QLabel *sourceVal = new QLabel(tr("Value:"));
    35. inputLine = new QLineEdit;
    36. sourceUnit = new QComboBox;
    37.  
    38. sourceUnit->addItem(tr("km"));
    39. sourceUnit->addItem(tr("Nautical mile"));
    40.  
    41. QGroupBox *targetBox = new QGroupBox(tr("To..."));
    42. QLabel *targetVal = new QLabel(tr("Value:"));
    43. resultLine = new QLineEdit;
    44. targetUnit = new QComboBox;
    45.  
    46. targetUnit->addItem(tr("km"));
    47. targetUnit->addItem(tr("Nautical mile"));
    48.  
    49. QPushButton *quitButton = new QPushButton(tr("Quit"));
    50.  
    51. connect(inputLine,SIGNAL(textChanged(QString)),this,SLOT(convert()));
    52.  
    53. QHBoxLayout *inputLayout = new QHBoxLayout;
    54. inputLayout->addWidget(sourceVal);
    55. inputLayout->addWidget(inputLine);
    56. inputLayout->addWidget(sourceUnit);
    57.  
    58. QHBoxLayout *targetLayout = new QHBoxLayout;
    59. targetLayout->addWidget(targetVal);
    60. targetLayout->addWidget(resultLine);
    61. targetLayout->addWidget(targetUnit);
    62.  
    63. QVBoxLayout *mainLayout = new QVBoxLayout;
    64. mainLayout->addLayout(inputLayout);
    65. mainLayout->addLayout(targetLayout);
    66.  
    67.  
    68. setLayout(mainLayout);
    69.  
    70.  
    71. setWindowTitle(tr("DConvert!"));
    72.  
    73.  
    74. }
    75. void Dconvert::convert()
    76. {
    77. resultLine->setText(inputLine->text());
    78. }
    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!

    Qt Code:
    1. ...
    2. connect(inputLine,SIGNAL(textChanged(QString)),this,SLOT(convert()));
    3. ...
    4. ...
    5. void Dconvert::convert()
    6. {
    7.  
    8. double *resultaat = new double;
    9. resultaat = (double*) &inputLine->text();
    10. resultLine->setText(QString (*resultaat));
    11. }
    To copy to clipboard, switch view to plain text mode 

    After compiling, this is the error I receive:

    Qt Code:
    1. dconvert.cpp:58: error: conversion from `double' to `QChar' is ambiguous
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: Problem with type casting

    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.
    Last edited by Lykurg; 8th November 2009 at 16:18. Reason: updated contents

  3. #3
    Join Date
    Nov 2009
    Location
    Belgium
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with type casting

    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?

  4. #4
    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: Problem with type casting

    Quote Originally Posted by qtDave View Post
    I have no idea what you are talking aoubt...
    Where and how did I create a double on the heap
    Ehm, here:
    Quote Originally Posted by qtDave View Post
    Qt Code:
    1. double *resultaat = new double;
    To copy to clipboard, switch view to plain text mode 
    , and, what is the alternative and how should I have done this?
    Create int, double etc. on the stack:

    Qt Code:
    1. double resultaat;
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Nov 2009
    Location
    Belgium
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with type casting

    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.

    Qt Code:
    1. void Dconvert::convert()
    2. {
    3.  
    4. double resultaat;
    5. resultaat = (inputLine->text().toDouble());
    6.  
    7. QString resultstring = QString::number(resultaat, 'g',10);
    8.  
    9.  
    10. resultLine->setText(resultstring);
    11. }
    To copy to clipboard, switch view to plain text mode 

  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: Problem with type casting

    Ah, you are an user that reads the docs! That's great

    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.

    Qt Code:
    1. connect(inputLine,SIGNAL(textChanged(const QString &)),this,SLOT(convert(const QString &)));
    2. //...
    3. void Dconvert::convert(const QString &value)
    4. {
    5. double resultaat = value.toDouble();
    6. // convert an alter resultaat
    7. resultLine->setText(QString::number(resultaat, 'g',10));
    8. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Nov 2009
    Location
    Belgium
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem with type casting --- and a note about dynamic allocation

    Quote Originally Posted by Lykurg View Post
    Ah, you are an user that reads the docs! That's great
    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?

  8. #8
    Join Date
    Jan 2006
    Location
    Knivsta, Sweden
    Posts
    153
    Thanks
    30
    Thanked 13 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Problem with type casting

    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.

Similar Threads

  1. Problem regaridng Type casting QVariant
    By sudheer168 in forum Qt Programming
    Replies: 5
    Last Post: 3rd November 2009, 06:02
  2. Unicode Font Display Problem
    By QbelcorT in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 2nd September 2009, 06:11
  3. problem with forward declaration
    By MarkoSan in forum General Programming
    Replies: 14
    Last Post: 6th January 2008, 21:45
  4. quint16 compiling problem
    By MarkoSan in forum Qt Programming
    Replies: 5
    Last Post: 30th November 2007, 18:08
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

Tags for this Thread

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.