why doesn't the button work?
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
Code:
#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
spinForm.h
Code:
#include "ui_spinForm.h"
class spinForm
: public QWidget,
public Ui
::spinForm{
Q_OBJECT
public:
private slots:
void setSpinBoxValue();
private:
Ui::spinForm ui;
};
spinForm.cpp
Code:
#include <QtGui>
#include "spinForm.h"
{
connect(addPushButton, SIGNAL(clicked()), this, SLOT(setSpinBoxValue()));
ui.setupUi(this);
}
void spinForm::setSpinBoxValue()
{
spinBox->setValue(45);
}
main.cpp
Code:
#include <QApplication>
#include "ui_spinForm.h"
int main(int argc, char *argv[])
{
Ui::spinForm ui;
ui.setupUi(widget);
widget->show();
return app.exec();
}
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 ;)
Re: why doesn't the button work?
First, you are mixing aproaches.
Either use the 'ui' as member OR derive it.
I recommend using it as member - more flexible.
Try:
Code:
#include "ui_spinForm.h"
{
Q_OBJECT
public:
private slots:
void setSpinBoxValue();
private:
Ui::spinForm ui;
};
Code:
#include <QtGui>
#include "spinForm.h"
{
ui.setupUi(this);
connect(ui.addPushButton, SIGNAL(clicked()), this, SLOT(setSpinBoxValue()));
}
void spinForm::setSpinBoxValue()
{
ui.spinBox->setValue(45);
}
Re: why doesn't the button work?
I did it, and I realized that i was mixing aproaches.
I don't have any errors when i compile the project, but the button doesn't work...thx
Re: why doesn't the button work?
run it from a console, and see if you are getting any warnings
Re: why doesn't the button work?
This is the output from my make command:
Code:
$make:
g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/local/Trolltech/Qt-4.3.1/mkspecs/linux-g++ -I. -I/usr/local/Trolltech/Qt-4.3.1/include/QtCore -I/usr/local/Trolltech/Qt-4.3.1/include/QtCore -I/usr/local/Trolltech/Qt-4.3.1/include/QtGui -I/usr/local/Trolltech/Qt-4.3.1/include/QtGui -I/usr/local/Trolltech/Qt-4.3.1/include -I. -I. -I. -o main.o main.cpp
g++ -Wl,-rpath,/usr/local/Trolltech/Qt-4.3.1/lib -o spinForm main.o spinForm.o moc_spinForm.o -L/usr/local/Trolltech/Qt-4.3.1/lib -lQtGui -L/usr/local/Trolltech/Qt-4.3.1/lib -L/usr/X11R6/lib -lpng -lSM -lICE -pthread -pthread -lXi -lXrender -lXrandr -lXfixes -lXcursor -lXinerama -lfreetype -lfontconfig -lXext -lX11 -lQtCore -lz -lm -pthread -lgthread-2.0 -lglib-2.0 -lrt -ldl -lpthread
and i attach my .pro file too:
Code:
TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .
# Input
HEADERS += spinForm.h
FORMS += spinForm.ui
SOURCES += main.cpp spinForm.cpp
Re: why doesn't the button work?
try changing setSpinBoxValue() from private slot to public slot!
Re: why doesn't the button work?
what hgedek said is correct.
And I didn't ask for compilation output but that you RUN the program in a console.
Re: why doesn't the button work?
i've changed it to public but i have some error:
Code:
spinForm.
cpp: In constructor ‘spinForm
::spinForm(QWidget*)’
:spinForm.cpp:9: error: ‘addPushButton’ was not declared in this scope
spinForm.cpp: In member function ‘void spinForm::setSpinBoxValue()’:
spinForm.cpp:15: error: ‘spinBox’ was not declared in this scope
make: *** [spinForm.o] Error 1
Re: why doesn't the button work?
Quote:
Originally Posted by
mattia
i've changed it to public but i have some error:
Code:
spinForm.
cpp: In constructor ‘spinForm
::spinForm(QWidget*)’
:spinForm.cpp:9: error: ‘addPushButton’ was not declared in this scope
spinForm.cpp: In member function ‘void spinForm::setSpinBoxValue()’:
spinForm.cpp:15: error: ‘spinBox’ was not declared in this scope
make: *** [spinForm.o] Error 1
Actually, it doesn't matter whether it's public or not. That only affects the visibility when the slot is called as a normal function. However, looks like you still have mixed up "single inheritance" and "multiple inheritance" approaches. Take a closer look at second post in this thread.
Re: why doesn't the button work?
yes, you are right, I had omit the ui. before the object declered into ui_spinForm.h.
Now i've added them but i can't set any value into the spinBox, maybe i've to set the range?
Re: why doesn't the button work?
I've added into Ui_spinForm.h a spinBox->setValue(5); and when i start the widget the value is 5. I've added ui.spinBox->setValue(5); into the spinForm.cpp constructor
Code:
{
ui.setupUi(this);
connect(ui.addPushButton, SIGNAL(clicked()), this, SLOT(setSpinBoxValue()));
ui.spinBox->setValue(3);
}
but nothing...
Re: why doesn't the button work?
mmm...i added a textBrowser to see if it works, but in the spinForm.cpp i can't add any text, no errors, but it doesn't work....:crying:
Re: why doesn't the button work?
post your full code, and run the application in a console and see if you get any warnings.
Re: why doesn't the button work?
ui_spinForm.h
Code:
#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/QTextBrowser>
#include <QtGui/QWidget>
class Ui_spinForm
{
public:
{
if (spinForm->objectName().isEmpty())
spinForm
->setObjectName
(QString::fromUtf8("spinForm"));
spinForm->resize(359, 370);
layoutWidget
= new QWidget(spinForm
);
layoutWidget
->setObjectName
(QString::fromUtf8("layoutWidget"));
layoutWidget
->setGeometry
(QRect(60,
10,
133,
29));
hboxLayout
->setObjectName
(QString::fromUtf8("hboxLayout"));
hboxLayout->setContentsMargins(0, 0, 0, 0);
spinBox
->setObjectName
(QString::fromUtf8("spinBox"));
spinBox->setValue(5);
hboxLayout->addWidget(spinBox);
addPushButton
->setObjectName
(QString::fromUtf8("addPushButton"));
hboxLayout->addWidget(addPushButton);
textBrowser
->setObjectName
(QString::fromUtf8("textBrowser"));
textBrowser
->setGeometry
(QRect(40,
70,
256,
192));
retranslateUi(spinForm);
} // setupUi
void retranslateUi
(QWidget *spinForm
) {
Q_UNUSED(spinForm);
} // retranslateUi
};
namespace Ui {
class spinForm: public Ui_spinForm {};
} // namespace Ui
#endif // UI_SPINFORM_H
spinForm.h
Code:
#include "ui_spinForm.h"
{
Q_OBJECT
public:
public slots:
void setSpinBoxValue();
private:
Ui::spinForm ui;
};
spinForm.cpp
Code:
#include <QtGui>
#include "spinForm.h"
{
ui.setupUi(this);
connect(ui.addPushButton, SIGNAL(clicked()), this, SLOT(setSpinBoxValue()));
ui.spinBox->setValue(3);
}
void spinForm::setSpinBoxValue()
{
ui.spinBox->setValue(45);
}
main.cpp
Code:
#include <QApplication>
#include "ui_spinForm.h"
int main(int argc, char *argv[])
{
Ui::spinForm ui;
ui.setupUi(widget);
widget->show();
return app.exec();
}
what can i do to run it in a consolle? is it helpful to see the run-time error?
cos now i'm using QT Designer and a simple text editor.
Thanks
Re: why doesn't the button work?
I imported the project into Eclipse with QT library plug-in but when i make to run the application nothing is shown in consolle.
Re: why doesn't the button work?
In your main() you instantiate Ui::spinform not your own class spinform.
Your code is never executed -> that's why you don't see anything!
Re: why doesn't the button work?
Quote:
Originally Posted by
DeepDiver
In your main() you instantiate Ui::spinform not your own class spinform.
Your code is never executed -> that's why you don't see anything!
Thanks for the hint, i've chenged my main.cpp in this way:
Code:
#include <QApplication>
#include "spinForm.h"
int main(int argc, char *argv[])
{
spinForm ui(widget);
//ui.setupUi(widget);
widget->show();
return app.exec();
}
and now it run, thanks to everybody...it was my first program with a slot and signal ;)
Re: why doesn't the button work?
Quote:
Originally Posted by
high_flyer
First, you are mixing aproaches.
Either use the 'ui' as member OR derive it.
I recommend using it as member - more flexible.
I'd like to know how's the derive approach, are there any examples on the net?
Thanks
Re: why doesn't the button work?
Its all in the docs.
Note that you can use the single or multiple inheritance approaches, see what best fits your case.