Reputation: 1215
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,
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 aclicked
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();
}
Upvotes: 0
Views: 1040
Reputation: 7034
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.
Upvotes: 1