PDA

View Full Version : Qt Creator Custom Slots



paulj
3rd November 2012, 08:34
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:


class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();

public slots:
void showActiveMembers();

private:
Ui::MainWindow *ui;
};


and in the implementation file I have:


void MainWindow::showActiveMembers()
{
memberWindow *mbrWindow;

mbrWindow = new memberWindow;
mbrWindow->resize(1000,500);
mbrWindow->show();
}


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:



MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
connect(ui->actionActive_Members,SIGNAL(triggered()),
this,SLOT(showActiveMembers()));
}


Do let me know if I have missed something!

edit: fixed the code in the slot

bulbmonkey
4th November 2012, 00:10
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.

yupifourbi
19th November 2012, 02:40
Dear Paul,

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



public slots:
void about();


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

Thank you.

my mainwindow.cpp


#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QObject::connect(ui->actionSiapa_Dimana, SIGNAL(activated()), this, SLOT(about()));
}

void MainWindow::about()
{
QMessageBox::about(this, tr("About UP4B - Siapa Dimana"),
tr("<p><b>Siapa Dimana</b> adalah program untuk melihat Siapa "
"Bermukim di mana dan Wilayahnya."
"<br>Versi 0.01 baru kerangka awal.</p>"));
}


MainWindow::~MainWindow()
{
delete ui;
}