Results 1 to 10 of 10

Thread: How to update lineEdit

  1. #1
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default How to update lineEdit

    Hi all! I'm new to Qt.I need you help.

    It's a simple Dialog base app. the dialog has two lineEdits to receive input data.
    And I use a 2 label to display the results of the transformation of the two lineEdits.

    Qt Code:
    1. #include<QtGui>
    2. #include"dencaler.h"
    3.  
    4. Dencaler::Dencaler(QWidget *parent):QDialog(parent)
    5. {
    6. setupUi(this);
    7. //make lineedits only accepte a certain double
    8. knownTemperatureLineEdit->setValidator(new QDoubleValidator(15.0,101.0,2,knownTemperatureLineEdit));
    9. knowDensityLineEdit->setValidator(new QDoubleValidator(0.1,1.2,2,knowDensityLineEdit));
    10. // Are the above Validators OK? I found that they couldn't work properly. They can accept numbers like this:10000.0.
    11. // I just want the first lineEdit to accept double 15.00~101.00
    12. // and the second lineEdit to accept double range 0.10~1.20
    13.  
    14.  
    15. //connect sigal to slot to make update available
    16. connect(okButton, SIGNAL(clicked()), this, SLOT(update()));
    17. }
    18.  
    19. void Dencaler::on_knowDensityLineEdit_textChanged()
    20. {
    21. okButton->setEnabled(knownTemperatureLineEdit->hasAcceptableInput()&&knowDensityLineEdit->hasAcceptableInput());
    22. // Only the two lineEdit get proper numbers can enable the OK button.
    23. }
    24.  
    25. void Dencaler::update()
    26. {
    27. //implement calculation for the dialog
    28. double val1=(knownTemperatureLineEdit->text()).toDouble();
    29. double val2=(knowDensityLineEdit->text()).toDouble();
    30. double sum=val1+val2;
    31. // Implement plus to simulate data transformation.
    32.  
    33. char str[8];
    34. sprintf(str,"%f",sum);
    35. label20->setText(QString(str));
    36. sprintf(str,"%f",sum+0.3);
    37. label15->setText(QString(str));
    38. QDialog::update();
    39. //Update date in the dialog.
    40. }
    To copy to clipboard, switch view to plain text mode 

    I had posted the main questions inside the code.
    By the way, I still want to set two icons for my app. one for the program, and the other in the dialog title bar following by the dialog title.

    If you have any advises, please help! Thanks for your attention.

  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: How to update lineEdit

    Hi,

    have a look at QWidget::setWindowIcon() and http://doc.trolltech.com/4.4/appicon.html. Why you don't use QDoubleSpinBox instead of QLabel's with a validiator?

    Lykurg

    Edit: and better use QString::number() than sprintf...

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

    HelloDan (16th February 2009)

  4. #3
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to update lineEdit

    Quote Originally Posted by Lykurg View Post
    Hi,

    have a look at QWidget::setWindowIcon() and http://doc.trolltech.com/4.4/appicon.html. Why you don't use QDoubleSpinBox instead of QLabel's with a validiator?

    Lykurg

    Edit: and better use QString::number() than sprintf...
    Because I'm a greenhand, know little about Qt. I have not go over my book yet.

    Thanks

  5. #4
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to update lineEdit

    Quote Originally Posted by Lykurg View Post
    Hi,

    have a look at QWidget::setWindowIcon() and http://doc.trolltech.com/4.4/appicon.html. Why you don't use QDoubleSpinBox instead of QLabel's with a validiator?

    Lykurg

    Edit: and better use QString::number() than sprintf...
    QDoubleSpinBox has up and down buttons to turn it up or down. But I for me, an empty rectangle area is OK.


    What about the update? thanks

  6. #5
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to update lineEdit

    It's my fault to express my views.
    With your help, I had corrected my code in this way:

    Qt Code:
    1. #include<QtGui>
    2. #include"dencaler.h"
    3. #include"QPersonalDoubleValidator.h"
    4.  
    5. Dencaler::Dencaler(QWidget *parent):QDialog(parent)
    6. {
    7. setupUi(this);
    8. //make lineedits only accepte a certain double
    9. knownTemperatureLineEdit->setValidator(new QPersonalDoubleValidator(15.0,101.0,2,knownTemperatureLineEdit));
    10. knowDensityLineEdit->setValidator(new QPersonalDoubleValidator(0.1,1.2,2,knowDensityLineEdit));
    11.  
    12. //connect sigal to slot to make update available
    13. connect(okButton, SIGNAL(clicked()), this, SLOT(update()));
    14. }
    15.  
    16. void Dencaler::on_knowDensityLineEdit_textChanged()
    17. {
    18. okButton->setEnabled(knownTemperatureLineEdit->hasAcceptableInput()&&knowDensityLineEdit->hasAcceptableInput());
    19. }
    20.  
    21. void Dencaler::update()
    22. {
    23. //implement calculation for the dialog
    24. double val1=(knownTemperatureLineEdit->text()).toDouble();
    25. double val2=(knowDensityLineEdit->text()).toDouble();
    26. double sum=val1+val2;
    27.  
    28.  
    29. label20->setText(QString::number(sum));
    30. sum+=0.3;
    31. label15->setText(QString::number(sum));
    32. QDialog::update();
    33. }
    To copy to clipboard, switch view to plain text mode 

    But that's still a problem.
    I intend to use the two number input in the QLineEdits to implement some caculation and then set the results in two labels to display them. I wrote the action in an overloaded update(), but i doesn't work. the code post as follow:

    Qt Code:
    1. void Dencaler::update()
    2. {
    3. //implement calculation for the dialog
    4. double val1=(knownTemperatureLineEdit->text()).toDouble();
    5. double val2=(knowDensityLineEdit->text()).toDouble();
    6. double sum=val1+val2;
    7.  
    8.  
    9. label20->setText(QString::number(sum));
    10. sum+=0.3;
    11. label15->setText(QString::number(sum));
    12. QDialog::update();
    13. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to update lineEdit

    what do you what to update?
    void QWidget::update () [slot]
    Updates the widget unless updates are disabled or the widget is hidden.
    This function does not cause an immediate repaint; instead it schedules a paint event for processing when Qt returns to the main event loop. This permits Qt to optimize for more speed and less flicker than a call to repaint() does.
    it's used for repainting.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  8. #7
    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: How to update lineEdit

    Instead of overloading the update function you should create a simple slot and connect it to the change signal of your QLineEdit's. (or as you currently do with your ok button). Then all should work? Or what exactly do you want? To be "updated" whenever a value has changes just connect the textChanged signal for the QLineEdit's to your "update"-slot.
    Qt Code:
    1. //connect(okButton, SIGNAL(clicked()), this, SLOT(recalculate()));
    2. connect(knownTemperatureLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(recalculate()));
    3. connect(knowDensityLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(recalculate()));
    4.  
    5. //...
    6.  
    7. void Dencaler::recalculate()
    8. {
    9. double sum=knownTemperatureLineEdit->text().toDouble() + knowDensityLineEdit->text().toDouble();
    10.  
    11. label20->setText(QString::number(sum));
    12. sum+=0.3;
    13. label15->setText(QString::number(sum));
    14. }
    To copy to clipboard, switch view to plain text mode 

  9. #8
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to update lineEdit

    Thanks for your advises. But the problem is not solved. The main problem is that I just want the label names to change at the same time display the changes in the dialog.

    Qt Code:
    1. #ifndef DENCALER_H
    2. #define DENCALER_H
    3.  
    4. #include<QDialog>
    5. #include"ui_dencaler.h"
    6.  
    7. class Dencaler:public QDialog,public Ui::Dencaler
    8. {
    9. public:
    10. Dencaler(QWidget *parent=0);
    11. private slots:
    12. void on_knownTemperatureLineEdit_textChanged();
    13. void on_knownDensityLineEdit_textChanged();
    14. //void update();
    15. void updateLabels();
    16. };
    17.  
    18. #endif
    19.  
    20.  
    21. #include<QtGui>
    22. #include"dencaler.h"
    23. #include"QPersonalDoubleValidator.h"
    24.  
    25. Dencaler::Dencaler(QWidget *parent):QDialog(parent)
    26. {
    27. setupUi(this);
    28. //make lineedits only accepte a certain double
    29. knownTemperatureLineEdit->setValidator(new QPersonalDoubleValidator(15.0,101.0,2,knownTemperatureLineEdit));
    30. knownDensityLineEdit->setValidator(new QPersonalDoubleValidator(0.1,1.2,2,knownDensityLineEdit));
    31.  
    32. //connect sigal to slot to make update available
    33. connect(okButton, SIGNAL(clicked()), this, SLOT(updateLabels()));
    34.  
    35. }
    36.  
    37.  
    38. void Dencaler::on_knownTemperatureLineEdit_textChanged()
    39. {
    40. okButton->setEnabled(knownTemperatureLineEdit->hasAcceptableInput()&&knownDensityLineEdit->hasAcceptableInput());
    41. }
    42.  
    43.  
    44. void Dencaler::on_knownDensityLineEdit_textChanged()
    45. {
    46. okButton->setEnabled(knownTemperatureLineEdit->hasAcceptableInput()&&knownDensityLineEdit->hasAcceptableInput());
    47. }
    48.  
    49.  
    50. void Dencaler::updateLabels()
    51. {
    52. double sum=knownTemperatureLineEdit->text().toDouble() + knownDensityLineEdit->text().toDouble();
    53.  
    54. label20->setText(QString::number(sum));
    55. sum+=0.3;
    56. label15->setText(QString::number(sum));
    57. QDialog::repaint();
    58. }
    To copy to clipboard, switch view to plain text mode 

    Please help to correct my code. Thanks

  10. #9
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: How to update lineEdit

    you need to change data whet it is being chenged in line edits? I really don't understand what you whant. btw, you don't need to force painting using repaint.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  11. #10
    Join Date
    Feb 2009
    Location
    Guangzhou,China
    Posts
    89
    Thanks
    17
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to update lineEdit

    Thanks anyway!
    It's a very stupid mistake.

    The Q_OBJECT macro was missing. Somebody pointed it out for me.

Similar Threads

  1. Qt Update project - Opinions wanted
    By pvdk in forum Qt Programming
    Replies: 0
    Last Post: 8th November 2008, 08:41
  2. update() works fine sometime but not everytime..!!
    By salmanmanekia in forum Qt Programming
    Replies: 19
    Last Post: 22nd August 2008, 09:11
  3. Copy / Paste doesn't work with LineEdit
    By ia32 in forum Qt Tools
    Replies: 2
    Last Post: 5th May 2008, 21:44
  4. QPainter update()
    By csvivek in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2008, 09:42
  5. How to update statusbar and tooltips
    By aamer4yu in forum Qt Programming
    Replies: 9
    Last Post: 21st December 2006, 12:38

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.