Page 1 of 2 12 LastLast
Results 1 to 20 of 31

Thread: Q_OBJECT, vtables and basic GUI control.

  1. #1
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Question Q_OBJECT, vtables and basic GUI control.

    Hello QT Geeks!

    I have to create GUI for my application and after re-search I've had to choose between wxWidgets and QT, and my choice was QT.
    I've downloaded newest QT Version from http://qt.nokia.com (i.e. 4.6.3), MinGW 5.1.6 and Code::Blocks (Also latest version.) and I'm working on Windows XP Professional Service Pack 3.
    I've configured my Code::Blocks using some wiki tutorial (Can't remember where did I find the link so can't post :/).

    Basically I need 2 "windows" in 2 classes:
    a) Login Window with 2 static texts, 2 text boxes and 3 push buttons.
    b) Client window with few tabs, buttons and such.

    Here's my code:

    main.cpp:
    Qt Code:
    1. #include <QApplication>
    2. #include "LoginInterface.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. LoginInterface li;
    9. li.show();
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    LoginInterface.cpp:
    Qt Code:
    1. #include <QLabel>
    2.  
    3. #include "LoginInterface.h"
    4.  
    5. LoginInterface::LoginInterface()
    6. {
    7. QLabel *NameDialog = new QLabel(tr("Name:"));
    8. NameDialog->show();
    9. }
    To copy to clipboard, switch view to plain text mode 

    LoginInterface.h:
    Qt Code:
    1. #ifndef LOGININTERFACE_H
    2. #define LOGININTERFACE_H
    3.  
    4. #include <QMainWindow>
    5.  
    6. class LoginInterface : public QMainWindow
    7. {
    8. Q_OBJECT
    9. public:
    10. LoginInterface();
    11. };
    12.  
    13. #endif
    To copy to clipboard, switch view to plain text mode 

    As you can see in LoginInterface header file, there's Q_OBJECT variable, but with this variable I'm getting following errors:
    Qt Code:
    1. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x66): undefined reference to `LoginInterface::staticMetaObject'
    2. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x71): undefined reference to `vtable for LoginInterface'
    3. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x78): undefined reference to `vtable for LoginInterface'
    4. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x1f8): undefined reference to `vtable for LoginInterface'
    5. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x1ff): undefined reference to `vtable for LoginInterface'
    6. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x213): undefined reference to `LoginInterface::staticMetaObject'
    To copy to clipboard, switch view to plain text mode 

    But when I remove that (Yea I know, ugly cheat. :p), everything compiles fine, but GUI comes with separated windows, like on this screen:


    Is there any way to fix Q_OBJECT thing and fix windows, so new controls will be CHILDS, not separated windows?

    Also I've tried other ready code and C::B threw me error about 'Undefined QThread' or something, do I miss some parts of QT or?

    Cheers, Diath.
    PS: I hope you'll understand my english, it's kinda bad but i tried to write as proper as I can.

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Q_OBJECT, vtables and basic GUI control.

    There are a couple of things not completely correct.

    The following code is ok:
    Qt Code:
    1. #include <QApplication>
    2. #include "LoginInterface.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7.  
    8. LoginInterface li;
    9. li.show();
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    The LoginInterface class isn't.
    Try this:
    Qt Code:
    1. #ifndef LOGININTERFACE_H
    2. #define LOGININTERFACE_H
    3.  
    4. #include <QDialog>
    5.  
    6. class LoginInterface : public QDialog
    7. {
    8. Q_OBJECT
    9.  
    10. public:
    11. LoginInterface(QWidget *parent = 0, Qt::WindowFlags flags = 0 );
    12. };
    13.  
    14. #endif
    To copy to clipboard, switch view to plain text mode 

    And for the implementation try this:

    Qt Code:
    1. #include <QLabel>
    2. #include <QHBoxLayout>
    3.  
    4. #include "LoginInterface.h"
    5.  
    6. LoginInterface::LoginInterface(QWidget *parent = 0, Qt::WindowFlags flags = 0) :
    7. QDialog(parent, flags)
    8. {
    9. QHBoxLayout *labelLayout = new QHBoxLayout;
    10.  
    11. QLabel *inputLabel = new QLabel(tr("Name:"));
    12. QLineEdit *inputText = new QLineEdit;
    13.  
    14. labelLayout->addWidget(inputLabel);
    15. labelLayout->addWidget(inputText);
    16.  
    17. setLayout(labelLayout);
    18. }
    To copy to clipboard, switch view to plain text mode 

    If you create a widget within the code of another widget, and you don't set the parent of the new widget and then you call show(), you do show this new widget in a new window.

    Use layouts. You don't have to explicitly set parents for child widgets as the layout reparents them and setLayout will make the parent widget take control of it.

    Edit: and try to clean your project and rebuild it completely.

  3. #3
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: Q_OBJECT, vtables and basic GUI control.

    This is where you need to pass the parent pointer (in your case the "this" pointer):
    Qt Code:
    1. QLabel *NameDialog = new QLabel(tr("Name:"), this);
    To copy to clipboard, switch view to plain text mode 
    LE: to late, and incomplete
    Last edited by Zlatomir; 29th July 2010 at 18:34.

  4. The following user says thank you to Zlatomir for this useful post:

    Diath (30th July 2010)

  5. #4
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Thank you both guys for fast answers.

    @tbscope:
    I did try your code, and did as you wrote in your edit, but C::B threw me new errors:
    Qt Code:
    1. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.h:14:7: warning: no newline at end of file
    2. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.cpp:6: error: default argument given for parameter 1 of `LoginInterface::LoginInterface(QWidget*, Qt::WindowFlags)'
    3. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.h:11: error: after previous specification in `LoginInterface::LoginInterface(QWidget*, Qt::WindowFlags)'
    4. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.cpp:6: error: default argument given for parameter 2 of `LoginInterface::LoginInterface(QWidget*, Qt::WindowFlags)'
    5. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.h:11: error: after previous specification in `LoginInterface::LoginInterface(QWidget*, Qt::WindowFlags)'
    6. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.cpp: In constructor `LoginInterface::LoginInterface(QWidget*, Qt::WindowFlags)':
    7. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.cpp:12: error: `QLineEdit' was not declared in this scope
    8. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.cpp:12: error: `inputText' was not declared in this scope
    9. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.cpp:12: error: `QLineEdit' is not a type
    10. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.cpp:12: warning: unused variable 'QLineEdit'
    To copy to clipboard, switch view to plain text mode 

    So I did add following code under #include <QLabel>:
    Qt Code:
    1. #include <QLineEdit>
    To copy to clipboard, switch view to plain text mode 

    Now I'm getting only these errors (I assume they're about LoginInterface constructor.):
    Qt Code:
    1. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.cpp:7: error: default argument given for parameter 1 of `LoginInterface::LoginInterface(QWidget*, Qt::WindowFlags)'
    2. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.h:11: error: after previous specification in `LoginInterface::LoginInterface(QWidget*, Qt::WindowFlags)'
    3. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.cpp:7: error: default argument given for parameter 2 of `LoginInterface::LoginInterface(QWidget*, Qt::WindowFlags)'
    4. C:\Documents and Settings\Kamil\Pulpit\The Bloody War\client_launcher\LoginInterface.h:11: error: after previous specification in `LoginInterface::LoginInterface(QWidget*, Qt::WindowFlags)'
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Yes, sorry, I posted it too quickly.
    Indeed, add the qlineedit include
    But also:

    Qt Code:
    1. #include <QLabel>
    2. #include <QHBoxLayout>
    3.  
    4. #include "LoginInterface.h"
    5.  
    6. LoginInterface::LoginInterface(QWidget *parent, Qt::WindowFlags flags) :
    7. QDialog(parent, flags)
    8. {
    9. QHBoxLayout *labelLayout = new QHBoxLayout;
    10.  
    11. QLabel *inputLabel = new QLabel(tr("Name:"));
    12. QLineEdit *inputText = new QLineEdit;
    13.  
    14. labelLayout->addWidget(inputLabel);
    15. labelLayout->addWidget(inputText);
    16.  
    17. setLayout(labelLayout);
    18. }
    To copy to clipboard, switch view to plain text mode 

    Remove the two = 0 as in the code above. It's not allowed to add default values in the implementation.

  7. The following user says thank you to tbscope for this useful post:

    Diath (30th July 2010)

  8. #6
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Well, I've removed = 0 in both .cpp and .h files, then I moved to main.cpp and my code looks now like this:
    Qt Code:
    1. #include <QApplication>
    2. #include "LoginInterface.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. QWidget *MainWidget = new QWidget();
    8. LoginInterface *li = new LoginInterface(MainWidget, Qt::Dialog);
    9. li->show();
    10.  
    11. return app.exec();
    12. }
    To copy to clipboard, switch view to plain text mode 

    But again, C::B throws me an errors:
    Qt Code:
    1. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x6a): undefined reference to `vtable for LoginInterface'
    2. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x71): undefined reference to `vtable for LoginInterface'
    3. obj\LoginInterface.o:LoginInterface.cpp:(.text+0xbe): undefined reference to `LoginInterface::staticMetaObject'
    4. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x31a): undefined reference to `vtable for LoginInterface'
    5. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x321): undefined reference to `vtable for LoginInterface'
    6. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x36e): undefined reference to `LoginInterface::staticMetaObject'
    To copy to clipboard, switch view to plain text mode 

    I guess it's me who's doing something wrong, I really appreciate your help .

  9. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Leave the =0 in header file

  10. #8
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Still:
    Qt Code:
    1. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x6a): undefined reference to `vtable for LoginInterface'
    2. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x71): undefined reference to `vtable for LoginInterface'
    3. obj\LoginInterface.o:LoginInterface.cpp:(.text+0xbe): undefined reference to `LoginInterface::staticMetaObject'
    4. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x31a): undefined reference to `vtable for LoginInterface'
    5. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x321): undefined reference to `vtable for LoginInterface'
    6. obj\LoginInterface.o:LoginInterface.cpp:(.text+0x36e): undefined reference to `LoginInterface::staticMetaObject'
    To copy to clipboard, switch view to plain text mode 

    And yes, I did "clean and rebuild" project.

    I have to leave for the moment, will take a look here later. I hope someone will help .

  11. #9
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Here you have the little project that worksTestTest.zip
    LE: exact code that tbscope provided (incuding the two little corrections)
    Last edited by Zlatomir; 29th July 2010 at 19:30.

  12. #10
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Does your setup find the meta object compiler?

  13. #11
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Quote Originally Posted by Zlatomir View Post
    Here you have the little project that worksTestTest.zip
    LE: exact code that tbscope provided (incuding the two little corrections)
    Seems like I've installed QT wrong, still throws me an error.

    Quote Originally Posted by tbscope View Post
    Does your setup find the meta object compiler?
    How do I check/fix? (Sorry for dumb questions.)

  14. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Q_OBJECT, vtables and basic GUI control.

    How does Code::Blocks manage the steps needed to build a Qt program? Is Code::Blocks running your source through Qt moc (look for LoginInterface_moc.cpp) ? Is there a qmake PRO file?

    If you have no pressing reason to stick with Code::Blocks then you should consider Qt Creator, which manages many Qt aspects for you.

  15. #13
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Quote Originally Posted by ChrisW67 View Post
    How does Code::Blocks manage the steps needed to build a Qt program? Is Code::Blocks running your source through Qt moc (look for LoginInterface_moc.cpp) ? Is there a qmake PRO file?

    If you have no pressing reason to stick with Code::Blocks then you should consider Qt Creator, which manages many Qt aspects for you.
    There's no moc or pro file.

    And I'm using C::B only to press damn F9 button, so I can switch to Qt Creator, but what then? Will I have to integrate MinGW or something with it? Is there any guide how to setup it properly?
    Last edited by Diath; 30th July 2010 at 11:02.

  16. #14
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    62
    Thanked 260 Times in 246 Posts

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Qt SDK for Windows has everything ready to run, and the one for Linux need to have g++ and make installed on your system.

  17. #15
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Q_OBJECT, vtables and basic GUI control.

    If you download the Qt SDK, it includes everything. Just install it, open Qt Creator and build without problems.

  18. #16
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.


  19. #17
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Okay, I don't want to create new thread, so gonna post here.
    I have button in LoginInterface class, and after passing click signal to that button I call following function:
    Qt Code:
    1. void LoginInterface::Run()
    2. {
    3. // Totalny rozpierdol!
    4. delete UserLabel;
    5. delete UserInput;
    6. delete MainLayout;
    7.  
    8. // Basic Tab
    9. BasicWidget = new QWidget;
    10. BasicLayout = new QGridLayout;
    11.  
    12. // // Basic Tab - Controls
    13. UsernameLabel = new QLabel("User:");
    14. UsernameEdit = new QLineEdit;
    15. BasicWidget->setLayout(BasicLayout);
    16.  
    17. // // Basic Tab - Adding widgets
    18. BasicLayout->addWidget(UsernameLabel);
    19. BasicLayout->addWidget(UsernameEdit);
    20.  
    21. // Tabs
    22. Tabs = new QTabWidget;
    23. Tabs->addTab(BasicWidget, tr("Basic Info"));
    24. Tabs->show();
    25. }
    To copy to clipboard, switch view to plain text mode 

    But tabs comes in separated window, how do I pass parent? ;p

  20. #18
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Add the Tabs widget to a layout inside LoginInterface's central widget, or the layout of the window you want the widget to appear in, rather than "Tabs->show()". Exactly how you do that depends on exactly how things are arranged now, and what you want to happen.

  21. #19
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Oh, lol. Thank you, solution:
    Qt Code:
    1. MainLayout->addWidget(Tabs);
    To copy to clipboard, switch view to plain text mode 

    + removing:
    Qt Code:
    1. delete MainLayout;
    To copy to clipboard, switch view to plain text mode 

  22. #20
    Join Date
    Jul 2010
    Posts
    30
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    6
    Thanked 1 Time in 1 Post

    Default Re: Q_OBJECT, vtables and basic GUI control.

    Seems like bad solution, it doesn't fit window (It's like container inside other.), also I haven't "hook" with main window or something so I can't resize it. Any tips?

Similar Threads

  1. Replies: 0
    Last Post: 16th December 2009, 09:45
  2. why we need Q_OBJECT???
    By phillip_Qt in forum Qt Programming
    Replies: 9
    Last Post: 26th March 2009, 07:49
  3. When do I need Q_OBJECT?
    By Morea in forum Newbie
    Replies: 1
    Last Post: 24th February 2006, 08:15
  4. Q_object
    By Mariane in forum Newbie
    Replies: 5
    Last Post: 7th February 2006, 17:39

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
  •  
Qt is a trademark of The Qt Company.