PDA

View Full Version : Strange Error



keeperofthegrove
17th October 2008, 18:05
Hello, I've been testing around, but I know have problem with this program -- it's giving me strange errors when Q_OBJECT is in the class:
the code
(i'm using Ubuntu, if it matters):

#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();
}
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

keeperofthegrove
17th October 2008, 19:29
Alright, when I comment Q_OBJECT I can't use functional slots(which is right) and when I uncomment it, it shows me the same errors as above!

lyuts
22nd October 2008, 14:57
Show me your makefile.

lyuts
22nd October 2008, 15:01
I think this will be useful
http://doc.trolltech.com/4.4/moc.html#moc

spirit
22nd October 2008, 15:16
offtop
you don't delete widget in this code


...
Calculator* calculator = new Calculator();
calculator -> show();
...


create this widget in stack, i.e.


...
Calculator c;
c.show();
...


or set attribute for this widget Qt::WA_DeleteOnClose (http://doc.trolltech.com/4.4/qt.html#WidgetAttribute-enum)
i.e.


...
Calculator* calculator = new Calculator();
calculator->setAttribute(Qt::WA_DeleteOnClose);
calculator -> show();
...


or delete this widget manually


...
Calculator* calculator = new Calculator();
calculator -> show();
...
bool res = app.exec();
delete calculator;
calculator = 0;
return res;