iambehnam
یک شنبه 19 خرداد 1392, 12:28 عصر
سلام به همهی دوستان عزیز
مرتب نوشتم که اعتراض نکنیدا:بامزه:
این کد کوچولو (کپی از کتاب the-book-of-qt4) اجرا میشه ولی درست کار نمیده
قسمت main و header چیز خاصی نداشت. قسمت مورد سؤال:
// byteConverter/ByteConverterDialog.cpp
#include "byteconverterdialog.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QValidator>
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("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);
// 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);
connect(exitButton, SIGNAL(clicked()), this, SLOT(accept()));
// Limit input to valid values
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(decEdit, SIGNAL(textChanged(const QString&)), this, SLOT(decChanged(const QString&)));
connect(hexEdit, SIGNAL(textChanged(const QString&)), this, SLOT(hexChanged(const QString&)));
connect(binEdit, SIGNAL(textChanged(const QString&)), this, SLOT(binChanged(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 {
decEdit->setText("");
hexEdit->setText("");
}
}
بنظرتون مشکل چیه؟
مرتب نوشتم که اعتراض نکنیدا:بامزه:
این کد کوچولو (کپی از کتاب the-book-of-qt4) اجرا میشه ولی درست کار نمیده
قسمت main و header چیز خاصی نداشت. قسمت مورد سؤال:
// byteConverter/ByteConverterDialog.cpp
#include "byteconverterdialog.h"
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QValidator>
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("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);
// 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);
connect(exitButton, SIGNAL(clicked()), this, SLOT(accept()));
// Limit input to valid values
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(decEdit, SIGNAL(textChanged(const QString&)), this, SLOT(decChanged(const QString&)));
connect(hexEdit, SIGNAL(textChanged(const QString&)), this, SLOT(hexChanged(const QString&)));
connect(binEdit, SIGNAL(textChanged(const QString&)), this, SLOT(binChanged(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 {
decEdit->setText("");
hexEdit->setText("");
}
}
بنظرتون مشکل چیه؟