0

So,I am making a simple calculator app in C++ using Qt4 as the GUI framework. I have worked with Qt before but this time I am using C++ rather than python.

Now I have three files in my project,

  • main.cpp (Simple suite that calls and runs the main GUI class)
  • window.cpp (File Containing suite that implements the GUI class and other routines.)
  • header.h (Template for window.cpp file.)

The main GUI implementing class is called Ui_MainWindow.

Now, this class has a member function called handleButton

What I want to do is call this handlebutton function when a clicked event happens. So, what I should do is to connect the event to the slot (handlebutton) , right??

The name of the Button is pushButton, and as stated the handlebutton is a member function of the class Ui_MainWindow

And here is the snippet which should implement the later:

QObject::connect( pushButton , SIGNAL (clicked()), this , handleButton );

So,when this click event happens this->handleButton should be called, right??

But I am getting an error which is:

error: no matching function for call to ‘QObject::connect(QPushButton*&, const char [11], Ui_MainWindow*, void (&)())
QObject::connect( pushButton , SIGNAL (clicked()), this , handleButton );

I tried making the member func static and tried all that i could think of but it doesn't seems to work for me.

Please guide me on how to fix this.

FILE: window.cpp

#include <iostream>
#include <window.h>

void Ui_MainWindow::handleButton ( void ) {
    std::cout << "Hi this is  MEeeeee!.\n";
    return;
}

void Ui_MainWindow::setupUi(QMainWindow *MainWindow) {
        if (MainWindow->objectName().isEmpty())
            MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
        MainWindow->resize(423, 410);
        MainWindow->setMaximumSize(QSize(700, 600));
        centralwidget = new QWidget(MainWindow);
        centralwidget->setObjectName(QString::fromUtf8("centralwidget"));
        gridLayout_2 = new QGridLayout(centralwidget);
        gridLayout_2->setObjectName(QString::fromUtf8("gridLayout_2"));
        label_2 = new QLabel(centralwidget);
        label_2->setObjectName(QString::fromUtf8("label_2"));
        label_2->setMaximumSize(QSize(16777215, 41));
        QFont font;
        font.setPointSize(20);
        label_2->setFont(font);
        label_2->setAlignment(Qt::AlignCenter);

        gridLayout_2->addWidget(label_2, 0, 0, 1, 1);

        label = new QLabel(centralwidget);
        label->setObjectName(QString::fromUtf8("label"));
        QFont font1;
        font1.setFamily(QString::fromUtf8("Roboto"));
        font1.setPointSize(22);
        label->setFont(font1);
        label->setLayoutDirection(Qt::RightToLeft);
        label->setStyleSheet(QString::fromUtf8("background-color: rgb(179, 179, 179);\n""border-radius:4px;\n""padding:20px;"));
        label->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);

        gridLayout_2->addWidget(label, 1, 0, 1, 1);

        horizontalLayout = new QHBoxLayout();
        horizontalLayout->setObjectName(QString::fromUtf8("horizontalLayout"));
        gridLayout = new QGridLayout();
        gridLayout->setObjectName(QString::fromUtf8("gridLayout"));
        pushButton = new QPushButton(centralwidget);
        pushButton->setObjectName(QString::fromUtf8("pushButton"));
        pushButton->setMinimumSize(QSize(0, 60));
        pushButton->setMaximumSize(QSize(60, 60));

        gridLayout->addWidget(pushButton, 0, 0, 1, 1);

        pushButton_2 = new QPushButton(centralwidget);
        pushButton_2->setObjectName(QString::fromUtf8("pushButton_2"));
        pushButton_2->setMinimumSize(QSize(0, 60));
        pushButton_2->setMaximumSize(QSize(60, 60));

        gridLayout->addWidget(pushButton_2, 0, 1, 1, 1);

        pushButton_3 = new QPushButton(centralwidget);
        pushButton_3->setObjectName(QString::fromUtf8("pushButton_3"));
        pushButton_3->setMinimumSize(QSize(0, 60));
        pushButton_3->setMaximumSize(QSize(60, 60));

        gridLayout->addWidget(pushButton_3, 0, 2, 1, 1);

        pushButton_4 = new QPushButton(centralwidget);
        pushButton_4->setObjectName(QString::fromUtf8("pushButton_4"));
        pushButton_4->setMinimumSize(QSize(0, 60));
        pushButton_4->setMaximumSize(QSize(60, 60));

        gridLayout->addWidget(pushButton_4, 1, 0, 1, 1);

        pushButton_5 = new QPushButton(centralwidget);
        pushButton_5->setObjectName(QString::fromUtf8("pushButton_5"));
        pushButton_5->setMinimumSize(QSize(0, 60));
        pushButton_5->setMaximumSize(QSize(60, 60));

        gridLayout->addWidget(pushButton_5, 1, 1, 1, 1);

        pushButton_6 = new QPushButton(centralwidget);
        pushButton_6->setObjectName(QString::fromUtf8("pushButton_6"));
        pushButton_6->setMinimumSize(QSize(0, 60));
        pushButton_6->setMaximumSize(QSize(60, 60));

        gridLayout->addWidget(pushButton_6, 1, 2, 1, 1);

        pushButton_7 = new QPushButton(centralwidget);
        pushButton_7->setObjectName(QString::fromUtf8("pushButton_7"));
        pushButton_7->setMinimumSize(QSize(0, 60));
        pushButton_7->setMaximumSize(QSize(60, 60));

        gridLayout->addWidget(pushButton_7, 2, 0, 1, 1);

        pushButton_8 = new QPushButton(centralwidget);
        pushButton_8->setObjectName(QString::fromUtf8("pushButton_8"));
        pushButton_8->setMinimumSize(QSize(0, 60));
        pushButton_8->setMaximumSize(QSize(60, 60));

        gridLayout->addWidget(pushButton_8, 2, 1, 1, 1);

        pushButton_9 = new QPushButton(centralwidget);
        pushButton_9->setObjectName(QString::fromUtf8("pushButton_9"));
        pushButton_9->setMinimumSize(QSize(0, 60));
        pushButton_9->setMaximumSize(QSize(60, 60));

        gridLayout->addWidget(pushButton_9, 2, 2, 1, 1);


        horizontalLayout->addLayout(gridLayout);

        verticalLayout = new QVBoxLayout();
        verticalLayout->setObjectName(QString::fromUtf8("verticalLayout"));
        verticalLayout->setContentsMargins(0, -1, -1, -1);
        pushButton_15 = new QPushButton(centralwidget);
        pushButton_15->setObjectName(QString::fromUtf8("pushButton_15"));
        pushButton_15->setMinimumSize(QSize(0, 127));
        pushButton_15->setMaximumSize(QSize(60, 152));

        verticalLayout->addWidget(pushButton_15);

        pushButton_10 = new QPushButton(centralwidget);
        pushButton_10->setObjectName(QString::fromUtf8("pushButton_10"));
        pushButton_10->setMinimumSize(QSize(0, 60));
        pushButton_10->setMaximumSize(QSize(60, 60));

        verticalLayout->addWidget(pushButton_10);


        horizontalLayout->addLayout(verticalLayout);

        verticalLayout_3 = new QVBoxLayout();
        verticalLayout_3->setObjectName(QString::fromUtf8("verticalLayout_3"));
        pushButton_12 = new QPushButton(centralwidget);
        pushButton_12->setObjectName(QString::fromUtf8("pushButton_12"));
        pushButton_12->setMinimumSize(QSize(0, 60));
        pushButton_12->setMaximumSize(QSize(60, 60));

        verticalLayout_3->addWidget(pushButton_12);

        pushButton_11 = new QPushButton(centralwidget);
        pushButton_11->setObjectName(QString::fromUtf8("pushButton_11"));
        pushButton_11->setMinimumSize(QSize(0, 60));
        pushButton_11->setMaximumSize(QSize(60, 60));

        verticalLayout_3->addWidget(pushButton_11);

        pushButton_14 = new QPushButton(centralwidget);
        pushButton_14->setObjectName(QString::fromUtf8("pushButton_14"));
        pushButton_14->setMinimumSize(QSize(0, 60));
        pushButton_14->setMaximumSize(QSize(60, 60));

        verticalLayout_3->addWidget(pushButton_14);


        horizontalLayout->addLayout(verticalLayout_3);

        verticalLayout_2 = new QVBoxLayout();
        verticalLayout_2->setObjectName(QString::fromUtf8("verticalLayout_2"));
        pushButton_17 = new QPushButton(centralwidget);
        pushButton_17->setObjectName(QString::fromUtf8("pushButton_17"));
        pushButton_17->setMinimumSize(QSize(0, 127));
        pushButton_17->setMaximumSize(QSize(60, 152));

        verticalLayout_2->addWidget(pushButton_17);

        pushButton_13 = new QPushButton(centralwidget);
        pushButton_13->setObjectName(QString::fromUtf8("pushButton_13"));
        pushButton_13->setMinimumSize(QSize(0, 60));
        pushButton_13->setMaximumSize(QSize(60, 60));

        verticalLayout_2->addWidget(pushButton_13);


        horizontalLayout->addLayout(verticalLayout_2);


        gridLayout_2->addLayout(horizontalLayout, 2, 0, 1, 1);

        MainWindow->setCentralWidget(centralwidget);

        retranslateUi(MainWindow);

        QObject::connect( pushButton , SIGNAL (clicked()), this , handleButton );

        //this->handleButton(); 
      } // setupU

void Ui_MainWindow::retranslateUi(QMainWindow *MainWindow)
{
        MainWindow->setWindowTitle(QApplication::translate("MainWindow", "DD | Calc", 0, QApplication::UnicodeUTF8));
        label_2->setText(QApplication::translate("MainWindow", "DD - Calc", 0, QApplication::UnicodeUTF8));
        label->setText(QApplication::translate("MainWindow", "0", 0, QApplication::UnicodeUTF8));
        pushButton->setText(QApplication::translate("MainWindow", "1", 0, QApplication::UnicodeUTF8));
        pushButton_2->setText(QApplication::translate("MainWindow", "2", 0, QApplication::UnicodeUTF8));
        pushButton_3->setText(QApplication::translate("MainWindow", "3", 0, QApplication::UnicodeUTF8));
        pushButton_4->setText(QApplication::translate("MainWindow", "4", 0, QApplication::UnicodeUTF8));
        pushButton_5->setText(QApplication::translate("MainWindow", "5", 0, QApplication::UnicodeUTF8));
        pushButton_6->setText(QApplication::translate("MainWindow", "6", 0, QApplication::UnicodeUTF8));
        pushButton_7->setText(QApplication::translate("MainWindow", "7", 0, QApplication::UnicodeUTF8));
        pushButton_8->setText(QApplication::translate("MainWindow", "8", 0, QApplication::UnicodeUTF8));
        pushButton_9->setText(QApplication::translate("MainWindow", "9", 0, QApplication::UnicodeUTF8));
        pushButton_15->setText(QApplication::translate("MainWindow", "=", 0, QApplication::UnicodeUTF8));
        pushButton_10->setText(QApplication::translate("MainWindow", "0", 0, QApplication::UnicodeUTF8));
        pushButton_12->setText(QApplication::translate("MainWindow", "-", 0, QApplication::UnicodeUTF8));
        pushButton_11->setText(QApplication::translate("MainWindow", "+", 0, QApplication::UnicodeUTF8));
        pushButton_14->setText(QApplication::translate("MainWindow", "/", 0, QApplication::UnicodeUTF8));
        pushButton_17->setText(QApplication::translate("MainWindow", "C", 0, QApplication::UnicodeUTF8));
        pushButton_13->setText(QApplication::translate("MainWindow", "x", 0, QApplication::UnicodeUTF8));
    } // retranslateUi

FILE: window.h

#ifndef WINDOW_H
#define WINDOW_H

#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QGridLayout>
#include <QtGui/QHBoxLayout>
#include <QtGui/QHeaderView>
#include <QtGui/QLabel>
#include <QtGui/QMainWindow>
#include <QtGui/QPushButton>
#include <QtGui/QVBoxLayout>
#include <QtGui/QWidget>
#include <iostream>

QT_BEGIN_NAMESPACE

class Ui_MainWindow
{

private slots:
    void handleButton();

public:

    QWidget *centralwidget;
    QGridLayout *gridLayout_2;
    QLabel *label_2;
    QLabel *label;
    QHBoxLayout *horizontalLayout;
    QGridLayout *gridLayout;
    QPushButton *pushButton;
    QPushButton *pushButton_2;
    QPushButton *pushButton_3;
    QPushButton *pushButton_4;
    QPushButton *pushButton_5;
    QPushButton *pushButton_6;
    QPushButton *pushButton_7;
    QPushButton *pushButton_8;
    QPushButton *pushButton_9;
    QVBoxLayout *verticalLayout;
    QPushButton *pushButton_15;
    QPushButton *pushButton_10;
    QVBoxLayout *verticalLayout_3;
    QPushButton *pushButton_12;
    QPushButton *pushButton_11;
    QPushButton *pushButton_14;
    QVBoxLayout *verticalLayout_2;
    QPushButton *pushButton_17;
    QPushButton *pushButton_13;

    void setupUi(QMainWindow *MainWindow);
    void retranslateUi(QMainWindow *MainWindow);
};




// namespace Ui {
//     class MainWindow: public Ui_MainWindow {};
// } // namespace Ui


QT_END_NAMESPACE

#endif // WINDOW_H

FILE: main.cpp

#include <QApplication>
#include <QtCore/QVariant>
#include <iostream>
#include <window.h>

int main(int argc, char **argv)
{
 QApplication app (argc, argv);

 QMainWindow window;

 Ui_MainWindow win;

 win.setupUi(&window);

 window.show();

 return app.exec();
}
Ubdus Samad
  • 1,218
  • 1
  • 15
  • 27

1 Answers1

1

The correct syntax for connect is this:

QObject::connect( pushButton , SIGNAL (clicked()), this , SLOT(handleButton()));

or the new function pointers syntax (in Qt5):

QObject::connect( pushButton , &QPushButton::clicked, this , Ui_MainWindow::handleButton);

But you are doing something wrong there, you are not supposed to modify the class UI_FormName, because that file will most likely be regenerated when you modify your .ui file in Designer, check the documentation here on how you can use code generated by uic in your code.

LE: Thank you @MathiasSchmid I didn't noticed that macro is missing, Q_OBJECT macro needs to be added in the private section of your class for signals and slots to work.

Zlatomir
  • 6,964
  • 3
  • 26
  • 32
  • I tried this: `QObject::connect( pushButton , &QPushButton::clicked(), this , Ui_MainWindow::handleButton);` But i got this error: `error: cannot call member function ‘void QAbstractButton::clicked(bool)’ without object QObject::connect( pushButton , &QPushButton::clicked(), this , Ui_MainWindow::handleButton);` – Ubdus Samad Oct 12 '19 at 18:03
  • 2
    The `Q_OBJECT` macro is missing in class definition `Ui_MainWindow`. And `UI_MainWindow` must be derived from `QObject` or another class which is derived from `QObject` then it works. – Mathias Schmid Oct 12 '19 at 18:04
  • Okay so i derived the class `Ui` class from `QObject` , and the code does compiles but now i get the error in runtime which is : `Object::connect: No such slot QObject::handleButton()` – Ubdus Samad Oct 12 '19 at 18:41
  • 1
    You need the `Q_OBJECT` macro to resolve the signal-slot connection at runtime. Just place `Q_OBJECT` as first line at your class definition of Ui_MainWindow. – Mathias Schmid Oct 12 '19 at 19:38
  • More intel on QOBJECT macro [here](https://stackoverflow.com/questions/1368584/what-does-the-q-object-macro-do-why-do-all-qt-objects-need-this-macro/50197883). – Ubdus Samad Oct 13 '19 at 03:54
  • @Zlatomir Thanks, even i think that too. I tried adding arguments to the `handlebutton` function and i get the same error again. – Ubdus Samad Oct 13 '19 at 07:54
  • Well for arguments you need the signal to have arguments too, clicked has an overload with bool, if that is not what you want, you need to define your own signals and slots you need, else the connection can't guess the arguments you want to pass to your slot. So if you want the bool you can do this: _QObject::connect( pushButton , SIGNAL (clicked(bool)), this , SLOT(handleButton(bool)));_ //notice that only the type is passed in the connect – Zlatomir Oct 13 '19 at 08:04
  • You can read more about signals and slots in the documentation: https://doc.qt.io/archives/qt-4.8/signalsandslots.html#signals – Zlatomir Oct 13 '19 at 08:06
  • I forgot to mention the QPushButton needs to be checkable, that is what the bool argument is for (added this just for "completion", because i don't think that is what you are looking for) – Zlatomir Oct 13 '19 at 08:10