Results 1 to 13 of 13

Thread: Problem When Creating my own Slot

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

    Default Problem When Creating my own Slot

    I'm creating my first app with my own slot .
    Everything goes sooo fine till now . However when I want to create my own slot , nothing is happened (i.e. slot didn't work !! )

    The purpose of my App : when the user press a button , an empty .txt file 'll be created .

    I'm working under MS Win XP , & I've tried to run it over Linux to find out the problem . I've such that output :

    Object::connect: No such slot QDialog::Custom()
    Object::connect: (sender name: 'pushButton')
    Object::connect: (receiver name: 'Form')

    Though , I've add such this slot "Custom" @ QTDesign .

    I've got .exe BUT the slot never responds ( i.e. Createing the file )

    I've added a little piece of code @ the header file which was generated , is that true !!
    I'm using :
    - qt-win-opensource-4.4.0
    - qmake to build the project
    -MinGw
    N.B. : I've created the slot , according to what I've understand from QT Assistant

    So , What' the problem @ My Code :

    Qt Code:
    1. /********************************************************************************
    2. ** Form generated from reading ui file 'Test.ui'
    3. **
    4. ** Created: Mon Jun 2 06:29:48 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. // **************** My Own Code *********************
    22. #include <stdio.h>
    23.  
    24. // **************** My Own Code *********************
    25.  
    26. QT_BEGIN_NAMESPACE
    27.  
    28. class Ui_Form
    29. {
    30.  
    31. public:
    32. QPushButton *pushButton;
    33. QLineEdit *lineEdit;
    34.  
    35. void setupUi(QWidget *Form)
    36. {
    37. if (Form->objectName().isEmpty())
    38. Form->setObjectName(QString::fromUtf8("Form"));
    39. Form->resize(475, 393);
    40. pushButton = new QPushButton(Form);
    41. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    42. pushButton->setGeometry(QRect(170, 190, 75, 24));
    43. lineEdit = new QLineEdit(Form);
    44. lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
    45. lineEdit->setGeometry(QRect(160, 90, 113, 20));
    46.  
    47. retranslateUi(Form);
    48. QObject::connect(pushButton, SIGNAL(pressed()), Form, SLOT(Custom()));
    49. QMetaObject::connectSlotsByName(Form);
    50. } // setupUi
    51.  
    52. void retranslateUi(QWidget *Form)
    53. {
    54. Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8));
    55. pushButton->setText(QApplication::translate("Form", "PushButton", 0, QApplication::UnicodeUTF8));
    56. lineEdit->setText(QString());
    57. Q_UNUSED(Form);
    58. } // retranslateUi
    59.  
    60.  
    61. // **************** My Own Code *********************
    62. public slots:
    63. void Custom();
    64. //******* My Own Code *********************
    65.  
    66. };
    67.  
    68. // **************** My Own Code *********************
    69. void Ui_Form::Custom()
    70. {
    71. FILE * pFile;
    72. pFile = fopen ("C:/Documents and Settings/Ahmed Osama/Desktop/myfile.txt","w");
    73. }
    74. // **************** My Own Code *********************
    75.  
    76. namespace Ui {
    77. class Form: public Ui_Form {};
    78. } // namespace Ui
    79.  
    80. QT_END_NAMESPACE
    81.  
    82. #endif // UI_TEST_H
    To copy to clipboard, switch view to plain text mode 


    Thanks

  2. #2
    Join Date
    Jul 2006
    Location
    Atlanta, GA
    Posts
    86
    Thanks
    26
    Thanked 6 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem When Creating my own Slot

    Yes it is true. All changes to the file will be lost when you build the project again.
    fnmblot
    --------------------------------------
    Gee Ricky, I'm sorry your mom blew up.

  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

    So , What's the problem in my Code which refuses to invoke my "Custom" function ?!!

  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

    Ui_Form is not a QObject. Only QObjects can have slots. Take a look at Using a Component in Your Application. Pay attention to single and multiple inheritance approaches. You are supposed to implement the slot in your own class which uses Ui_Form. Do NOT edit ui_test.h!
    J-P Nurmi

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

    Fatla (3rd June 2008)

  6. #5
    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

    Really Thanks jpn as well as fnmblot for your help .
    The resource that you've given to me was Extremely helpful .
    I've applied what was mentioned there . However , still my own slot doesn't work ?!!

    I've 4 main files besides the other makefile ,....etc
    1- Test.ui
    2- Main.cpp
    3- ui_Test.h
    4- CustomSlot.cpp => Which I implement my own slot into it .

    Her 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_Form 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: Tue Jun 3 23:57:00 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_Form
    24. {
    25. public:
    26. QPushButton *pushButton;
    27. QLineEdit *lineEdit;
    28.  
    29. void setupUi(QWidget *Form)
    30. {
    31. if (Form->objectName().isEmpty())
    32. Form->setObjectName(QString::fromUtf8("Form"));
    33. Form->resize(533, 393);
    34. pushButton = new QPushButton(Form);
    35. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    36. pushButton->setGeometry(QRect(170, 190, 75, 24));
    37. lineEdit = new QLineEdit(Form);
    38. lineEdit->setObjectName(QString::fromUtf8("lineEdit"));
    39. lineEdit->setGeometry(QRect(160, 90, 113, 20));
    40.  
    41. retranslateUi(Form);
    42. QObject::connect(pushButton, SIGNAL(pressed()), Form, SLOT(CustomSlot()));
    43.  
    44. QMetaObject::connectSlotsByName(Form);
    45. } // setupUi
    46.  
    47. void retranslateUi(QWidget *Form)
    48. {
    49. Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8));
    50. pushButton->setText(QApplication::translate("Form", "PushButton", 0, QApplication::UnicodeUTF8));
    51. lineEdit->setText(QString());
    52. Q_UNUSED(Form);
    53. } // retranslateUi
    54.  
    55. };
    56.  
    57. namespace Ui {
    58. class Form: public Ui_Form {};
    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.cpp 's code is :

    Qt Code:
    1. #include "ui_Test.h"
    2. #include <stdio.h>
    3.  
    4. class Test: public QWidget
    5. {
    6. // Q_OBJECT
    7.  
    8. public:
    9. Test(QWidget *parent = 0);
    10.  
    11. private slots:
    12. void CustomSlot();
    13.  
    14.  
    15. private:
    16. Ui_Form ui;
    17. };
    18.  
    19.  
    20. Test::Test(QWidget *parent)
    21. : QWidget(parent)
    22. {
    23. ui.setupUi(this);
    24. }
    25.  
    26. void Test:: CustomSlot()
    27. {
    28. FILE * pFile;
    29. pFile = fopen ("C:/myfile.txt","w");
    30. }
    To copy to clipboard, switch view to plain text mode 

    Thanks

  7. #6
    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.

  8. #7
    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

  9. #8
    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

  10. #9
    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

  11. #10
    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.

  12. #11
    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

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

    Fatla (6th June 2008)

  14. #12
    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 .

  15. #13
    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, 09:02
  2. problem creating dom tree
    By dreamer in forum Qt Programming
    Replies: 2
    Last Post: 8th May 2008, 09:12
  3. Problem with slot
    By beerkg in forum Qt Programming
    Replies: 29
    Last Post: 3rd April 2007, 20:54
  4. creating dll problem -> no exports ...
    By vrudolf in forum Qt Programming
    Replies: 2
    Last Post: 30th July 2006, 17:47
  5. Problem in creating thread in GUI application
    By jyoti kumar in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2006, 13: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.