PDA

View Full Version : QLineEdit problem



kumarpraveen
12th July 2010, 05:37
hi,
I am writing a simple converter application here when we give a decimal number (0-255) then its convert in hex and binary or hex is convert decimal and binary ..., all are synchronize using signal-slot. i use validator for control use input. but it not showing me desired result. :(

Thanks in advance

ByteConvertorDialog.h


#ifndef BYTECONVERTERDIALOG_H
#define BYTECONVERTERDIALOG_H

#include <QWidget>

class QLineEdit; //forward declartion

class ByteConverterDialog : public QWidget
{
Q_OBJECT
public:
ByteConverterDialog();
private:
QLineEdit* decEdit;
QLineEdit* hexEdit;
QLineEdit* binEdit;
private slots:
void decChanged(const QString&);
void hexChanged(const QString&);
void binChanged(const QString&);
};

#endif // BYTECONVERTERDIALOG_H


ByteConvertorDialog.cpp


#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QIntValidator>
#include <QRegExpValidator>
#include "ByteConverterDialog.h"

ByteConverterDialog::ByteConverterDialog()
{
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QGridLayout *editLayout = new QGridLayout;
QHBoxLayout *buttonLayout = new QHBoxLayout;

mainLayout->addLayout(editLayout);
mainLayout->addStretch();
mainLayout->addLayout(buttonLayout);

QLabel *decLabel = new QLabel(tr("Decimal"));
QLabel *hexLabel = new QLabel(tr("Hex"));
QLabel *binLabel = new QLabel(tr("Binary"));
decEdit = new QLineEdit;
hexEdit = new QLineEdit;
binEdit = new QLineEdit;

editLayout->addWidget(decLabel,0,0);
editLayout->addWidget(decEdit,0,1);
editLayout->addWidget(hexLabel,1,0);
editLayout->addWidget(hexEdit,1,1);
editLayout->addWidget(binLabel,2,0);
editLayout->addWidget(binEdit,2,1);

QPushButton *exitButton = new QPushButton(tr("Quit"));

buttonLayout->addStretch();
buttonLayout->addWidget(exitButton);

exitButton->setDefault(true);

this->setWindowTitle("Byte Converter");

QIntValidator* decValidator = new QIntValidator(0,255,decEdit);
decEdit->setValidator(decValidator);

QRegExpValidator* hexValidator = new QRegExpValidator(QRegExp("[0-9A-Fa-f]{1,2}"),hexEdit);
hexEdit->setValidator(hexValidator);

QRegExpValidator* binValidator = new QRegExpValidator(QRegExp("[01]{1,8}"),binEdit);
binEdit->setValidator(binValidator);

connect(exitButton,SIGNAL(clicked()),this,SLOT(clo se()));
connect(decEdit,SIGNAL(textChanged(QString)),this, SLOT(decChanged(QString)));
connect(hexEdit,SIGNAL(textChanged(QString)),this, SLOT(hexChanged(QString)));
connect(binEdit,SIGNAL(textChanged(QString)),this, SLOT(binChanged(QString)));
}

void ByteConverterDialog::decChanged(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok);
if(ok)
{
hexEdit->setText(QString::number(num,16));
binEdit->setText(QString::number(num,2));
}
else
{
hexEdit->setText("");
binEdit->setText("");
}
}

void ByteConverterDialog::hexChanged(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok);
if(ok)
{
decEdit->setText(QString::number(num));
binEdit->setText(QString::number(num,2));
}
else
{
decEdit->setText("");
binEdit->setText("");
}
}

void ByteConverterDialog::binChanged(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok);
if(ok)
{
decEdit->setText(QString::number(num));
hexEdit->setText(QString::number(num,16));
}
else
{
decEdit->setText("");
hexEdit->setText("");
}
}


main.cpp


#include <QApplication>
#include "ByteConverterDialog.h"

int main(int argc,char *argv[])
{
QApplication app(argc,argv);
ByteConverterDialog bc;
bc.setAttribute(Qt::WA_QuitOnClose);
bc.show();
return app.exec();
}

Zlatomir
12th July 2010, 05:51
The only problem i see (at first look) is the textChanged() signal, that gets emited even when you don't want to, so use textEdited:


connect(decEdit,SIGNAL(textEdited(QString)),this,S LOT(decChanged(QString)));
//...


LE: The textChanged() is emitted even when when your lineEdit is updated from modify in another lineEdit and you want the signal emitted only when the user writes in that lineEdit

kumarpraveen
12th July 2010, 06:20
The only problem i see (at first look) is the textChanged() signal, that gets emited even when you don't want to, so use textEdited:


connect(decEdit,SIGNAL(textEdited(QString)),this,S LOT(decChanged(QString)));
//...


LE: The textChanged() is emitted even when when your lineEdit is updated from modify in another lineEdit and you want the signal emitted only when the user writes in that lineEdit

Thanks a lot.

Zlatomir
12th July 2010, 06:26
Another thing, use base 16 when convert from QString into int in hexChanged (because you will get empty string if you write FF or any "letter" in the hex lineEdit)


void ByteConverterDialog::hexChanged(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok, 16); // Here you need to use base 16

if(ok)
{
decEdit->setText(QString::number(num));
binEdit->setText(QString::number(num,2));
}
else
{
decEdit->setText("");
binEdit->setText("");
}
}

Zlatomir
12th July 2010, 06:34
Sorry for double post, but i found another little bug:
the same thing needs to be done with binChanged():


void ByteConverterDialog::binChanged(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok, 2); //use base 2 to get the results you want
if(ok)
{
decEdit->setText(QString::number(num));
hexEdit->setText(QString::number(num,16));
}
//...
}

kumarpraveen
12th July 2010, 12:19
yeah you are right. Thanks