PDA

View Full Version : why doesn't the button work?



mattia
26th October 2007, 13:47
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:
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


spinForm.h


#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;
};


spinForm.cpp


#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);
}


main.cpp


#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();
}


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 ;)

high_flyer
26th October 2007, 13:58
First, you are mixing aproaches.
Either use the 'ui' as member OR derive it.
I recommend using it as member - more flexible.
Try:


#include "ui_spinForm.h"

class spinForm : public QWidget
{
Q_OBJECT
public:
spinForm(QWidget *parent = 0);
private slots:
void setSpinBoxValue();
private:
Ui::spinForm ui;
};




#include <QtGui>
#include "spinForm.h"

spinForm::spinForm(QWidget *parent):QWidget(parent)
{
ui.setupUi(this);
connect(ui.addPushButton, SIGNAL(clicked()), this, SLOT(setSpinBoxValue()));

}

void spinForm::setSpinBoxValue()
{
ui.spinBox->setValue(45);

}

mattia
26th October 2007, 14:15
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

high_flyer
26th October 2007, 14:34
run it from a console, and see if you are getting any warnings

mattia
26th October 2007, 14:40
This is the output from my make command:


$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:

TEMPLATE = app
TARGET =
DEPENDPATH += .
INCLUDEPATH += .

# Input
HEADERS += spinForm.h
FORMS += spinForm.ui
SOURCES += main.cpp spinForm.cpp

hgedek
26th October 2007, 14:44
try changing setSpinBoxValue() from private slot to public slot!

high_flyer
26th October 2007, 14:47
what hgedek said is correct.
And I didn't ask for compilation output but that you RUN the program in a console.

mattia
26th October 2007, 14:51
i've changed it to public but i have some error:


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

jpn
26th October 2007, 14:57
i've changed it to public but i have some error:


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.

mattia
26th October 2007, 15:06
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?

mattia
26th October 2007, 15:14
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

spinForm::spinForm(QWidget *parent):QWidget(parent)
{
ui.setupUi(this);
connect(ui.addPushButton, SIGNAL(clicked()), this, SLOT(setSpinBoxValue()));
ui.spinBox->setValue(3);
}

but nothing...

mattia
26th October 2007, 15:43
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:

high_flyer
26th October 2007, 15:47
post your full code, and run the application in a console and see if you get any warnings.

mattia
26th October 2007, 16:03
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/QTextBrowser>
#include <QtGui/QWidget>

class Ui_spinForm
{
public:
QWidget *layoutWidget;
QHBoxLayout *hboxLayout;
QSpinBox *spinBox;
QPushButton *addPushButton;
QTextBrowser *textBrowser;

void setupUi(QWidget *spinForm)
{
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 = new QHBoxLayout(layoutWidget);
hboxLayout->setObjectName(QString::fromUtf8("hboxLayout"));
hboxLayout->setContentsMargins(0, 0, 0, 0);
spinBox = new QSpinBox(layoutWidget);
spinBox->setObjectName(QString::fromUtf8("spinBox"));
spinBox->setValue(5);

hboxLayout->addWidget(spinBox);

addPushButton = new QPushButton(layoutWidget);
addPushButton->setObjectName(QString::fromUtf8("addPushButton"));

hboxLayout->addWidget(addPushButton);

textBrowser = new QTextBrowser(spinForm);
textBrowser->setObjectName(QString::fromUtf8("textBrowser"));
textBrowser->setGeometry(QRect(40, 70, 256, 192));

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


spinForm.h


#include "ui_spinForm.h"

class spinForm : public QWidget
{
Q_OBJECT

public:
spinForm(QWidget *parent = 0);

public slots:
void setSpinBoxValue();

private:
Ui::spinForm ui;
};


spinForm.cpp


#include <QtGui>
#include "spinForm.h"

spinForm::spinForm(QWidget *parent):QWidget(parent)
{
ui.setupUi(this);
connect(ui.addPushButton, SIGNAL(clicked()), this, SLOT(setSpinBoxValue()));
ui.spinBox->setValue(3);

}

void spinForm::setSpinBoxValue()
{
ui.spinBox->setValue(45);
}


main.cpp


#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();
}


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

mattia
26th October 2007, 16:15
I imported the project into Eclipse with QT library plug-in but when i make to run the application nothing is shown in consolle.

DeepDiver
26th October 2007, 16:18
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!

mattia
26th October 2007, 16:27
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:


#include <QApplication>

#include "spinForm.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QWidget *widget = new QWidget;
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 ;)

mattia
5th November 2007, 12:11
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

high_flyer
5th November 2007, 12:14
Its all in the docs. (http://doc.trolltech.com/4.3/designer-using-a-component.html#the-single-inheritance-approach)
Note that you can use the single or multiple inheritance approaches, see what best fits your case.