Hi all.
Can someone please help me. I have designed a GUI in Qt and have got my main.cpp code to execute the GUI and I have also been able to access my components via my C++ code, like changing button font size, etc. But one thing that I'm really struggeling with is to call one of my own functions from a button click. I have read about signals and slot, but I have not successfully connected the GUI's signals to my code. Can anyone please help me or point me to a site that hav a tutorial or code snippets so that I can understand how to implement this?
Thanks!
Qt designer and C++
-
- Moderator Emeritus
- Posts: 5547
- Joined: 21 Dec 2002, 02:00
- Processor: i7 3770k
- Motherboard: ASUS P8P67-Pro
- Graphics card: 2xNvidia GTX670
- Memory: 16 GB Gskill Sniper
- Location: In SQL Space inserting 'null' on purpose
- Contact:
Use MFC AppWizard, name your button, make it dialog based...
Place a button control on the dialog and double click on it to create its click event. Before typing the code for the button click (BN_CLICKED) event you must define a new function pointer with the correct number of parameters, according to the parameters of the function above.
Place a button control on the dialog and double click on it to create its click event. Before typing the code for the button click (BN_CLICKED) event you must define a new function pointer with the correct number of parameters, according to the parameters of the function above.
Temporary Absence
The signal/slots mechanism is a core feature of Qt, it's documented in the offical docs at http://doc.trolltech.com/
Basically, you can define a local slot called <i>clickHandler()</i> like this in the your header file:
Then, in your implementation, you can connect the button's <i>clicked()</i> signal to your local slot like this:
(Where <i>button</i> is a pointer to your button.)
Now every time the button is clicked your clickHandler() function will be called.
Basically, you can define a local slot called <i>clickHandler()</i> like this in the your header file:
Code: Select all
private slots:
void clickHandler ();
Then, in your implementation, you can connect the button's <i>clicked()</i> signal to your local slot like this:
Code: Select all
connect ( button, SIGNAL ( clicked() ), this, SLOT ( clickHandler() ) );
Now every time the button is clicked your clickHandler() function will be called.
'One will rarely err if extreme actions be ascribed to vanity, ordinary actions to habit, and mean actions to fear.'
- Friedrich Nietzsche
'Do not argue with Judas, nube, that would be foolish!'
- D3PART3D
- Friedrich Nietzsche
'Do not argue with Judas, nube, that would be foolish!'
- D3PART3D
Ok, i'm really struggeling to get this working. This is my code. Could you please tell me where I need to put the "private slots" part in cause I get a compile error when I put it my header file. Also, I have a mainwindow.cpp and a main.cpp file. Where should i call/implement the function?
main.cpp:
mainwindow.cpp:
mainwindow.h:
main.cpp:
Code: Select all
#include <QApplication>
#include <QPushButton>
#include <QFont>
#include "mainwindow.h"
int main(int argc,char** argv)
{
QApplication app(argc,argv);
MainWindow mw;
mw.show();
return app.exec();
}
mainwindow.cpp:
Code: Select all
#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//ui.add_btn->
connect(ui.add_btn, SIGNAL(clicked()), this, SLOT(clickHandler()));
}
Code: Select all
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "ui_mainform.h"
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
private:
Ui::MainWindow ui;
};
#endif