Results 1 to 15 of 15

Thread: Creating slots with Q_OBJECT

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2007
    Posts
    209
    Thanks
    34
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Creating slots with Q_OBJECT

    Hey all,
    I have been using DevC++ Windows QT 4.2.2 to make a class that has my own slot which i can use to make my own event handlers. However, everytime i include Q_OBJECT macro in the class, it creates linker errors such as vtable bla bla, and also I have read while searching these forums some information about moc, which i don't understand at all. Something about including mocs? Well anyway I use a template for DevC++ that someone had made before, i'll include that so you can see whether this moc stuff was made already or maybe you can edit it so that i won't have to do anything else dealing with moc.

    [Template]
    ver=1
    Name=Qt
    Icon=Qt.ico
    Description=Create a GUI using the Qt library.
    Catagory=GUI

    [Unit0]
    CppName=main.cpp
    Cpp=qt_cpp.txt

    [Project]
    UnitCount=1
    Type=1
    IsCpp=1
    CppCompiler=-O2 -O2 -frtti -fexceptions -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_NO_DEBUG -DQT_CORE_LIB -DQT_GUI_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"C:/Qt/4.2.2/include/QtGui" -I"C:/Qt/4.2.2/include/QtCore" -I"C:/Qt/4.2.2/include" -I"." -I"C:/Qt/4.2.2/include/ActiveQt" -I"tmp\moc\release_shared" -I"." -I"C:\Qt\4.2.2\mkspecs\win32-g++"
    Linker=-mthreads -Wl,-enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-s -Wl,-s -Wl,-subsystem,windows -L"C:\Qt\4.2.2\lib" -L"C:\Qt\4.2.2\lib" -lmingw32 -lqtmain -lQtCore4 -lQtGui4
    ProjectIcon=Qt.ico
    I'm very new to Qt, but so far I like it better than wxWidgets, except for the event handling since compiling this stuff with connect() and slots/signals has taken many hours to figure out and i still can't get it to work. To get Qt ready, i just downloaded the latest exe file and installed it to my system.

    Here's my code example:
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public slots:
    5. void TestSlot();
    6. public:
    7. MyWidget(QWidget *parent = 0);
    8.  
    9. };
    10. void MyWidget::TestSlot(){
    11. Box->show();
    12. }
    13. MyWidget::MyWidget(QWidget *parent)
    14. : QWidget(parent)
    15. {
    16. QPushButton *quit = new QPushButton(tr("Quit"));
    17. quit->setFont(QFont("Times", 18, QFont::Bold));
    18. QPushButton *Message = new QPushButton(tr("Message"));
    19. Message->setFont(QFont("Times", 18, QFont::Bold));
    20. Box = new QMessageBox(QMessageBox::Information, tr("Hey"), tr("Hi director"),
    21. QMessageBox::Ok, this, Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint );
    22. connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
    23. connect(Message, SIGNAL(clicked()), this, SLOT(TestSlot()));
    24. QVBoxLayout *layout = new QVBoxLayout;
    25. layout->addWidget(quit);
    26. layout->addWidget(Message);
    27. setLayout(layout);
    28. }
    29. int main(int argc, char *argv[])
    30. {
    31. QApplication app(argc, argv);
    32. MyWidget widget;
    33. widget.show();
    34. widget.setToolTip("A nice little tooltip");
    35. QTabWidget* Tab = new QTabWidget();
    36. Tab->addTab(&widget, "A Tab");
    37. Tab->setToolTip("This is a Tab!");
    38. Tab->show();
    39. return app.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 
    Don't worry about the includes, i just didn't write it down here... but they exist.
    And yes, I know that I could have attached the slot to the Box itself (messagebox), and then used the public slot "show()"... but I want to test custom slots first.

    Thanks for your help + time.
    EDIT: Oh one more thing.
    When I do compile my programs, the program will not run unless mingwm10.dll QtCore4.dll and QtGui4.dll are in the same folder. I thought that with the template it would compile with the libraries and DLLs would not be required, is there a way to edit the DevC++ template to not use DLLs and to compile them all into the file?
    Last edited by jacek; 13th January 2007 at 23:26. Reason: changed [code] to [quote]

Similar Threads

  1. Signals and Slots question
    By Thoosle in forum Qt Programming
    Replies: 5
    Last Post: 5th December 2006, 00:24
  2. Adding slots in Designer
    By jamos in forum Qt Tools
    Replies: 5
    Last Post: 18th May 2006, 23:28
  3. Problem in creating thread in GUI application
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:05
  4. Problem with Signal and Slots
    By Kapil in forum Installation and Deployment
    Replies: 2
    Last Post: 10th February 2006, 08:51
  5. Missing slots
    By Mariane in forum Newbie
    Replies: 1
    Last Post: 5th February 2006, 01:50

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.