Question

Hi im brand new to c++ and trying to get my head around the concepts. I am creating a very simple app to get going with the help of the tutorials, so im trying to do my own first try. I'm having problems with the file.h and file.cpp the one besides main.cpp I would like to click the button in the button box "ok" and have text come up in the text box. Here is MainWindow.h first

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "ui_MainWindow.h"

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

public:
    MainWindow(QMainWindow *parent = 0);

private slots:
    //here is where im tyring to add a slot.
    void on_buttonbox_buttonClicked ( QAbstractButton * );
    // void on_inputSpinBox2_valueChanged(int value);
    private:
    Ui::MainWindow ui;
};

#endif

Next is the MainWindow.cpp

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

MainWindow::MainWindow(QMainWindow *parent)
     : QMainWindow(parent)
{
    ui.setupUi(this);
}

//This is where i would like to catch the clicked signal from the ok button and add the text to the text box.
void MainWindow::on_buttonbox_buttonClicked ( QAbstractButton * ){
    ui.textEdit->setText(QString::number(16));
}

Im trying to be as simple as posible to just to get it going, it will compile but i can't get the signals and slots to talk, where am i going wrong.... remember brand new.

Was it helpful?

Solution

You have to connect your Slots to the Signals, add this into your constructor:

this->connect(this->ui.yourButton, SIGNAL(clicked()), this, SLOT(on_buttonbox_buttonClicked(QAbstractButton*))); 
//                         ^
//                         |
//             The name of your Button here ...

Also please see here: http://qt-project.org/doc/qt-4.8/signalsandslots.html


Edit:

MainWindow.cpp

MainWindow::MainWindow(QMainWindow *parent) : QMainWindow(parent)
{
    ui.setupUi(this);

    // Connect Signals-Slots
    this->connect(this->ui.yourButton, SIGNAL(clicked()), this, SLOT(on_buttonbox_buttonClicked(QAbstractButton*))); 
}

// ...

But don't forget to change yourButton to whatever you've named yours.

OTHER TIPS

Alright, you have to connect the signal to the slot somewhere. You should do this on the constructor,

connect(button, SIGNAL(clicked()), this, SLOT(on_buttonbox_buttonClicked(QAbstractButton *)));

Just remember that the slot only will be called if there is a signal connect to it. Otherwise your button won´t be able to know where to go.

The mechanism of SIGNAL and SLOT is very simple and is used to register a widget(buttons, spinbox etc..) to an event. For example "when I click that button a new window will show up." That being said, let's see how can we register our SLOT(what to do after receiving the signal) to his SIGNAL(an event: a click, a selection, an edit of a form etc..)

QObject::connect( button , SIGNAL( click()), this , SLOT( openWindow() ))

button is the widget that will throw the signal.

SIGNAL( click()) you are telling that clicking(press and release) button an action will be performed

this is the object that declare the slot

SLOT( openWindow() ) is the method(slot) will be called clicking button

Signals and slots must have same parameters!! So to answer your question you have to declare a slot with same parameters as the signal. click() has no parameter so you have to declare your slot as:

    void on_buttonbox_buttonClicked ()

PS: as I remember there is an issue naming a slot with the prefix on. But I have to do a little search because I don't remember very well.

update: I made a little test and naming the slot with the prefix on_ gives an error message at run time QMetaObject::connectSlotsByName: No matching signal for on_ClickChangeBack() but the slot execute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top