I have created the form widget(not dialog) using the Qt Designer and then add all the necessary labels and spin boxes to interact with them.
Then i executed the qmake -project to create the header file from the .ui file. And inside the header file i can see that there is a function created that expects a (setupUi(QDialog*)) QDialog as a parameter.
To use that component using single inheritance approach i created a class CalculatorForm which is the subclass of QWidget and there is where i get the error saying:
Not a good idea though paste such a chunk of code in the forum..........
***********************CalculatorForm.h*********** **********
#ifndef CALCULATORFORM_H
#define CALCULATORFORM_H
#include "ui_calculatorform.h"
class CalculatorForm
: public QWidget {
Q_OBJECT
public:
CalculatorForm
(QWidget *parent
= 0);
private slots:
void on_inputSpinBox1_valueChanged(int value);
void on_inputSpinBox2_valueChanged(int value);
private:
Ui::CalculatorForm ui;
};
#endif
#ifndef CALCULATORFORM_H
#define CALCULATORFORM_H
#include "ui_calculatorform.h"
class CalculatorForm : public QWidget
{
Q_OBJECT
public:
CalculatorForm(QWidget *parent = 0);
private slots:
void on_inputSpinBox1_valueChanged(int value);
void on_inputSpinBox2_valueChanged(int value);
private:
Ui::CalculatorForm ui;
};
#endif
To copy to clipboard, switch view to plain text mode
***********************CalculatorForm.h*********** **********
***********************CalculatorForm.cpp********* ************
#include <QtGui>
#include "CalculatorForm.h"
CalculatorForm
::CalculatorForm(QWidget *parent
) {
ui.setupUi(this);
}
void CalculatorForm::on_inputSpinBox1_valueChanged(int value)
{
ui.
outputWidget->setText
(QString::number(value
+ ui.
inputSpinBox2->value
()));
}
void CalculatorForm::on_inputSpinBox2_valueChanged(int value)
{
ui.
outputWidget->setText
(QString::number(value
+ ui.
inputSpinBox1->value
()));
}
#include <QtGui>
#include "CalculatorForm.h"
CalculatorForm::CalculatorForm(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
void CalculatorForm::on_inputSpinBox1_valueChanged(int value)
{
ui.outputWidget->setText(QString::number(value + ui.inputSpinBox2->value()));
}
void CalculatorForm::on_inputSpinBox2_valueChanged(int value)
{
ui.outputWidget->setText(QString::number(value + ui.inputSpinBox1->value()));
}
To copy to clipboard, switch view to plain text mode
***********************CalculatorForm.cpp********* ************
sajjad@sajjad:~/QT/calculatorform$ make
g++ -c -pipe -fpermissive -g -Wall -W -D_REENTRANT -DQT_SHARED -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -o CalculatorForm.o CalculatorForm.cpp
In file included from CalculatorForm.cpp:3:
CalculatorForm.h:18: error: ‘CalculatorForm’ in namespace ‘Ui’ does not name a type
CalculatorForm.cpp: In constructor ‘CalculatorForm::CalculatorForm(QWidget*)â⠂¬â„¢:
CalculatorForm.cpp:8: error: ‘ui’ was not declared in this scope
CalculatorForm.cpp: In member function ‘void CalculatorForm::on_inputSpinBox1_valueChanged(int) ’:
CalculatorForm.cpp:13: error: ‘ui’ was not declared in this scope
CalculatorForm.cpp: In member function ‘void CalculatorForm::on_inputSpinBox2_valueChanged(int) ’:
CalculatorForm.cpp:18: error: ‘ui’ was not declared in this scope
make: *** [CalculatorForm.o] Error 1
I did not understand the error.
Really need some light into that issue.
Regards
Sajjad
Bookmarks