Hi, i've created a widget with a button and a spinBox, i want to set a value in the spinBox when i'm going to click on the button. Down here the code.
ui_spinForm.h --> created with QT Designer
#ifndef UI_SPINFORM_H
#define UI_SPINFORM_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QSpinBox>
#include <QtGui/QWidget>
class Ui_spinForm
{
public:
{
if (spinForm->objectName().isEmpty())
spinForm
->setObjectName
(QString::fromUtf8("spinForm"));
spinForm->resize(258, 52);
widget
->setObjectName
(QString::fromUtf8("widget"));
widget
->setGeometry
(QRect(60,
10,
133,
29));
hboxLayout
->setObjectName
(QString::fromUtf8("hboxLayout"));
hboxLayout->setContentsMargins(0, 0, 0, 0);
spinBox
->setObjectName
(QString::fromUtf8("spinBox"));
hboxLayout->addWidget(spinBox);
addPushButton
->setObjectName
(QString::fromUtf8("addPushButton"));
hboxLayout->addWidget(addPushButton);
retranslateUi(spinForm);
} // setupUi
void retranslateUi
(QWidget *spinForm
) {
Q_UNUSED(spinForm);
} // retranslateUi
};
namespace Ui {
class spinForm: public Ui_spinForm {};
} // namespace Ui
#endif // UI_SPINFORM_H
#ifndef UI_SPINFORM_H
#define UI_SPINFORM_H
#include <QtCore/QVariant>
#include <QtGui/QAction>
#include <QtGui/QApplication>
#include <QtGui/QButtonGroup>
#include <QtGui/QHBoxLayout>
#include <QtGui/QPushButton>
#include <QtGui/QSpinBox>
#include <QtGui/QWidget>
class Ui_spinForm
{
public:
QWidget *widget;
QHBoxLayout *hboxLayout;
QSpinBox *spinBox;
QPushButton *addPushButton;
void setupUi(QWidget *spinForm)
{
if (spinForm->objectName().isEmpty())
spinForm->setObjectName(QString::fromUtf8("spinForm"));
spinForm->resize(258, 52);
widget = new QWidget(spinForm);
widget->setObjectName(QString::fromUtf8("widget"));
widget->setGeometry(QRect(60, 10, 133, 29));
hboxLayout = new QHBoxLayout(widget);
hboxLayout->setObjectName(QString::fromUtf8("hboxLayout"));
hboxLayout->setContentsMargins(0, 0, 0, 0);
spinBox = new QSpinBox(widget);
spinBox->setObjectName(QString::fromUtf8("spinBox"));
hboxLayout->addWidget(spinBox);
addPushButton = new QPushButton(widget);
addPushButton->setObjectName(QString::fromUtf8("addPushButton"));
hboxLayout->addWidget(addPushButton);
retranslateUi(spinForm);
QMetaObject::connectSlotsByName(spinForm);
} // setupUi
void retranslateUi(QWidget *spinForm)
{
spinForm->setWindowTitle(QApplication::translate("spinForm", "Try a spinbox", 0, QApplication::UnicodeUTF8));
addPushButton->setText(QApplication::translate("spinForm", "Add", 0, QApplication::UnicodeUTF8));
Q_UNUSED(spinForm);
} // retranslateUi
};
namespace Ui {
class spinForm: public Ui_spinForm {};
} // namespace Ui
#endif // UI_SPINFORM_H
To copy to clipboard, switch view to plain text mode
spinForm.h
#include "ui_spinForm.h"
class spinForm
: public QWidget,
public Ui
::spinForm{
Q_OBJECT
public:
private slots:
void setSpinBoxValue();
private:
Ui::spinForm ui;
};
#include "ui_spinForm.h"
class spinForm : public QWidget, public Ui::spinForm
{
Q_OBJECT
public:
spinForm(QWidget *parent = 0);
private slots:
void setSpinBoxValue();
private:
Ui::spinForm ui;
};
To copy to clipboard, switch view to plain text mode
spinForm.cpp
#include <QtGui>
#include "spinForm.h"
{
connect(addPushButton, SIGNAL(clicked()), this, SLOT(setSpinBoxValue()));
ui.setupUi(this);
}
void spinForm::setSpinBoxValue()
{
spinBox->setValue(45);
}
#include <QtGui>
#include "spinForm.h"
spinForm::spinForm(QWidget *parent):QWidget(parent)
{
connect(addPushButton, SIGNAL(clicked()), this, SLOT(setSpinBoxValue()));
ui.setupUi(this);
}
void spinForm::setSpinBoxValue()
{
spinBox->setValue(45);
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include <QApplication>
#include "ui_spinForm.h"
int main(int argc, char *argv[])
{
Ui::spinForm ui;
ui.setupUi(widget);
widget->show();
return app.exec();
}
#include <QApplication>
#include "ui_spinForm.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *widget = new QWidget;
Ui::spinForm ui;
ui.setupUi(widget);
widget->show();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
When i compile it i don't have any problems, i can lunch the widget but when i click on the button nothing happen.
I've tried to put spinBox->setValue(45) in the constructor too...
I've checked the code a lot of time but i haven't catch any errors, maybe cos i'm a newbie!
Thanks ppl for your support
Bookmarks