Results 1 to 7 of 7

Thread: Howto create custom slot with Designer

  1. #1
    Join Date
    Jan 2011
    Location
    Boston, MA USA
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Howto create custom slot with Designer

    I am working on my first Qt project using Designer and Visual Studio 2008 with Qt add-on. In Designer I added a button and connected it to a custom slot. Designer generated connect(...) but it did not add slot function to any .h file. I read FAQ "How can I add a custom slot in Qt4 Designer?" and it says that it's not supported any more. Unfortunately FAQ gives a link to documentation that is broken.

    Can somebody point me at tutorial or something explaining how properly add custom slots while using designer? Anything I could find so far describes pure coding and I need to know how to co-exist with designer-generated code.

    Thank you.

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 348 Times in 333 Posts

    Default Re: Howto create custom slot with Designer

    You can create the slot function yourself. Designer only regenerates .ui file which UIC will use to regenerate _ui.h, so your .cpp/.h file is safe (will not be modified by designer)

  3. #3
    Join Date
    Jan 2011
    Location
    Boston, MA USA
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Howto create custom slot with Designer

    When I tried, my code wasn't called and connect function failed. I was hoping to find an example (with Designer, because it creates a bunch of classes and a namespace).


    Added after 1 41 minutes:


    OK, I will be more specific. I have created a button btnConnect and from Designer linked signal clicked() with custom slot btnConnect_Clicked(). Designer added the line to generated .h file:

    void setupUi(QMainWindow *TcpipClientQtClass)
    {
    ...
    QObject::connect(btnConnect, SIGNAL(clicked()), TcpipClientQtClass, SLOT(btnConnect_Clicked()));
    }

    The problem is that the target class (TcpipClientQtClass) does not have declaration for slot function. And I cannot add it manually because this class is generated by Designer and my change will be overwritten. I created inherited class with this function but it was never called, probably, because it is not declared in the parent. And parent is auto-generated.

    How can I get out of this catch-22?
    Last edited by gkhrapunovich; 26th January 2011 at 20:23.

  4. #4
    Join Date
    Mar 2006
    Location
    Mexico City
    Posts
    31
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    3
    Thanked 1 Time in 1 Post

    Default Re: Howto create custom slot with Designer

    You have to subclass your main window class:

    Qt Code:
    1. #include <QMainWindow>
    2. #include "ui_mainclass.h"
    3.  
    4. namespace Ui {
    5. class MainClass;
    6. }
    7.  
    8. class NewClass : public QMainWindow , public Ui::MainClass
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. NewClass(QMainWindow* parent = 0 ,
    14. Qt::WindowFlags fl = Qt::Window);
    15. ~NewClass();
    16. ...
    17. public slots:
    18. void yourCustomSlot();
    19.  
    20. ...
    21. Ui::MainClass *ui;
    22. ...
    23. };
    To copy to clipboard, switch view to plain text mode 

    And then, your NewClass is:

    Qt Code:
    1. #include "newclass.h"
    2. ...
    3.  
    4. NewClass::NewClass(QMainWindow * parent, Qt::WindowFlags fl) :
    5. QMainWindow(parent)
    6. {
    7. setupUi(this);
    8. ...
    9. }
    10.  
    11. voidYourCustomSlot()
    12. {
    13. ...
    14. }
    To copy to clipboard, switch view to plain text mode 

    I hope this can help you.

  5. #5
    Join Date
    Jan 2011
    Location
    Boston, MA USA
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Howto create custom slot with Designer

    Thank you. In my case subclass was already created by the wizard (I am using Qt add-on to Visual Studio). I tried it before but it did not work because I didn't add the line "public slots:". Now everything works.
    What is annoying that there is no compiler checks. If I misspell the slot name, the only indication of error will be that slot won't get called.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    6
    Thanked 348 Times in 333 Posts

    Default Re: Howto create custom slot with Designer

    You are correct in the fact that there are no compile type checks. Due to the flexibility of the system, this is not possible (eg. you may be connecting signal is some closed source file so only you know the signal names).

    However, an error will be generated at run time once the connect method is called stating no such slot or no such signal and the full class name. The return code of the connect call can also be checked, and it's likely that if the connect fails, your unit tests will fail also. So in all, your not going to forget about it

  7. #7
    Join Date
    Jan 2011
    Location
    Boston, MA USA
    Posts
    9
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Howto create custom slot with Designer

    Thank you for this note. I knew I could check the return value of connect() but it involved manual editting of compiler-generated file and was very inconvinient. It didn't cross my mind to look at the Output window. Now I see the error message there.

Similar Threads

  1. Replies: 5
    Last Post: 4th February 2010, 18:15
  2. Replies: 1
    Last Post: 22nd January 2010, 12:22
  3. How to create Custom Slot upon widgets
    By ashukla in forum Qt Programming
    Replies: 6
    Last Post: 8th September 2007, 15:07
  4. Example HowTo create custom view
    By dexjam in forum Newbie
    Replies: 6
    Last Post: 12th July 2006, 12:06
  5. How to create custom slot in Qt Designer 4.1?
    By jamadagni in forum Qt Tools
    Replies: 31
    Last Post: 18th January 2006, 21:46

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.