Results 1 to 3 of 3

Thread: Setting the text of a QLineEdit

  1. #1
    Join Date
    Sep 2008
    Posts
    24
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Setting the text of a QLineEdit

    Hi everyone,

    Real n00b question here... I'm developing a simple calculator (just for addition). Have 2 QLineEdits for entering two numbers, a QPushButton to initiate the addition and a third QLineEdit to display the answer.

    Now everything is going grand, all the connects are good etc but I can't set the answer to display in the third QLineEdit.

    Here's the pseudo code

    // firstNum and secondNum are the two input QLineEdits
    void addition::add()
    {
    int num1, num2, r;

    num1 = firstNum->text().toInt();
    num2 = secondNum->text().toInt();

    r = num1 + num2;

    result = new QString(r);


    answer->setText(result);
    }
    Now this tells me "error C2664: 'QLineEdit::setText' : cannot convert parameter 1 from 'QString *' to 'const QString &'" so I change the line to

    answer->setText(*result)

    which compiles but nothing will display in the answer section. I'm at wits end here, seems like I've tried everything! I've gotten other text in there (just debugging) but how do I get the result?

    Cheers, any help appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Setting the text of a QLineEdit

    Create the string on the stack and not on heap.
    Qt Code:
    1. QString result = QString::number(num1+num2); // instead of new ...
    2. answer->setText(result);
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to wysota for this useful post:

    td (11th September 2008)

  4. #3
    Join Date
    Sep 2008
    Posts
    24
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Setting the text of a QLineEdit

    Thanks very much. Argh, why didn't I see that?! Just starting out with it as you may have guessed

Similar Threads

  1. Unhandled exception in qatomic
    By NewGuy in forum Qt Programming
    Replies: 14
    Last Post: 23rd July 2013, 09:49
  2. Text before editting on QLineEdit
    By ^NyAw^ in forum Qt Programming
    Replies: 2
    Last Post: 8th September 2008, 15:51
  3. QLineEdit text() crash - Qt3
    By user_mail07 in forum Qt Programming
    Replies: 7
    Last Post: 10th June 2008, 09:42
  4. Pointer Question related to QLineEdit
    By ChrisReath in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2008, 15:13
  5. Setting text color on QLabel
    By Yorma in forum Newbie
    Replies: 11
    Last Post: 15th June 2006, 07:25

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.