PDA

View Full Version : Qpushbutton



iamhere
14th October 2008, 10:14
Im trying to create a keypad widget. I am having problems with the button events. I keep getting the following errors:

undefined references to `vtable for Keypad' follow CarPuta keypad.h line 20
undefined reference to `Keypad::staticMetaObject' CarPuta keypad.h line 21
undefined reference to `vtable for Keypad' CarPuta keypad.h line 20
undefined reference to `vtable for Keypad' CarPuta keypad.h line 33

This is my code



#ifndef KEYPAD_H_
#define KEYPAD_H_

#include <QWidget>
#include <QApplication>
#include <QFont>
#include <QPushButton>
#include <QMainWindow>
#include <QLabel>
#include <QScrollArea>
#include <QMouseEvent>
#include <iostream>
#include <X11/Xlib.h>
#include <QLineEdit>
#include <iostream>
#include <QObject>

class Keypad : public QWidget
{
Q_OBJECT
public slots:
void button_1_clicked();
public:
Keypad(QWidget *parent = 0);
};

void Keypad::button_1_clicked()
{
std::cout << "Clicked";
}

Keypad::Keypad(QWidget *parent) : QWidget(parent)
{

//get the screen resolution
Display* dpy = XOpenDisplay(0);
int xscreenres = DisplayWidth(dpy, DefaultScreen(dpy));
int yscreenres = DisplayHeight(dpy, DefaultScreen(dpy));

//the text box
QLineEdit *password = new QLineEdit(this);
password->setGeometry(((xscreenres/2) - 190), ((yscreenres/2) - 270), 380, 80);
password->setAlignment(Qt::AlignHCenter);
password->setMaxLength(8);
password->setEnabled(false);
password->setEchoMode(QLineEdit::Password);

QPushButton *Button_1 = new QPushButton(tr("1"), this);
Button_1->setGeometry((xscreenres/2) - 190, (yscreenres/2) - 125, 120, 120);
Button_1->setFont(QFont("Times", 40, QFont::Bold));
connect(Button_1, SIGNAL(clicked()), this, SLOT(button_1_clicked()));

QPushButton *Button_2 = new QPushButton(tr("2"), this);
Button_2->setGeometry((xscreenres/2) - 60, (yscreenres/2) - 125, 120, 120);
Button_2->setFont(QFont("Times", 40, QFont::Bold));

QPushButton *Button_3 = new QPushButton(tr("3"), this);
Button_3->setGeometry((xscreenres/2) + 70, (yscreenres/2) - 125, 120, 120);
Button_3->setFont(QFont("Times", 40, QFont::Bold));

QPushButton *Button_4 = new QPushButton(tr("4"), this);
Button_4->setGeometry((xscreenres/2) - 190, (yscreenres/2) + 5, 120, 120);
Button_4->setFont(QFont("Times", 40, QFont::Bold));

QPushButton *Button_5 = new QPushButton(tr("5"), this);
Button_5->setGeometry((xscreenres/2) - 60, (yscreenres/2) + 5, 120, 120);
Button_5->setFont(QFont("Times", 40, QFont::Bold));

QPushButton *Button_6 = new QPushButton(tr("6"), this);
Button_6->setGeometry((xscreenres/2) + 70, (yscreenres/2) + 5, 120, 120);
Button_6->setFont(QFont("Times", 40, QFont::Bold));

QPushButton *Button_7 = new QPushButton(tr("7"), this);
Button_7->setGeometry((xscreenres/2) - 190, (yscreenres/2) + 135, 120, 120);
Button_7->setFont(QFont("Times", 40, QFont::Bold));

QPushButton *Button_8 = new QPushButton(tr("8"), this);
Button_8->setGeometry((xscreenres/2) - 60, (yscreenres/2) + 135, 120, 120);
Button_8->setFont(QFont("Times", 40, QFont::Bold));

QPushButton *Button_9 = new QPushButton(tr("9"), this);
Button_9->setGeometry((xscreenres/2) + 70, (yscreenres/2) + 135, 120, 120);
Button_9->setFont(QFont("Times", 40, QFont::Bold));

QPushButton *Button_10 = new QPushButton(tr("Clear"), this);
Button_10->setGeometry((xscreenres/2) - 190, (yscreenres/2) + 265, 120, 120);
Button_10->setFont(QFont("Times", 30, QFont::Bold));

QPushButton *Button_11 = new QPushButton(tr("0"), this);
Button_11->setGeometry((xscreenres/2) - 60, (yscreenres/2) + 265, 120, 120);
Button_11->setFont(QFont("Times", 40, QFont::Bold));

QPushButton *Button_12 = new QPushButton(tr("Enter"), this);
Button_12->setGeometry((xscreenres/2) + 70, (yscreenres/2) + 265, 120, 120);
Button_12->setFont(QFont("Times", 30, QFont::Bold));

//connect(Button_1, SIGNAL(clicked()), qApp, SLOT(quit()));
}

#endif /*KEYPAD_H_*/

caduel
14th October 2008, 11:02
make sure that .h file is listed in the HEADERS section of your .pro file.
run qmake.
(If moc is not run for that .h file, no code will be generated for your slots etc and you get this error.)

HTH

iamhere
14th October 2008, 11:35
Im now getting different error messages:

first defined here CarPuta keypad.h line 28
first defined here CarPuta keypad.h line 33
multiple definition of `Keypad::button_1_clicked()' CarPuta keypad.h line 28
multiple definition of `Keypad::Keypad(QWidget*)' CarPuta keypad.h line 33

montylee
14th October 2008, 14:21
Try to keep your class declaration and code separate. Ideally you should give the class declaration in the header file and definition of class and all member functions in the CPP file. Also check your .pro file as @caduel said.

Suppose you have temp.h and temp.cpp for the above code and you want to run the code. Use the following commands:


1) First create the project file:
$ qmake-qt4 -project
2) Create the Makfile
$ qmake-qt4
3) Compile the code
$ make

These commands are for Qt 4. For Qt 3, replace qmake-qt4 with qmake.

Regarding the error you are getting, make sure you haven't defined the constructor and member functions in both header as well as CPP file.

iamhere
15th October 2008, 01:31
When working on a class I tend to do it all in the header initially to save switching between files. Thanks guys Its all working now

montylee
15th October 2008, 05:40
nice to know it's working :) Enjoy your coding.