Re: How do you add slots?
You don't. Designer doesn't parse any cpp or h files.
Re: How do you add slots?
How would you ever get a callback in C++ when a button is pressed then?
Re: How do you add slots?
Connect the signal to the slot in your code using QObject::connect(). For example:
In the header file:
Code:
class MyClass
: public QWidget,
private Ui
::MyWidget { Q_OBJECT
public:
public slots:
void mySlot();
}
In the source file:
Code:
{
setupUi(this);
connect(m_button, SIGNAL(clicked()),
this, SLOT(mySlot()));
}
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>.
Re: How do you add slots?
... or add the slot directly in Designer.
Re: How do you add slots?
Quote:
Originally Posted by
rakkar
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.
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
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtGui/QMainWindow>
#include <QtNetwork>
namespace Ui
{
class MainWindow;
}
{
Q_OBJECT
public:
~MainWindow();
private:
Ui::MainWindow *ui;
public slots:
void connectNet();
};
#endif // MAINWINDOW_H
MainWindow.cpp
Code:
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
){
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:
Quote:
/home/maurelio/pgm/qt-sdk/esercizi/FirstGui/mainwindow.h:21: error: ISO C++ forbids declaration of ‘connectAction’ with no type
Or i get
Quote:
/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.
Re: How do you add slots?
Quote:
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.
Quote:
/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.:
Code:
...
public slots:
void connectNet();
private:
...
Quote:
/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->".
Code:
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QtNetwork>
#include "ui_mainwindow.h"
class MainWindow
: public QMainWindow,
private Ui
::MainWindow{
Q_OBJECT
public:
public slots:
void connectNet();
};
#endif // MAINWINDOW_H
Code:
#include <QtGui>
#include "mainwindow.h"
MainWindow
::MainWindow(QWidget *parent
){
setupUi(this);
connect(connectButton, SIGNAL(clicked()), this, SLOT(connectNet()));
}
void MainWindow::connectNet()
{
//Some Stuff;
}
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?
Re: How do you add slots?
Quote:
Originally Posted by
maurelio79
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.
Quote:
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.