Results 1 to 3 of 3

Thread: Custom Slots

  1. #1
    Join Date
    May 2008
    Location
    North Wales, UK
    Posts
    3
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Custom Slots

    I have a simple application which has a main window, utilising the designer aspect in Creator. I have a menu item which should open a window with some details I collect from a database. This window and the database management is working OK. The problem I have is actually very simple. I have created a slot in the MainWindow class:
    Qt Code:
    1. class MainWindow : public QMainWindow
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. explicit MainWindow(QWidget *parent = 0);
    7. ~MainWindow();
    8.  
    9. public slots:
    10. void showActiveMembers();
    11.  
    12. private:
    13. Ui::MainWindow *ui;
    14. };
    To copy to clipboard, switch view to plain text mode 

    and in the implementation file I have:
    Qt Code:
    1. void MainWindow::showActiveMembers()
    2. {
    3. memberWindow *mbrWindow;
    4.  
    5. mbrWindow = new memberWindow;
    6. mbrWindow->resize(1000,500);
    7. mbrWindow->show();
    8. }
    To copy to clipboard, switch view to plain text mode 

    When I try to create the link to the action_ActiveMembers menu action in the "Signal and Slot Editor", the slot I have created above is not listed in the slots available with MainWindow as the receiving object.
    • I recognise that the signature of the slot and signal must match, but looking through the headers, it looks like the signature of the signal is "void triggered()", which matched my signature above.
    • I have saved all the files, and in fact built the application with one of the available slots. Under those circumstances it builds OK and runs, but still doesn't recognise my slot.
    • I do recognise I can do this in the code, but I am keen to understand why this is not being picked up in Creator.


    Have I missed something?

    Thanks!


    Added after 22 minutes:


    Well, I'll reply to my own thread. I am still interested to understand if there is a way of linking the ui signal to a custom slot, but I am now of the opinion that the approach intended is to use the signal/slot editor to link different parts of the ui together, and for custom slots such as mine, it is more appropriate to do the linking in the accompanying code:

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. connect(ui->actionActive_Members,SIGNAL(triggered()),
    7. this,SLOT(showActiveMembers()));
    8. }
    To copy to clipboard, switch view to plain text mode 

    Do let me know if I have missed something!

    edit: fixed the code in the slot
    Last edited by paulj; 3rd November 2012 at 08:42.

  2. #2

    Default Re: Custom Slots

    Right click on your custom class, either directly in the form or in the right hand list. Somewhere roughly in the middle of the upcoming context menu there should be "change signals/slots" (or something to that effect, I haven't got the English version installed). Enter your signals and slots there, and you can use them in the designer.

  3. #3
    Join Date
    Nov 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Custom Slots

    Dear Paul,

    I designed my mainwindow.ui for menu then coding mainwindow.cpp
    I put at mainwindow.h

    Qt Code:
    1. public slots:
    2. void about();
    To copy to clipboard, switch view to plain text mode 

    I use your suggested code and it is work for me.

    Thank you.

    my mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. ui->setupUi(this);
    9. QObject::connect(ui->actionSiapa_Dimana, SIGNAL(activated()), this, SLOT(about()));
    10. }
    11.  
    12. void MainWindow::about()
    13. {
    14. QMessageBox::about(this, tr("About UP4B - Siapa Dimana"),
    15. tr("<p><b>Siapa Dimana</b> adalah program untuk melihat Siapa "
    16. "Bermukim di mana dan Wilayahnya."
    17. "<br>Versi 0.01 baru kerangka awal.</p>"));
    18. }
    19.  
    20.  
    21. MainWindow::~MainWindow()
    22. {
    23. delete ui;
    24. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. My list custom errors. Signal and Slots
    By 8080205 in forum Newbie
    Replies: 1
    Last Post: 27th October 2012, 07:48
  2. Custom signal and slots
    By amitahire in forum Newbie
    Replies: 14
    Last Post: 4th July 2012, 12:35
  3. custom slots / generated ui
    By mboeni in forum Newbie
    Replies: 19
    Last Post: 20th October 2010, 14:21
  4. Creating custom slots
    By harb37 in forum Qt Programming
    Replies: 2
    Last Post: 23rd June 2008, 18:10
  5. Replies: 2
    Last Post: 12th July 2007, 09:55

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.