Qt designer and C++

Get help on programming - C++, Java, Delphi, etc.
Post Reply
frankwas
Registered User
Posts: 100
Joined: 31 Oct 2004, 02:00

Qt designer and C++

Post by frankwas »

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!
Anthro
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:

Post by Anthro »

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.
Temporary Absence
Judas
Registered User
Posts: 2118
Joined: 17 Oct 2006, 02:00
Location: Stellenbosch
Contact:

Post by Judas »

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:

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() ) );
(Where <i>button</i> is a pointer to your button.)

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
frankwas
Registered User
Posts: 100
Joined: 31 Oct 2004, 02:00

Post by frankwas »

Thanks a lot guys!


I'm gonna get cracking right away! I really appreciate your input!

Frank
frankwas
Registered User
Posts: 100
Joined: 31 Oct 2004, 02:00

Post by frankwas »

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:

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()));
}
mainwindow.h:

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
Post Reply