Results 1 to 6 of 6

Thread: Qpushbutton

  1. #1
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Qpushbutton

    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

    Qt Code:
    1. #ifndef KEYPAD_H_
    2. #define KEYPAD_H_
    3.  
    4. #include <QWidget>
    5. #include <QApplication>
    6. #include <QFont>
    7. #include <QPushButton>
    8. #include <QMainWindow>
    9. #include <QLabel>
    10. #include <QScrollArea>
    11. #include <QMouseEvent>
    12. #include <iostream>
    13. #include <X11/Xlib.h>
    14. #include <QLineEdit>
    15. #include <iostream>
    16. #include <QObject>
    17.  
    18. class Keypad : public QWidget
    19. {
    20. Q_OBJECT
    21. public slots:
    22. void button_1_clicked();
    23. public:
    24. Keypad(QWidget *parent = 0);
    25. };
    26.  
    27. void Keypad::button_1_clicked()
    28. {
    29. std::cout << "Clicked";
    30. }
    31.  
    32. Keypad::Keypad(QWidget *parent) : QWidget(parent)
    33. {
    34.  
    35. //get the screen resolution
    36. Display* dpy = XOpenDisplay(0);
    37. int xscreenres = DisplayWidth(dpy, DefaultScreen(dpy));
    38. int yscreenres = DisplayHeight(dpy, DefaultScreen(dpy));
    39.  
    40. //the text box
    41. QLineEdit *password = new QLineEdit(this);
    42. password->setGeometry(((xscreenres/2) - 190), ((yscreenres/2) - 270), 380, 80);
    43. password->setAlignment(Qt::AlignHCenter);
    44. password->setMaxLength(8);
    45. password->setEnabled(false);
    46. password->setEchoMode(QLineEdit::Password);
    47.  
    48. QPushButton *Button_1 = new QPushButton(tr("1"), this);
    49. Button_1->setGeometry((xscreenres/2) - 190, (yscreenres/2) - 125, 120, 120);
    50. Button_1->setFont(QFont("Times", 40, QFont::Bold));
    51. connect(Button_1, SIGNAL(clicked()), this, SLOT(button_1_clicked()));
    52.  
    53. QPushButton *Button_2 = new QPushButton(tr("2"), this);
    54. Button_2->setGeometry((xscreenres/2) - 60, (yscreenres/2) - 125, 120, 120);
    55. Button_2->setFont(QFont("Times", 40, QFont::Bold));
    56.  
    57. QPushButton *Button_3 = new QPushButton(tr("3"), this);
    58. Button_3->setGeometry((xscreenres/2) + 70, (yscreenres/2) - 125, 120, 120);
    59. Button_3->setFont(QFont("Times", 40, QFont::Bold));
    60.  
    61. QPushButton *Button_4 = new QPushButton(tr("4"), this);
    62. Button_4->setGeometry((xscreenres/2) - 190, (yscreenres/2) + 5, 120, 120);
    63. Button_4->setFont(QFont("Times", 40, QFont::Bold));
    64.  
    65. QPushButton *Button_5 = new QPushButton(tr("5"), this);
    66. Button_5->setGeometry((xscreenres/2) - 60, (yscreenres/2) + 5, 120, 120);
    67. Button_5->setFont(QFont("Times", 40, QFont::Bold));
    68.  
    69. QPushButton *Button_6 = new QPushButton(tr("6"), this);
    70. Button_6->setGeometry((xscreenres/2) + 70, (yscreenres/2) + 5, 120, 120);
    71. Button_6->setFont(QFont("Times", 40, QFont::Bold));
    72.  
    73. QPushButton *Button_7 = new QPushButton(tr("7"), this);
    74. Button_7->setGeometry((xscreenres/2) - 190, (yscreenres/2) + 135, 120, 120);
    75. Button_7->setFont(QFont("Times", 40, QFont::Bold));
    76.  
    77. QPushButton *Button_8 = new QPushButton(tr("8"), this);
    78. Button_8->setGeometry((xscreenres/2) - 60, (yscreenres/2) + 135, 120, 120);
    79. Button_8->setFont(QFont("Times", 40, QFont::Bold));
    80.  
    81. QPushButton *Button_9 = new QPushButton(tr("9"), this);
    82. Button_9->setGeometry((xscreenres/2) + 70, (yscreenres/2) + 135, 120, 120);
    83. Button_9->setFont(QFont("Times", 40, QFont::Bold));
    84.  
    85. QPushButton *Button_10 = new QPushButton(tr("Clear"), this);
    86. Button_10->setGeometry((xscreenres/2) - 190, (yscreenres/2) + 265, 120, 120);
    87. Button_10->setFont(QFont("Times", 30, QFont::Bold));
    88.  
    89. QPushButton *Button_11 = new QPushButton(tr("0"), this);
    90. Button_11->setGeometry((xscreenres/2) - 60, (yscreenres/2) + 265, 120, 120);
    91. Button_11->setFont(QFont("Times", 40, QFont::Bold));
    92.  
    93. QPushButton *Button_12 = new QPushButton(tr("Enter"), this);
    94. Button_12->setGeometry((xscreenres/2) + 70, (yscreenres/2) + 265, 120, 120);
    95. Button_12->setFont(QFont("Times", 30, QFont::Bold));
    96.  
    97. //connect(Button_1, SIGNAL(clicked()), qApp, SLOT(quit()));
    98. }
    99.  
    100. #endif /*KEYPAD_H_*/
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qpushbutton

    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

  3. The following user says thank you to caduel for this useful post:

    iamhere (15th October 2008)

  4. #3
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qpushbutton

    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

  5. #4
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qpushbutton

    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.

  6. The following user says thank you to montylee for this useful post:

    iamhere (15th October 2008)

  7. #5
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qpushbutton

    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

  8. #6
    Join Date
    Oct 2007
    Location
    India
    Posts
    162
    Thanks
    20
    Thanked 6 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Qpushbutton

    nice to know it's working Enjoy your coding.

Similar Threads

  1. Adding Image to QPushButton
    By Seema Rao in forum Qt Programming
    Replies: 3
    Last Post: 18th November 2011, 09:06
  2. QPushButton not calling repaint when needed?
    By Enygma in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2010, 17:03
  3. Changing QPushButton text colour with mouseMoveEvent
    By Misenko in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2008, 16:53
  4. Replies: 3
    Last Post: 26th September 2006, 12:16
  5. QT4 layout of complex dialog is very slow
    By cboles in forum Qt Programming
    Replies: 15
    Last Post: 28th April 2006, 19:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.