Results 1 to 11 of 11

Thread: How do you add slots?

  1. #1
    Join Date
    Aug 2009
    Posts
    56
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default How do you add slots?

    In Visual Studio, I used File / New / QT4 Projects / Qt Application, and now have an application. In QT Designer I want a slot on my class that I can call from a button press. I tried going to the header file for the class (TestApp.h) and adding

    public slots:
    void MyNewSlot( void );

    This compiled, but in QT Designer under Signal / Slot Editor, "MyNewSlot" wasn't added to the dropdown list under Slot(). Only the options that were already there show up (close(), update(), etc)

    How do I get new slots to be selectable in the Signal / Slot editor?

    Is there documentation on this somewhere? Everything I have seen assumes you are using text files only, and tends to be incomplete.

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do you add slots?

    You don't. Designer doesn't parse any cpp or h files.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Aug 2009
    Posts
    56
    Thanks
    14
    Thanked 1 Time in 1 Post

    Default Re: How do you add slots?

    How would you ever get a callback in C++ when a button is pressed then?

  4. #4
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do you add slots?

    Connect the signal to the slot in your code using QObject::connect(). For example:

    In the header file:

    Qt Code:
    1. class MyClass : public QWidget, private Ui::MyWidget {
    2. Q_OBJECT
    3.  
    4. public:
    5. MyClass(QWidget *parent = 0);
    6.  
    7. public slots:
    8. void mySlot();
    9. }
    To copy to clipboard, switch view to plain text mode 

    In the source file:
    Qt Code:
    1. MyClass::MyClass(QWidget *parent)
    2. : QWidget(parent)
    3. {
    4. setupUi(this);
    5.  
    6. connect(m_button, SIGNAL(clicked()),
    7. this, SLOT(mySlot()));
    8. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: How do you add slots?

    ...or let Qt work for you:

    slot: on_<object name>_<signalname>(), where you have to substitute <object name> and <signalname>.

  6. #6
    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: How do you add slots?

    ... or add the slot directly in Designer.
    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.


  7. #7
    Join Date
    Aug 2009
    Location
    Greece, Chania
    Posts
    63
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60

    Default Re: How do you add slots?

    Quote Originally Posted by rakkar View Post
    How would you ever get a callback in C++ when a button is pressed then?
    Keep in mind that the moc tool will generate a class based on what you've done in the designer.
    Then you subclass it and add more functionality (slots, even new widgets, etc) in the new class.

  8. #8
    Join Date
    Aug 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do you add slots?

    Ok. I'm starting to use .ui (in this way is faster build a gui), but i'm not able to add an action to a mine button.

    MainWindow.h

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include <QtNetwork>
    6.  
    7. namespace Ui
    8. {
    9. class MainWindow;
    10.  
    11. }
    12.  
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16.  
    17. public:
    18. MainWindow(QWidget *parent = 0);
    19. ~MainWindow();
    20.  
    21. private:
    22. Ui::MainWindow *ui;
    23.  
    24. public slots:
    25. void connectNet();
    26. };
    27.  
    28.  
    29. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    MainWindow.cpp

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4.  
    5. MainWindow::MainWindow(QWidget *parent)
    6. : QMainWindow(parent), ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. }
    10.  
    11. MainWindow::~MainWindow()
    12. {
    13. delete ui;
    14. }
    15.  
    16. void MainWindow::connectNet()
    17. {
    18. //Some Stuff;
    19. }
    To copy to clipboard, switch view to plain text mode 

    In the MainWindow.ui i have a cancel button (no problem with, because i added a close() slot with gui), but if i want to add a myFunction to other button??

    I tried different thing but usually i get:

    /home/maurelio/pgm/qt-sdk/esercizi/FirstGui/mainwindow.h:21: error: ISO C++ forbids declaration of ‘connectAction’ with no type
    Or i get
    /home/maurelio/pgm/qt-sdk/esercizi/FirstGui/mainwindow.cpp:9: error: ‘connectButton’ was not declared in this scope
    but connectButton is in the ui file.
    Last edited by maurelio79; 25th August 2009 at 22:53.

  9. #9
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How do you add slots?

    In the MainWindow.ui i have a cancel button (no problem with, because i added a close() slot with gui), but if i want to add a myFunction to other button??
    You would need to subclass the button in order to add a method. However, you normally don't need to, since you can just add myFunction to MainWindow and connect the clicked() signal to your slot.

    /home/maurelio/pgm/qt-sdk/esercizi/FirstGui/mainwindow.h:21: error: ISO C++ forbids declaration of ‘connectAction’ with no type
    This error usually happens when you forget to add an #include for the class. However, I don't think connectAction is a class but I guess it's an object, so you probably forgot to specify the class name. e.g.:

    Qt Code:
    1. ...
    2. public slots:
    3. void connectNet();
    4.  
    5. private:
    6. QAction *connectAction;
    7. ...
    To copy to clipboard, switch view to plain text mode 

    /home/maurelio/pgm/qt-sdk/esercizi/FirstGui/mainwindow.cpp:9: error: ‘connectButton’ was not declared in this scope
    This means there's no such connectButton in MainWindow. If you added it in Qt Designer, you would have to use ui->connectButton instead. You could make MainWindow a subclass of Ui::MainWindow. This way you could directly access connectButton without needing to add "ui->".

    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtNetwork>
    6. #include "ui_mainwindow.h"
    7.  
    8. class MainWindow : public QMainWindow, private Ui::MainWindow
    9. {
    10. Q_OBJECT
    11.  
    12. public:
    13. MainWindow(QWidget *parent = 0);
    14.  
    15. public slots:
    16. void connectNet();
    17. };
    18.  
    19. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui>
    2. #include "mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent)
    5. : QMainWindow(parent)
    6. {
    7. setupUi(this);
    8.  
    9. connect(connectButton, SIGNAL(clicked()), this, SLOT(connectNet()));
    10. }
    11.  
    12. void MainWindow::connectNet()
    13. {
    14. //Some Stuff;
    15. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Aug 2009
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How do you add slots?

    Yes, you're right, i forgot to declare QAction.... sorry, but i'm try to learn both: C++ and Qt, no much time and for me is very difficult. Thanks very much.

    Maybe i'm wrong, but it seems that using ui file, the application is a little bit slow, without ui file (using code to make gui) on my FreeRunner the application is faster.

    Is it possible in your opinion?

  11. #11
    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: How do you add slots?

    Quote Originally Posted by maurelio79 View Post
    sorry, but i'm try to learn both: C++ and Qt, no much time and for me is very difficult.
    Save yourself pain and learn C++ first before taking on Qt.

    Maybe i'm wrong, but it seems that using ui file, the application is a little bit slow, without ui file (using code to make gui) on my FreeRunner the application is faster.

    Is it possible in your opinion?
    No, it's not possible.
    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. Signals and Slots Problem
    By GenericProdigy in forum Qt Programming
    Replies: 4
    Last Post: 2nd February 2009, 09:06
  2. Replies: 12
    Last Post: 23rd June 2008, 08:05
  3. Slots or new slots
    By Colx007 in forum Qt Programming
    Replies: 3
    Last Post: 21st January 2008, 17:38
  4. Adding slots in Designer
    By jamos in forum Qt Tools
    Replies: 5
    Last Post: 18th May 2006, 23:28
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.