PDA

View Full Version : How do you add slots?



rakkar
24th August 2009, 00:25
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.

franz
24th August 2009, 07:03
You don't. Designer doesn't parse any cpp or h files.

rakkar
24th August 2009, 07:32
How would you ever get a callback in C++ when a button is pressed then?

victor.fernandez
24th August 2009, 12:08
Connect the signal to the slot in your code using QObject::connect() (http://qt.nokia.com/doc/4.5/qobject.html#connect-2). For example:

In the header file:


class MyClass : public QWidget, private Ui::MyWidget {
Q_OBJECT

public:
MyClass(QWidget *parent = 0);

public slots:
void mySlot();
}

In the source file:

MyClass::MyClass(QWidget *parent)
: QWidget(parent)
{
setupUi(this);

connect(m_button, SIGNAL(clicked()),
this, SLOT(mySlot()));
}

Lykurg
24th August 2009, 14:29
...or let Qt work for you:

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

wysota
24th August 2009, 15:08
... or add the slot directly in Designer.

Archimedes
25th August 2009, 05:30
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.

maurelio79
25th August 2009, 22:44
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



#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtGui/QMainWindow>
#include <QtNetwork>

namespace Ui
{
class MainWindow;

}

class MainWindow : public QMainWindow
{
Q_OBJECT

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

private:
Ui::MainWindow *ui;

public slots:
void connectNet();
};


#endif // MAINWINDOW_H



MainWindow.cpp



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


MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

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

void MainWindow::connectNet()
{
//Some Stuff;
}


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.

victor.fernandez
26th August 2009, 07:25
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.:


...
public slots:
void connectNet();

private:
QAction *connectAction;
...


/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->".


#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtNetwork>
#include "ui_mainwindow.h"

class MainWindow : public QMainWindow, private Ui::MainWindow
{
Q_OBJECT

public:
MainWindow(QWidget *parent = 0);

public slots:
void connectNet();
};

#endif // MAINWINDOW_H


#include <QtGui>
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
setupUi(this);

connect(connectButton, SIGNAL(clicked()), this, SLOT(connectNet()));
}

void MainWindow::connectNet()
{
//Some Stuff;
}

maurelio79
26th August 2009, 23:07
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?

wysota
26th August 2009, 23:11
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.