Link lineEdit to a progressbar
Solved
General and Desktop
-
Hi everyone
I have three lineEdits(lineEdit_1, lineEdit_2 and lineEdit_3 ) in Dialog window. When lineEdit_2 and lineEdit_3 are empty, as the user types something in the lineEdit_1, an arc is supposed to be drawn from 0 to 120 degree.
I have written the following code. My problem is that when I type something in the lineEdit_1, the arc is drawn even if the lineEdit_2 and lineEdit_3 are not empty.
I don't know how to define the situation that just when the lineEdit_2 and lineEdit_3 are empty and the user types something in lineEdit_1, the arc is drawn.
I would appreciate if someone could help meHere is the code:
dialog.h:
#ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QPainter> #include <QPen> #include <QRectF> #include <QSlider> #include <QLineEdit> namespace Ui { class Dialog; } class Dialog : public QDialog { Q_OBJECT public: explicit Dialog(QWidget *parent = 0); ~Dialog(); double progress; QLineEdit *lineEdit_1; QLineEdit *lineEdit_2 ; QLineEdit *lineEdit_3; public slots: void setProgress(); protected: void paintEvent(QPaintEvent *); private: Ui::Dialog *ui; }; #endif // DIALOG_H
dialog.cpp:
#include "dialog.h" #include "ui_dialog.h" Dialog::Dialog(QWidget *parent) : QDialog(parent), ui(new Ui::Dialog) { ui->setupUi(this); lineEdit_1 = new QLineEdit(this); lineEdit_1->setGeometry(500,50,100,50); lineEdit_1->show(); lineEdit_2 = new QLineEdit(this); lineEdit_2->setGeometry(500,150,100,50); lineEdit_2->show(); lineEdit_3 = new QLineEdit(this); lineEdit_3->setGeometry(500,250,100,50); lineEdit_3->show(); if(lineEdit_2->text().isEmpty() && lineEdit_3->text().isEmpty()) { connect(lineEdit_1, SIGNAL(textChanged(const QString &)), this, SLOT(setProgress())); } } Dialog::~Dialog() { delete ui; } void Dialog::setProgress() { progress = 120; this->update(); } void Dialog::paintEvent(QPaintEvent *) { QPainter p(this); QPen pen; pen.setWidth(7); pen.setColor(Qt::red); p.setPen(pen); p.setRenderHint(QPainter::Antialiasing); QRectF rectangle(30.0, 20.0, 200.0, 200.0); int startAngle = 0 * 16; int spanAngle = progress * 16; p.drawArc(rectangle, startAngle, spanAngle); //the number texted in the circle center p.drawText(rectangle,Qt::AlignCenter,QString::number(progress)+" %"); }
main.cpp:
#include "dialog.h" #include <QApplication> #include <QSlider> int main(int argc, char *argv[]) { QApplication a(argc, argv); Dialog w; w.show(); return a.exec(); }
The output:
-
Hi,
Check the status of all your line edits in the slot and set the value accordingly.