[Programming a pushbutton to do a task]

Started by
6 comments, last by Thaumaturge 1 year, 9 months ago

Hello, I hope you're all doing well.

So I developed an application that rotates a cube using quaternions.

In my mainwindow.ui, I have 4 spin boxes, each one of them corresponds to the quaternion components ( w, x, y and z).

After inserting the components' values manually, the cube changes its orientation.

Now I want to add a push button "Apply rotation". So when I insert the quaternion components in the spin boxes, I should first click on the push button to apply the rotation and then see the cube changing its orientation.

In my code I have two classes:

MainWindow

Mainwidget

In my mainwidget.cpp, I have a method which is paintGL() and in which I render the whole scene and apply transformations on the cube.

To make the rotation possible using the push button I did this:

In mainwidow.cpp:

void MainWindow::on_pushButton_3_clicked()

{

this->ui->mainwidget->ApplyRotation = true;

}

In paintGL() in my mainwidget.cpp:

if(ApplyRotation == true)

{

EulerAngles.append(quaternion.toEulerAngles()); //rotation order is 213.

localMatrix.rotate(quaternion); // rotates the cube whenever I insert a quaternion and click on apply button

}

When I insert the quaternion components in the spin boxes, then click on the push button, the object changes its orientation.

However when I keep changing the quaternion components after the first attempt, the cube keeps rotating. I mean I no longer need to click on the push button again to apply the rotation after the first click on the push button.

So I did this:

//code

if(ApplyRotation == true)

{

ApplyRotation=false;

EulerAngles.append(quaternion.toEulerAngles()); //rotation order is 213.

localMatrix.rotate(quaternion);

//code

}

Now after inserting the quaternions components in the spin boxes, I click on the push button, the object changes its orientation immediately but goes back quickly to its initial orientation. I want it to stay at the new orientation.

I don't know what I'm doing wrong because the code seems logical.

My goal is to make the object rotate only if the push button is clicked.

Please if anyone can help me, I would really appreciate it.

ps: I'm using Qt and OpenGL

Thank you all.

Advertisement

Hmm… I don't see anything that would obviously cause the behaviour that you describe. Might there be code elsewhere in your program that either changes the euler-angles being stored (if I read your code correctly), or that calls “localMatrix.rotate”?

By the way, why not move the code that applies the rotation to be within the “button pushed” method? That would seem to simplify matters a little--but then, I don't know what other requirements your program might have.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

brackets are used for tags usually.

My project`s facebook page is “DreamLand Page”

Calin said:

brackets are used for tags usually.

Could you elaborate on what you mean here, please?

If you're referring to the original code, that appears to be C++ or the like, which uses brackets for things like function parameters and conditionals (as well as in maths).

If you're referring to my post, I simply used it for a parenthetical there.

Or are you referring to something that I'm missing?

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

Thaumaturge said:
Could you elaborate on what you mean here, please?

his thread title is wrapped in brackets, all of it. People usually use that type of brackets in the title for tags (a method to point out the category to which the thread belongs)

My project`s facebook page is “DreamLand Page”

You likely want to check when the value of “ApplyRotation” clearly it becomes ‘true’ at some point, but is it reset to false again somewhere? Is that code reached?

As a side-note, the condition C in “if (C) { … }” must be 'true' to execute the statements between the curly braces. Your value “ApplyRotation” is already true (or false). Comparing the value with true again just computes the same value that you already have. In other words you can simply write “if (ApplyRotation) { … }”, the “== true” part isn't doing anything useful.

Calin said:

his thread title is wrapped in brackets, all of it. People usually use that type of brackets in the title for tags (a method to point out the category to which the thread belongs)

Ah, you're quite right! I missed that entirely!

Thank you for clarifying. ^_^

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

This topic is closed to new replies.

Advertisement