Results 1 to 8 of 8

Thread: Where is the connection declared?

  1. #1
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Where is the connection declared?

    Hi,

    I've just learned from a tutorial that I can define my own slot for a pushbutton using the creator like this:
    - put a QPushButton on the Gui in the built-in designer.
    - right click on the button -> "Go to slot..."
    - chose "clicked()"

    And than the creator creates the new slot in the header and jumps to the implementation and I can work on it.

    Now my question is just technical: Where is it declared that this signal-slot (namely clicked()-on_pushButton_clicked()) connection exists? I cannot see a connect(...) line anywhere, nor a footprint in the designer, but it is surely there somewhere as it works fine. Personally I like better to define the connections in the code becasure I can than better see what connections exist and what not, but this mechanism is really pretty so I may want to use it sometimes but I can't see why it works.
    Szilvi

  2. #2
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Where is the connection declared?

    What I did:

    Opened designer, new widget, added push button, added radioButton, used slot/editor to make the connection.

    Output from Form->View code.. :

    Qt Code:
    1. /********************************************************************************
    2. ** Form generated from reading UI file 'designergq4768.ui'
    3. **
    4. ** Created: Tue 23. Aug 10:57:54 2011
    5. ** by: Qt User Interface Compiler version 4.7.0
    6. **
    7. ** WARNING! All changes made in this file will be lost when recompiling UI file!
    8. ********************************************************************************/
    9.  
    10. #ifndef DESIGNERGQ4768_H
    11. #define DESIGNERGQ4768_H
    12.  
    13. #include <QtCore/QVariant>
    14. #include <QtGui/QAction>
    15. #include <QtGui/QApplication>
    16. #include <QtGui/QButtonGroup>
    17. #include <QtGui/QHeaderView>
    18. #include <QtGui/QPushButton>
    19. #include <QtGui/QRadioButton>
    20. #include <QtGui/QWidget>
    21.  
    22. QT_BEGIN_NAMESPACE
    23.  
    24. class Ui_Form
    25. {
    26. public:
    27. QPushButton *pushButton;
    28. QRadioButton *radioButton;
    29.  
    30. void setupUi(QWidget *Form)
    31. {
    32. if (Form->objectName().isEmpty())
    33. Form->setObjectName(QString::fromUtf8("Form"));
    34. Form->resize(400, 300);
    35. pushButton = new QPushButton(Form);
    36. pushButton->setObjectName(QString::fromUtf8("pushButton"));
    37. pushButton->setGeometry(QRect(50, 90, 75, 23));
    38. radioButton = new QRadioButton(Form);
    39. radioButton->setObjectName(QString::fromUtf8("radioButton"));
    40. radioButton->setGeometry(QRect(220, 90, 82, 17));
    41.  
    42. retranslateUi(Form);
    43. QObject::connect(pushButton, SIGNAL(clicked()), radioButton, SLOT(toggle()));
    44.  
    45. QMetaObject::connectSlotsByName(Form);
    46. } // setupUi
    47.  
    48. void retranslateUi(QWidget *Form)
    49. {
    50. Form->setWindowTitle(QApplication::translate("Form", "Form", 0, QApplication::UnicodeUTF8));
    51. pushButton->setText(QApplication::translate("Form", "PushButton", 0, QApplication::UnicodeUTF8));
    52. radioButton->setText(QApplication::translate("Form", "RadioButton", 0, QApplication::UnicodeUTF8));
    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 // DESIGNERGQ4768_H
    To copy to clipboard, switch view to plain text mode 

    There you have one connection that has been made by using signal/slot editor and you have also a method that does automatic connections from matching signals of children objects to slots of that object, for example to on_pushButton_clicked(). That's the line:

    Qt Code:
    1. QMetaObject::connectSlotsByName(Form);
    To copy to clipboard, switch view to plain text mode 
    Last edited by Rachol; 23rd August 2011 at 09:34. Reason: spelling corrections
    Don't write a post just to thank someone, use "Thanks" button.

  3. #3
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Where is the connection declared?

    yeah, but that is generated when it is compiled. I'm sure about that, because it works even if I delete everithying from the xxx-build-desktop folder where it is going to be compiled.
    Is it maybe an atomatic mechanism wheneve I name a slot with "on_objectnaem_clicked()"?
    Szilvi

  4. #4
    Join Date
    Jun 2011
    Location
    Finland
    Posts
    164
    Thanks
    1
    Thanked 26 Times in 26 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Where is the connection declared?

    Yes and Yes.
    Don't write a post just to thank someone, use "Thanks" button.

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Where is the connection declared?

    Is it maybe an atomatic mechanism wheneve I name a slot with "on_objectnaem_clicked()"?
    the forms you design in designer are saved in *.ui files.
    These are just XML representation of your gui.
    Part of your make sequence calls uic (the ui compiler) which translated the *.ui files to C++.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  6. #6
    Join Date
    Aug 2009
    Location
    Belgium
    Posts
    310
    Thanks
    10
    Thanked 31 Times in 25 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Where is the connection declared?

    Quote Originally Posted by szisziszilvi View Post
    Where is it declared that this signal-slot (namely clicked()-on_pushButton_clicked()) connection exists? .... Personally I like better to define the connections in the code becasure I can than better see what connections exist and what not, but this mechanism is really pretty so I may want to use it sometimes but I can't see why it works.
    See http://doc.qt.nokia.com/...automatic-connections

    Die-hard Qt programmers prefer to write their own connect() statements instead of using that 'pretty' mechanism by just declaring their own slot, and use connect() in the constructor of their form to make the connection. But then again... they might also use vi ...

    Regards,
    Marc

  7. #7
    Join Date
    Nov 2010
    Location
    Budapest, Hungary
    Posts
    125
    Thanks
    17
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Where is the connection declared?

    Thanks for the link. Now I see what is happening.

    However a few coments on the topic. Whilst everybody is speaking about this line:
    Qt Code:
    1. QMetaObject::connectSlotsByName(this);
    To copy to clipboard, switch view to plain text mode 
    actually this line is just nowhere in the project files. Can be pretty confusing that it appears in the ui_mainwindow.h because this header file does not even exist before compiling. So assuming we have only these files:
    main.cpp
    mainwindow.cpp
    mainwindow.h
    mainwindow.ui
    myproject.pro

    The mentioned line can not be found in these files. The tricky thing is that when generating ui_mainwindow.h the critical line is _always_added - no matter if I created any connection with the above described method or not. So I can also declare them by hand with the proper syntax and the magical "right click - go to slot - clicked()" mechanism does the very same and adds no additional clue to the code.
    Szilvi

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Where is the connection declared?

    Quote Originally Posted by szisziszilvi View Post
    Whilst everybody is speaking about this line:
    Qt Code:
    1. QMetaObject::connectSlotsByName(this);
    To copy to clipboard, switch view to plain text mode 
    actually this line is just nowhere in the project files.
    It is in setupUi() that is generated by uic from .ui files.

    The mentioned line can not be found in these files. The tricky thing is that when generating ui_mainwindow.h the critical line is _always_added - no matter if I created any connection with the above described method or not.
    This mechanism does not depend on the way you create your UIs. Using QtCreator for that is not the only option. Don't assume everybody uses QtCreator's slot facilities only because you do.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. ui not declared in scope
    By steve.bush in forum Newbie
    Replies: 5
    Last Post: 19th March 2011, 08:58
  2. Replies: 1
    Last Post: 2nd April 2010, 06:42
  3. QDomDocument was not declared in this scope
    By grantbj74 in forum Newbie
    Replies: 5
    Last Post: 25th August 2009, 09:43
  4. beginInsertRows was not declared ???
    By travlr in forum Qt Programming
    Replies: 2
    Last Post: 6th June 2009, 21:22
  5. 'XEvent' has not been declared
    By dacla in forum Qt for Embedded and Mobile
    Replies: 3
    Last Post: 31st March 2008, 12:44

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.