Results 1 to 13 of 13

Thread: Problem When Creating my own Slot

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem When Creating my own Slot

    Uncomment Q_OBJECT and re-run qmake.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem When Creating my own Slot

    You should:
    • not declare classes with Q_OBJECT macro in a .cpp file but
    • place the class declaration in a header file, say CustomSlot.h
    • add the header file to the .pro file's HEADERS variable
    • continue with what Jacek said

    A workaround is to include the moc file in the end of CustomSlot.cpp.
    J-P Nurmi

  3. #3
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Problem When Creating my own Slot

    Thanks fnmblot as well as Thanks again jpn .
    It seems that I'm that kind of annoying students who won't stop questioning

    I've made what you 've told me & separated the declaration from the implementation .
    However , my slot didn't work yet ?!!
    + How does the header file which generated ,ui_Test.h , can call my CustomSlot ?!!

    I've changed a little in my App :
    Here are my Codes :

    => Main.cpp 's code is :

    Qt Code:
    1. #include <QApplication>
    2. #include <QDialog>
    3.  
    4. #include "ui_Test.h"
    5.  
    6. int main(int argc, char *argv[])
    7. {
    8. QApplication app(argc, argv);
    9.  
    10.  
    11.  
    12. QWidget *widget = new QWidget;
    13. Ui_MyForm ui;
    14. ui.setupUi(widget);
    15.  
    16. widget->show();
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 

    ---------------------------------------------------------------------------------------------------------------


    ui_Test.h 's code is :

    Qt Code:
    1. /********************************************************************************
    2. ** Form generated from reading ui file 'Test.ui'
    3. **
    4. ** Created: Thu Jun 5 19:58:05 2008
    5. ** by: Qt User Interface Compiler version 4.4.0
    6. **
    7. ** WARNING! All changes made in this file will be lost when recompiling ui file!
    8. ********************************************************************************/
    9.  
    10. #ifndef UI_TEST_H
    11. #define UI_TEST_H
    12.  
    13. #include <QtCore/QVariant>
    14. #include <QtGui/QAction>
    15. #include <QtGui/QApplication>
    16. #include <QtGui/QButtonGroup>
    17. #include <QtGui/QLineEdit>
    18. #include <QtGui/QPushButton>
    19. #include <QtGui/QWidget>
    20.  
    21. QT_BEGIN_NAMESPACE
    22.  
    23. class Ui_MyForm
    24. {
    25. public:
    26. QPushButton *btnCreateFile;
    27. QLineEdit *txtData;
    28.  
    29. void setupUi(QWidget *MyForm)
    30. {
    31. if (MyForm->objectName().isEmpty())
    32. MyForm->setObjectName(QString::fromUtf8("MyForm"));
    33. MyForm->resize(533, 393);
    34. btnCreateFile = new QPushButton(MyForm);
    35. btnCreateFile->setObjectName(QString::fromUtf8("btnCreateFile"));
    36. btnCreateFile->setGeometry(QRect(170, 190, 75, 24));
    37. txtData = new QLineEdit(MyForm);
    38. txtData->setObjectName(QString::fromUtf8("txtData"));
    39. txtData->setGeometry(QRect(160, 90, 113, 20));
    40.  
    41. retranslateUi(MyForm);
    42. QObject::connect(btnCreateFile, SIGNAL(pressed()), MyForm, SLOT(CreateFile()));
    43.  
    44. QMetaObject::connectSlotsByName(MyForm);
    45. } // setupUi
    46.  
    47. void retranslateUi(QWidget *MyForm)
    48. {
    49. MyForm->setWindowTitle(QApplication::translate("MyForm", "Form", 0, QApplication::UnicodeUTF8));
    50. btnCreateFile->setText(QApplication::translate("MyForm", "PushButton", 0, QApplication::UnicodeUTF8));
    51. txtData->setText(QString());
    52. Q_UNUSED(MyForm);
    53. } // retranslateUi
    54.  
    55. };
    56.  
    57. namespace Ui {
    58. class MyForm: public Ui_MyForm {};
    59. } // namespace Ui
    60.  
    61. QT_END_NAMESPACE
    62.  
    63. #endif // UI_TEST_H
    To copy to clipboard, switch view to plain text mode 
    ---------------------------------------------------------------------------------------------------------------

    => CustomSlot.h 's code is :

    Qt Code:
    1. #ifndef CustmSlot_Declaration_H
    2. #define CustmSlot_Declaration_H
    3. #include "ui_Test.h"
    4.  
    5. class CustomSlot: public QWidget
    6. {
    7. Q_OBJECT
    8.  
    9. public:
    10. CustomSlot(QWidget *parent = 0);
    11.  
    12. private slots:
    13. void CreateFile();
    14.  
    15.  
    16. private:
    17. Ui_MyForm ui;
    18. };
    19. #endif
    To copy to clipboard, switch view to plain text mode 

    ---------------------------------------------------------------------------------------------------------------

    => CustomSlot.cpp 's code is :

    Qt Code:
    1. #include"CustomSlot.h"
    2. #include "ui_Test.h"
    3. #include <stdio.h>
    4.  
    5.  
    6.  
    7.  
    8. CustomSlot::CustomSlot(QWidget *parent)
    9. : QWidget(parent)
    10. {
    11. ui.setupUi(this);
    12. }
    13.  
    14. void CustomSlot:: CreateFile()
    15. {
    16. FILE * pFile;
    17. pFile = fopen ("C:/myfile.txt","w");
    18. }
    To copy to clipboard, switch view to plain text mode 

    Thanks

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem When Creating my own Slot

    Quote Originally Posted by Fatla View Post
    Qt Code:
    1. QWidget *widget = new QWidget;
    2. Ui_MyForm ui;
    3. ui.setupUi(widget);
    4.  
    5. widget->show();
    To copy to clipboard, switch view to plain text mode 
    No instance of CustomSlot is created anywhere. You instantiate a plain QWidget and use the non-recommended "direct approach". It should be:
    Qt Code:
    1. CustomSlot widget;
    2. widget.show();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem When Creating my own Slot

    Plus you need to have a

    connect(ui->btnCreateFile, SIGNAL(pressed()), this, SLOT(CreateFile()));

    line in the constructor of your CustomSlot class.

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Problem When Creating my own Slot

    Quote Originally Posted by vycke View Post
    Plus you need to have a

    connect(ui->btnCreateFile, SIGNAL(pressed()), this, SLOT(CreateFile()));

    line in the constructor of your CustomSlot class.
    Actually that's already done in the generated code. Qt Designer 4.4.0 introduced a way to define custom slots:
    * [132874] Added support for user-defined signals and slots of promoted widgets and main container
    J-P Nurmi

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

    Fatla (6th June 2008)

  8. #7
    Join Date
    Jun 2008
    Posts
    35
    Thanks
    5
    Thanked 3 Times in 2 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Talking Re: Problem When Creating my own Slot

    Hooooooooray , it's Working Now

    Thanks vycke .
    Thanks SooOoOo Much jpn . I owe you a lot .

  9. #8
    Join Date
    Dec 2007
    Location
    Austin, TX
    Posts
    43
    Thanks
    12
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem When Creating my own Slot

    Quote Originally Posted by jpn View Post
    Actually that's already done in the generated code. Qt Designer 4.4.0 introduced a way to define custom slots:

    Heh.. missed that in the .ui code -- didn't really look hard though.

    Vycke

Similar Threads

  1. problem with slot
    By as001622 in forum Qt Programming
    Replies: 4
    Last Post: 24th May 2008, 08:02
  2. problem creating dom tree
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2008, 08:12
  3. Problem with slot
    By beerkg in forum Qt Programming
    Replies: 29
    Last Post: 3rd April 2007, 19:54
  4. creating dll problem -> no exports ...
    By vrudolf in forum Qt Programming
    Replies: 2
    Last Post: 30th July 2006, 16:47
  5. Problem in creating thread in GUI application
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 12:05

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.