PDA

View Full Version : problem with DoubleSpinBox



rimie23
8th June 2012, 20:00
Hi,
i want when i finished entred position (numbers) in 3 doublespinbox
it show to me an object
for that

i make like that, i define class


Coordinate3dWidget::Coordinate3dWidget(QWidget *parent):
QWidget(parent)
{

pos_cam = new QGroupBox(this);
pos_cam->setObjectName(QString::fromUtf8("pos_cam"));
pos_cam->setGeometry(QRect(10, 50, 191, 71));
pos_cam->setTitle("Position");

sbx_cam = new QDoubleSpinBox(pos_cam);
sbx_cam->setObjectName(QString::fromUtf8("sbx_cam"));
sbx_cam->setGeometry(QRect(10, 40, 51, 22));
sbx_cam->setRange(-5000, 5000);
lx_cam = new QLabel(pos_cam);
lx_cam->setObjectName(QString::fromUtf8("lx_cam"));
lx_cam->setGeometry(QRect(10, 20, 46, 13));
lx_cam->setText(QApplication::translate("MainWindow", "X :", 0, QApplication::UnicodeUTF8));

sby_cam = new QDoubleSpinBox(pos_cam);
sby_cam->setObjectName(QString::fromUtf8("sby_cam"));
sby_cam->setGeometry(QRect(70, 40, 51, 22));
sby_cam->setRange(-5000, 5000);
ly_cam = new QLabel(pos_cam);
ly_cam->setObjectName(QString::fromUtf8("ly_cam"));
ly_cam->setGeometry(QRect(70, 20, 46, 13));
ly_cam->setText(QApplication::translate("MainWindow", "Y :", 0, QApplication::UnicodeUTF8));
sbz_cam = new QDoubleSpinBox(pos_cam);
sbz_cam->setObjectName(QString::fromUtf8("sbz_cam"));
sbz_cam->setGeometry(QRect(130, 40, 51, 22));
sbz_cam->setRange(-5000, 5000);
lz_cam = new QLabel(pos_cam);
lz_cam->setObjectName(QString::fromUtf8("lz_cam"));
lz_cam->setGeometry(QRect(130, 20, 46, 13));
lz_cam->setText(QApplication::translate("MainWindow", "Z :", 0, QApplication::UnicodeUTF8));

connect(sbx_cam, SIGNAL(editingFinished()), this, SLOT(onCoordinateChanged()));
connect(sby_cam, SIGNAL(editingFinished()), this, SLOT(onCoordinateChanged()));
connect(sbz_cam, SIGNAL(editingFinished()), this, SLOT(onCoordinateChanged()));
}

but it dos not work , it add to me 3 object (when i enter value in sbx_cam it add to me an object , when i enter value in sby_cam it add to me another !!!!!!!!!!!)
i want when i the editingFinished() of the 3 DoubleSpinBox i add the object not when i finished one

wysota
8th June 2012, 21:28
Do yourself a favour and read about using layouts.

rimie23
8th June 2012, 22:04
layout!! why layout ?

rimie23
8th June 2012, 22:10
Hi
i do not use layout her
i just use groupbox

wysota
8th June 2012, 22:42
i do not use layout her
Yes, I can see that.


i just use groupbox
No, you don't. And even if you did, you'd still need layouts.

As a test, use this main() with your Coordinate3dWidget class:


int main(int argc, char **argv) {
QApplication app(argc, argv);
Coordinate3dWidget w;
QFont f;
f.setPointSize(72);
w.setFont(f);
w.show();
Coordinate3dWidget w2;
f.setPointSize(4);
w2.setFont(f);
w2.show();
return app.exec();
}

How does it look? I hope this answers rimie23's question as well. Conclusions are yours to draw.

rimie23
8th June 2012, 23:15
i don't undertand what you want by this exemple

maybe you do not understand where is my problem exactelly

because i make in my mainwindow like that


ogreWidget = new OgreWidget(this);
setCentralWidget(ogreWidget);
page_14 = new Coordinate3dWidget;
onnect(page_14, SIGNAL(coordinateChanged(const Ogre::Vector3&)),
ogreWidget, SLOT(setCameraPosition(const Ogre::Vector3&)));
connect(ogreWidget, SIGNAL(cameraPositionChanged(const Ogre::Vector3&)),
page_14, SLOT(setNewCoordinate(const Ogre::Vector3&)));

that mean i related it where i draw the object

it just do net let me finishe entring in the three spinbox

wysota
8th June 2012, 23:24
i don't undertand what you want by this exemple
Maybe if you ran it, you'd understand.


maybe you do not understand where is my problem exactelly
That's true, I don't, because you are describing your problem and then showing some code totally unrelated to that (and doing that again in this post).

Since I can understand the code better than your words, I talk about problems with your code hoping (in vain, as we can see) that either you show some related code next time or fixing the problem I see will somehow influence your primary problem or at least the description of it. Currently your English is so bad I can't make up what you are talking about. I can only guess that you want a slot to be called after all three of the boxes are changed and not after each one gets a value but since you didn't show any code related to that, I'm just left with my guesses.

rimie23
8th June 2012, 23:41
I am so sorry
i know that my english is bad (it's not my native language )

. I can only guess that you want a slot to be called after all three of the boxes are changed and not after each one gets a value but since you didn't show any code related to that, I'm just left with my guesses.
yes that what i want ,i want "add object" when i enter numbers in the three boxes .

I will give you all the code
in my class Coordinate3dWidget.h i have like that


class Coordinate3dWidget : public QWidget
{
Q_OBJECT
public:
Coordinate3dWidget(QWidget *parent=0);
public slots:
void setNewCoordinate(const Ogre::Vector3 &coordinate);
signals:
void coordinateChanged(const Ogre::Vector3 &coordinate);
private slots:
void onCoordinateChanged();
private:

QDoubleSpinBox *sbx_cam;
QLabel *lx_cam;
QLabel *ly_cam;
QDoubleSpinBox *sby_cam;
QDoubleSpinBox *sbz_cam;
QLabel *lz_cam;
QGroupBox *pos_cam;

};

in Coordinate3dWidget.cpp


#include <QLabel>
#include <QHBoxLayout>
#include <QToolButton>
#include <QPushButton>

#include "coordinate3dwidget.h"
#include"my_interface.h"
//class MainWindow;
Coordinate3dWidget::Coordinate3dWidget(QWidget *parent):
QWidget(parent)
{

pos_cam = new QGroupBox(this);
pos_cam->setObjectName(QString::fromUtf8("pos_cam"));
pos_cam->setGeometry(QRect(10, 50, 191, 71));
pos_cam->setTitle("Position");

sbx_cam = new QDoubleSpinBox(pos_cam);
sbx_cam->setObjectName(QString::fromUtf8("sbx_cam"));
sbx_cam->setGeometry(QRect(10, 40, 51, 22));
sbx_cam->setRange(-5000, 5000);
lx_cam = new QLabel(pos_cam);
lx_cam->setObjectName(QString::fromUtf8("lx_cam"));
lx_cam->setGeometry(QRect(10, 20, 46, 13));
lx_cam->setText(QApplication::translate("MainWindow", "X :", 0, QApplication::UnicodeUTF8));

sby_cam = new QDoubleSpinBox(pos_cam);
sby_cam->setObjectName(QString::fromUtf8("sby_cam"));
sby_cam->setGeometry(QRect(70, 40, 51, 22));
sby_cam->setRange(-5000, 5000);
ly_cam = new QLabel(pos_cam);
ly_cam->setObjectName(QString::fromUtf8("ly_cam"));
ly_cam->setGeometry(QRect(70, 20, 46, 13));
ly_cam->setText(QApplication::translate("MainWindow", "Y :", 0, QApplication::UnicodeUTF8));
sbz_cam = new QDoubleSpinBox(pos_cam);
sbz_cam->setObjectName(QString::fromUtf8("sbz_cam"));
sbz_cam->setGeometry(QRect(130, 40, 51, 22));
sbz_cam->setRange(-5000, 5000);
lz_cam = new QLabel(pos_cam);
lz_cam->setObjectName(QString::fromUtf8("lz_cam"));
lz_cam->setGeometry(QRect(130, 20, 46, 13));
lz_cam->setText(QApplication::translate("MainWindow", "Z :", 0, QApplication::UnicodeUTF8));

connect(sbx_cam, SIGNAL(/*valueChanged*/editingFinished()), this, SLOT(onCoordinateChanged()));
connect(sby_cam, SIGNAL(/*valueChanged*/editingFinished()), this, SLOT(onCoordinateChanged()));
connect(sbz_cam, SIGNAL(/*valueChanged*/editingFinished()), this, SLOT(onCoordinateChanged()));
}

void Coordinate3dWidget::setNewCoordinate(const Ogre::Vector3 &coordinate)
{
blockSignals(true);
sbx_cam->setValue(coordinate.x);
sby_cam->setValue(coordinate.y);
sbz_cam->setValue(coordinate.z);
blockSignals(false);
}

void Coordinate3dWidget::onCoordinateChanged()
{
Ogre::Vector3 newCoord(sbx_cam->value(),
sby_cam->value(),
sbz_cam->value());
emit coordinateChanged(newCoord);
}


and in my mainwindow


MainWindow::MainWindow()
{
ogreWidget = new OgreWidget(this);
setCentralWidget(ogreWidget);
createDockWidget();

}
void MainWindow::createDockWidget()
{
page_14 = new Coordinate3dWidget;
page_14->setGeometry(QRect(0, 0, 211, 250));
connect(page_14, SIGNAL(coordinateChanged(const Ogre::Vector3&)),
ogreWidget, SLOT(setCameraPosition(const Ogre::Vector3&)));
connect(ogreWidget, SIGNAL(cameraPositionChanged(const Ogre::Vector3&)),
page_14, SLOT(setNewCoordinate(const Ogre::Vector3&)));


in my class OgreWidget i defined slot


public slots:
void setCameraPosition(const Ogre::Vector3 &pos);



in setCameraPosition: when i entre the numbers in the three boxes i must execute this slot to add object in the position entred
void QOgreWidget::setCameraPosition(const Ogre::Vector3 &pos)
{

CamNode= mSceneMgr->getRootSceneNode()->createChildSceneNode();
ent_cam = mSceneMgr->createEntity( Ogre::SceneManager:: PT_CUBE);
Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().create("green", "General");
material->getTechnique( 0 )->getPass( 0 )->setAmbient(1, 1, 0);
ent_cam->setMaterial(material);
mSceneMgr->getRootSceneNode()->createChildSceneNode();
CamNode->setPosition(pos);
CamNode->attachObject(ent_cam);


}

wysota
9th June 2012, 09:00
Ok, now the situation is clear.

First of all please do consider using layouts in your widget and derive your widget from QGroupBox and not QWidget.


Coordinate3dWidget::Coordinate3dWidget(QWidget *parent):
QGroupBox(parent)
{
setTitle("Position");
QGridLayout *l = new QGridLayout(this);
lx_cam = new QLabel;
sbx_cam = new QDoubleSpinBox;
sbx_cam->setRange(-5000, 5000);

lx_cam->setText(tr("X"));
l->addWidget(lx_cam, 0,0);
l->addWidget(sbx_cam, 1, 0);

ly_cam = new QLabel;
sby_cam = new QDoubleSpinBox;
sby_cam->setRange(-5000, 5000);
ly_cam->setText(tr("Y"));
l->addWidget(ly_cam, 0,1);
l->addWidget(sby_cam, 1, 1);

lz_cam = new QLabel;
sbz_cam = new QDoubleSpinBox;
sbz_cam->setRange(-5000, 5000);
lz_cam->setText(tr("Z"));
l->addWidget(lx_cam, 0,2);
l->addWidget(sbz_cam, 1, 2);

connect(sbx_cam, SIGNAL(valueChanged(double)), this, SLOT(onCoordinateChanged()));
connect(sby_cam, SIGNAL(valueChanged(double)), this, SLOT(onCoordinateChanged()));
connect(sbz_cam, SIGNAL(valueChanged(double)), this, SLOT(onCoordinateChanged()));
}

Isn't that clearer and doesn't it work better?

Now for your main problem... here is how your "onCoordinateChanged()" which looks like:

void Coordinate3dWidget::onCoordinateChanged()
{
Ogre::Vector3 newCoord(sbx_cam->value(),
sby_cam->value(),
sbz_cam->value());
emit coordinateChanged(newCoord);
}

works:
1. create a new coordinate vector
2. emit the vector

Now, it should look like this:
1. if sbx_cam, sby_cam, sbz_cam don't have a valid value, return
2. otherwise create a coordinate vector
3. and emit it

The bad thing is I have no idea how you intend to determine that a value is not valid for you. In my opinion every real value is valid so you need a separate button outside your widget to accept the value entered in those three boxes together.
I would also suggest that you provide a method for reading the current value of the coordinate vector instead of only emitting a signal with it.

rimie23
9th June 2012, 16:36
i tried the function with layout
it's good

rimie23
9th June 2012, 16:46
i resolved the problem by
1-adding the notion of flag for each spinbox
2-i store when i execute my program the last value entred