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...
Re: How to update lineEdit
Quote:
Originally Posted by
Lykurg
Because I'm a greenhand, know little about Qt. I have not go over my book yet.
Thanks
Re: How to update lineEdit
Quote:
Originally Posted by
Lykurg
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
Re: How to update lineEdit
It's my fault to express my views.
With your help, I had corrected my code in this way:
Code:
#include<QtGui>
#include"dencaler.h"
#include"QPersonalDoubleValidator.h"
{
setupUi(this);
//make lineedits only accepte a certain double
knownTemperatureLineEdit->setValidator(new QPersonalDoubleValidator(15.0,101.0,2,knownTemperatureLineEdit));
knowDensityLineEdit->setValidator(new QPersonalDoubleValidator(0.1,1.2,2,knowDensityLineEdit));
//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
));
}
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:
Code:
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
));
}
Re: How to update lineEdit
what do you what to update?
Quote:
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.
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.
Code:
//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
));
}
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.
Code:
#ifndef DENCALER_H
#define DENCALER_H
#include<QDialog>
#include"ui_dencaler.h"
class Dencaler
:public QDialog,
public Ui
::Dencaler{
public:
private slots:
void on_knownTemperatureLineEdit_textChanged();
void on_knownDensityLineEdit_textChanged();
//void update();
void updateLabels();
};
#endif
#include<QtGui>
#include"dencaler.h"
#include"QPersonalDoubleValidator.h"
{
setupUi(this);
//make lineedits only accepte a certain double
knownTemperatureLineEdit->setValidator(new QPersonalDoubleValidator(15.0,101.0,2,knownTemperatureLineEdit));
knownDensityLineEdit->setValidator(new QPersonalDoubleValidator(0.1,1.2,2,knownDensityLineEdit));
//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
));
}
Please help to correct my code. Thanks
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. :confused: btw, you don't need to force painting using repaint.
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.