Results 1 to 8 of 8

Thread: Problem with type casting

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

  2. #2
    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 

  3. #3
    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 

  4. #4
    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?

  5. #5
    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
  •  
Qt is a trademark of The Qt Company.