#include <QApplication>
#include <QPushButton>
#include <QWidget>
#include <QGridLayout>
#include <QString>
#include <QLineEdit>
#include <QVBoxLayout>
char* DigitAsString(int number)
{
switch(number)
{
case 0: return "0";
case 1: return "1";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
default: return "";
}
}
{
Q_OBJECT
public:
void InitOperationButtons();
void InitDigitButtons();
void InitLineEdit();
void LayoutButtons();
//Add(double, double);
//Divide(double, double);
public slots:
void numberPressed()
{
editLine->setText(tr("1"));
}
private:
enum { NUM_KEYS = 12 };
QPushButton *plusButton,
*minusButton,
*multipleButton,
*divideButton;
QPushButton *numberKeys
[NUM_KEYS
];
//Всички цифри като пушБутони QGridLayout *buttonsLayout;
//Разположението на бутоните };
Calculator
::Calculator(QWidget* parent
){
InitOperationButtons();
InitDigitButtons();
InitLineEdit();
//
LayoutButtons();
commonLayout->addWidget(editLine);
commonLayout->addLayout(buttonsLayout);
//
connect(numberKeys[0], SIGNAL(clicked()), this, SLOT(numberPressed()));
//
setWindowTitle(tr("Calculator"));
setLayout(commonLayout);
setFixedSize(sizeHint()); //Най - добрата фиксирана позиция за елементите
}
void Calculator::InitOperationButtons()
{
}
void Calculator::InitDigitButtons()
{
for(int i = 0; i < NUM_KEYS; i++)
{
if(i == 10)
{
continue;
}
if(i == 11)
{
continue;
}
numberKeys
[i
] = new QPushButton(tr
(DigitAsString
(i
)),
this);
//Текста на бутоните ще бъде както следват цифрите
}
}
void Calculator::InitLineEdit()
{
editLine->setMaxLength(15); //Дължината на числото
}
void Calculator::LayoutButtons()
{
buttonsLayout -> addWidget(plusButton, 0, 0);
buttonsLayout -> addWidget(minusButton, 0, 1);
buttonsLayout -> addWidget(divideButton, 0, 2);
buttonsLayout -> addWidget(multipleButton, 0, 3);
int row = 1;
int col = 0;
for(int i = 0; i < NUM_KEYS; i++, col++)
{
if(i % 4 == 0)
{
col = 0;
buttonsLayout -> addWidget(numberKeys[i], ++row, col);
}else
{
buttonsLayout -> addWidget(numberKeys[i], row, col);
}
}
}
int main(int argc, char* argv[])
{
Calculator* calculator = new Calculator();
calculator -> show();
return qapp.exec();
}
#include <QApplication>
#include <QPushButton>
#include <QWidget>
#include <QGridLayout>
#include <QString>
#include <QLineEdit>
#include <QVBoxLayout>
char* DigitAsString(int number)
{
switch(number)
{
case 0: return "0";
case 1: return "1";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
default: return "";
}
}
class Calculator: public QWidget
{
Q_OBJECT
public:
Calculator(QWidget* parent = 0);
void InitOperationButtons();
void InitDigitButtons();
void InitLineEdit();
void LayoutButtons();
//Add(double, double);
//Divide(double, double);
public slots:
void numberPressed()
{
editLine->setText(tr("1"));
}
private:
enum { NUM_KEYS = 12 };
QPushButton *plusButton, *minusButton, *multipleButton, *divideButton;
QPushButton *numberKeys[NUM_KEYS]; //Всички цифри като пушБутони
QGridLayout *buttonsLayout; //Разположението на бутоните
QVBoxLayout *commonLayout;
QLineEdit* editLine;
};
Calculator::Calculator(QWidget* parent)
: QWidget(parent)
{
commonLayout = new QVBoxLayout;
buttonsLayout = new QGridLayout;
InitOperationButtons();
InitDigitButtons();
InitLineEdit();
//
LayoutButtons();
commonLayout->addWidget(editLine);
commonLayout->addLayout(buttonsLayout);
//
connect(numberKeys[0], SIGNAL(clicked()), this, SLOT(numberPressed()));
//
setWindowTitle(tr("Calculator"));
setLayout(commonLayout);
setFixedSize(sizeHint()); //Най - добрата фиксирана позиция за елементите
}
void Calculator::InitOperationButtons()
{
plusButton = new QPushButton(tr("+"), this);
minusButton = new QPushButton(tr("-"), this);
divideButton = new QPushButton(tr("/"), this);
multipleButton = new QPushButton(tr("*"), this);
}
void Calculator::InitDigitButtons()
{
for(int i = 0; i < NUM_KEYS; i++)
{
if(i == 10)
{
numberKeys[i] = new QPushButton(tr("."), this);
continue;
}
if(i == 11)
{
numberKeys[i] = new QPushButton(tr("="), this);
continue;
}
numberKeys[i] = new QPushButton(tr(DigitAsString(i)), this);
//Текста на бутоните ще бъде както следват цифрите
}
}
void Calculator::InitLineEdit()
{
editLine = new QLineEdit;
editLine->setMaxLength(15); //Дължината на числото
}
void Calculator::LayoutButtons()
{
buttonsLayout -> addWidget(plusButton, 0, 0);
buttonsLayout -> addWidget(minusButton, 0, 1);
buttonsLayout -> addWidget(divideButton, 0, 2);
buttonsLayout -> addWidget(multipleButton, 0, 3);
int row = 1;
int col = 0;
for(int i = 0; i < NUM_KEYS; i++, col++)
{
if(i % 4 == 0)
{
col = 0;
buttonsLayout -> addWidget(numberKeys[i], ++row, col);
}else
{
buttonsLayout -> addWidget(numberKeys[i], row, col);
}
}
}
int main(int argc, char* argv[])
{
QApplication qapp(argc, argv);
Calculator* calculator = new Calculator();
calculator -> show();
return qapp.exec();
}
To copy to clipboard, switch view to plain text mode
And the errors:
kotg@kotg-laptop:~/programming/calculator/test$ make
g++ -Wl,--no-undefined -o test test2.o -L/usr/lib -lQtGui -lQtCore -lpthread
test2.o: In function `Calculator::InitDigitButtons()':
test2.cpp:(.text+0x266): undefined reference to `Calculator::staticMetaObject'
test2.cpp:(.text+0x2e0): undefined reference to `Calculator::staticMetaObject'
test2.cpp:(.text+0x357): undefined reference to `Calculator::staticMetaObject'
test2.o: In function `Calculator::InitOperationButtons()':
test2.cpp:(.text+0x473): undefined reference to `Calculator::staticMetaObject'
test2.cpp:(.text+0x4cb): undefined reference to `Calculator::staticMetaObject'
test2.o:test2.cpp:(.text+0x523): more undefined references to `Calculator::staticMetaObject' follow
test2.
o: In function `Calculator
::Calculator(QWidget*)':test2.cpp:(.text+0x6cb): undefined reference to `vtable for Calculator'
test2.cpp:(.text+0x6d2): undefined reference to `vtable for Calculator'
test2.cpp:(.text+0x7a8): undefined reference to `Calculator::staticMetaObject'
test2.
o: In function `Calculator
::Calculator(QWidget*)':test2.cpp:(.text+0x87b): undefined reference to `vtable for Calculator'
test2.cpp:(.text+0x882): undefined reference to `vtable for Calculator'
test2.cpp:(.text+0x958): undefined reference to `Calculator::staticMetaObject'
collect2: ld returned 1 exit status
make: *** [test] Error 1
kotg@kotg-laptop:~/programming/calculator/test$ make
g++ -Wl,--no-undefined -o test test2.o -L/usr/lib -lQtGui -lQtCore -lpthread
test2.o: In function `Calculator::InitDigitButtons()':
test2.cpp:(.text+0x266): undefined reference to `Calculator::staticMetaObject'
test2.cpp:(.text+0x2e0): undefined reference to `Calculator::staticMetaObject'
test2.cpp:(.text+0x357): undefined reference to `Calculator::staticMetaObject'
test2.o: In function `Calculator::InitOperationButtons()':
test2.cpp:(.text+0x473): undefined reference to `Calculator::staticMetaObject'
test2.cpp:(.text+0x4cb): undefined reference to `Calculator::staticMetaObject'
test2.o:test2.cpp:(.text+0x523): more undefined references to `Calculator::staticMetaObject' follow
test2.o: In function `Calculator::Calculator(QWidget*)':
test2.cpp:(.text+0x6cb): undefined reference to `vtable for Calculator'
test2.cpp:(.text+0x6d2): undefined reference to `vtable for Calculator'
test2.cpp:(.text+0x7a8): undefined reference to `Calculator::staticMetaObject'
test2.o: In function `Calculator::Calculator(QWidget*)':
test2.cpp:(.text+0x87b): undefined reference to `vtable for Calculator'
test2.cpp:(.text+0x882): undefined reference to `vtable for Calculator'
test2.cpp:(.text+0x958): undefined reference to `Calculator::staticMetaObject'
collect2: ld returned 1 exit status
make: *** [test] Error 1
To copy to clipboard, switch view to plain text mode
Bookmarks