PDA

View Full Version : How to update lineEdit



HelloDan
16th February 2009, 11:59
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.



#include<QtGui>
#include"dencaler.h"

Dencaler::Dencaler(QWidget *parent):QDialog(parent)
{
setupUi(this);
//make lineedits only accepte a certain double
knownTemperatureLineEdit->setValidator(new QDoubleValidator(15.0,101.0,2,knownTemperatureLine Edit));
knowDensityLineEdit->setValidator(new QDoubleValidator(0.1,1.2,2,knowDensityLineEdit));
// Are the above Validators OK? I found that they couldn't work properly. They can accept numbers like this:10000.0.
// I just want the first lineEdit to accept double 15.00~101.00
// and the second lineEdit to accept double range 0.10~1.20


//connect sigal to slot to make update available
connect(okButton, SIGNAL(clicked()), this, SLOT(update()));
}

void Dencaler::on_knowDensityLineEdit_textChanged()
{
okButton->setEnabled(knownTemperatureLineEdit->hasAcceptableInput()&&knowDensityLineEdit->hasAcceptableInput());
// Only the two lineEdit get proper numbers can enable the OK button.
}

void Dencaler::update()
{
//implement calculation for the dialog
double val1=(knownTemperatureLineEdit->text()).toDouble();
double val2=(knowDensityLineEdit->text()).toDouble();
double sum=val1+val2;
// Implement plus to simulate data transformation.

char str[8];
sprintf(str,"%f",sum);
label20->setText(QString(str));
sprintf(str,"%f",sum+0.3);
label15->setText(QString(str));
QDialog::update();
//Update date in the dialog.
}


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.

Lykurg
16th February 2009, 12:15
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...

HelloDan
16th February 2009, 12:22
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

HelloDan
16th February 2009, 12:34
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

HelloDan
16th February 2009, 17:05
It's my fault to express my views.
With your help, I had corrected my code in this way:



#include<QtGui>
#include"dencaler.h"
#include"QPersonalDoubleValidator.h"

Dencaler::Dencaler(QWidget *parent):QDialog(parent)
{
setupUi(this);
//make lineedits only accepte a certain double
knownTemperatureLineEdit->setValidator(new QPersonalDoubleValidator(15.0,101.0,2,knownTempera tureLineEdit));
knowDensityLineEdit->setValidator(new QPersonalDoubleValidator(0.1,1.2,2,knowDensityLine Edit));

//connect sigal to slot to make update available
connect(okButton, SIGNAL(clicked()), this, SLOT(update()));
}

void Dencaler::on_knowDensityLineEdit_textChanged()
{
okButton->setEnabled(knownTemperatureLineEdit->hasAcceptableInput()&&knowDensityLineEdit->hasAcceptableInput());
}

void Dencaler::update()
{
//implement calculation for the dialog
double val1=(knownTemperatureLineEdit->text()).toDouble();
double val2=(knowDensityLineEdit->text()).toDouble();
double sum=val1+val2;


label20->setText(QString::number(sum));
sum+=0.3;
label15->setText(QString::number(sum));
QDialog::update();
}


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:



void Dencaler::update()
{
//implement calculation for the dialog
double val1=(knownTemperatureLineEdit->text()).toDouble();
double val2=(knowDensityLineEdit->text()).toDouble();
double sum=val1+val2;


label20->setText(QString::number(sum));
sum+=0.3;
label15->setText(QString::number(sum));
QDialog::update();
}

spirit
16th February 2009, 17:55
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.

Lykurg
16th February 2009, 18:23
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.


//connect(okButton, SIGNAL(clicked()), this, SLOT(recalculate()));
connect(knownTemperatureLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(recalculate()));
connect(knowDensityLineEdit, SIGNAL(textChanged(const QString&)), this, SLOT(recalculate()));

//...

void Dencaler::recalculate()
{
double sum=knownTemperatureLineEdit->text().toDouble() + knowDensityLineEdit->text().toDouble();

label20->setText(QString::number(sum));
sum+=0.3;
label15->setText(QString::number(sum));
}

HelloDan
17th February 2009, 00:58
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.



#ifndef DENCALER_H
#define DENCALER_H

#include<QDialog>
#include"ui_dencaler.h"

class Dencaler:public QDialog,public Ui::Dencaler
{
public:
Dencaler(QWidget *parent=0);
private slots:
void on_knownTemperatureLineEdit_textChanged();
void on_knownDensityLineEdit_textChanged();
//void update();
void updateLabels();
};

#endif


#include<QtGui>
#include"dencaler.h"
#include"QPersonalDoubleValidator.h"

Dencaler::Dencaler(QWidget *parent):QDialog(parent)
{
setupUi(this);
//make lineedits only accepte a certain double
knownTemperatureLineEdit->setValidator(new QPersonalDoubleValidator(15.0,101.0,2,knownTempera tureLineEdit));
knownDensityLineEdit->setValidator(new QPersonalDoubleValidator(0.1,1.2,2,knownDensityLin eEdit));

//connect sigal to slot to make update available
connect(okButton, SIGNAL(clicked()), this, SLOT(updateLabels()));

}


void Dencaler::on_knownTemperatureLineEdit_textChanged( )
{
okButton->setEnabled(knownTemperatureLineEdit->hasAcceptableInput()&&knownDensityLineEdit->hasAcceptableInput());
}


void Dencaler::on_knownDensityLineEdit_textChanged()
{
okButton->setEnabled(knownTemperatureLineEdit->hasAcceptableInput()&&knownDensityLineEdit->hasAcceptableInput());
}


void Dencaler::updateLabels()
{
double sum=knownTemperatureLineEdit->text().toDouble() + knownDensityLineEdit->text().toDouble();

label20->setText(QString::number(sum));
sum+=0.3;
label15->setText(QString::number(sum));
QDialog::repaint();
}


Please help to correct my code. Thanks

spirit
17th February 2009, 05:57
you need to change data whet it is being chenged in line edits? I really don't understand what you whant. :confused: btw, you don't need to force painting using repaint.

HelloDan
17th February 2009, 07:01
Thanks anyway!
It's a very stupid mistake.

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