#include "byteconverterdialog.h"
#include "byteconverter.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QIntValidator>
byteConverterDialog::byteConverterDialog()
{
//generate the necessary layouts
mainLayout->addLayout(editLayout);
mainLayout->addStretch();
mainLayout->addLayout(buttonLayout);
//generate the labels and line-edits and add them to the object pointed at by editLayout
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);
//create the quit button and add it to the object pointed at by buttonLayout.
buttonLayout->addStretch();
buttonLayout->addWidget(exitButton);
exitButton->setDefault(true);
decEdit->setValidator(decValidator);
hexEdit->setValidator(hexValidator);
binEdit->setValidator(binValidator);
setWindowTitle(tr("Byte Converter"));
connect(exitButton, SIGNAL(clicked()), this, SLOT(accept()));
ByteConverter* bc = new ByteConverter(this);
connect(decEdit,
SIGNAL(textChanged
(const QString &)), bc,
SLOT(setDec
(const QString &)));
connect(hexEdit,
SIGNAL(textChanged
(const QString &)), bc,
SLOT(setHex
(const QString &)));
connect(binEdit,
SIGNAL(textChanged
(const QString &)), bc,
SLOT(setBin
(const QString &)));
connect(bc, SIGNAL(decChanged(const QString&)), decEdit, SLOT(setText(const QString&)));
connect(bc, SIGNAL(hexChanged(const QString&)), hexEdit, SLOT(setText(const QString&)));
connect(bc, SIGNAL(binChanged(const QString&)), binEdit, SLOT(setText(const 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 {
hexEdit->setText("");
decEdit->setText("");
}
}
#include "byteconverterdialog.h"
#include "byteconverter.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QIntValidator>
byteConverterDialog::byteConverterDialog()
{
//generate the necessary layouts
QVBoxLayout *mainLayout = new QVBoxLayout(this);
QGridLayout *editLayout = new QGridLayout;
QHBoxLayout *buttonLayout = new QHBoxLayout;
mainLayout->addLayout(editLayout);
mainLayout->addStretch();
mainLayout->addLayout(buttonLayout);
//generate the labels and line-edits and add them to the object pointed at by editLayout
QLabel *decLabel = new QLabel(tr("Decimal"));
QLabel *hexLabel = new QLabel(tr("hexadecimal"));
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);
//create the quit button and add it to the object pointed at by buttonLayout.
QPushButton *exitButton = new QPushButton(tr("Quit"));
buttonLayout->addStretch();
buttonLayout->addWidget(exitButton);
exitButton->setDefault(true);
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);
setWindowTitle(tr("Byte Converter"));
connect(exitButton, SIGNAL(clicked()), this, SLOT(accept()));
ByteConverter* bc = new ByteConverter(this);
connect(decEdit, SIGNAL(textChanged(const QString &)), bc, SLOT(setDec(const QString &)));
connect(hexEdit, SIGNAL(textChanged(const QString &)), bc, SLOT(setHex(const QString &)));
connect(binEdit, SIGNAL(textChanged(const QString &)), bc, SLOT(setBin(const QString &)));
connect(bc, SIGNAL(decChanged(const QString&)), decEdit, SLOT(setText(const QString&)));
connect(bc, SIGNAL(hexChanged(const QString&)), hexEdit, SLOT(setText(const QString&)));
connect(bc, SIGNAL(binChanged(const QString&)), binEdit, SLOT(setText(const 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 {
hexEdit->setText("");
decEdit->setText("");
}
}
To copy to clipboard, switch view to plain text mode
#include "byteconverter.h"
{
}
void ByteConverter::setDec(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok);
if(ok) {
emit hexChanged
(QString::number(num,
16));
emit binChanged
(QString::number(num,
2));
}
else {
emit hexChanged("");
emit binChanged("");
}
}
void ByteConverter::setHex(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok, 16);
if(ok) {
emit decChanged
(QString::number(num
));
emit binChanged
(QString::number(num,
2));
}
else {
emit decChanged("");
emit binChanged("");
}
}
void ByteConverter::setBin(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok, 2);
if(ok) {
emit decChanged
(QString::number(num
));
emit hexChanged
(QString::number(num,
16));
}
else {
emit decChanged("");
emit hexChanged("");
}
}
#include "byteconverter.h"
ByteConverter::ByteConverter(QObject* parent) : QObject(parent)
{
}
void ByteConverter::setDec(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok);
if(ok) {
emit hexChanged(QString::number(num, 16));
emit binChanged(QString::number(num, 2));
}
else {
emit hexChanged("");
emit binChanged("");
}
}
void ByteConverter::setHex(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok, 16);
if(ok) {
emit decChanged(QString::number(num));
emit binChanged(QString::number(num, 2));
}
else {
emit decChanged("");
emit binChanged("");
}
}
void ByteConverter::setBin(const QString& newValue)
{
bool ok;
int num = newValue.toInt(&ok, 2);
if(ok) {
emit decChanged(QString::number(num));
emit hexChanged(QString::number(num, 16));
}
else {
emit decChanged("");
emit hexChanged("");
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks