Qpushbutton draws OpenGL problem
-
I'm a newbie to programming, learning by myself to use Qt with OpenGL. I made a button (button1) that just draws an OpenGL object(say object 1), then an Undo button that re-draws the scene without object 1. Now I want to improve on it in such a way that when I click button 1, it draws object 1, and if I click button 1 again, it draws another object 2. And also, the undo button also undo the last draw action.
Please I need help. I'm a beginner -
You're diving into the deep end here, learning to program with Qt and OpenGL when you're new to programming! However, I suggest you start learning about container classes such as std::vector, std::list, and [[Doc:QList]]: what you likely want to do is create a new object every time the user presses your button, and add it to a list of objects, which then get drawn. When the user clicks "undo" it simply removes the last item from the list and redraws.
-
In my code I use std::vector and create a base object class that has a virtual Render() call. I then subclass this base class for meshes and primitives. You would then in the main part of your application create a vector to hold these items.
std::vector<MyDrawClass*> m_objects;
as the application needs to queue objects to be rendered, you would do something like:
m_objects.push_back(new Rectangle()); // Rectangle is a sub-class of MyDrawClass
Then when you are ready to draw them you would do something like:
@
void RenderScene() {std::vector<MyDrawClass*>::iterator it;
for(it = m_objects.begin() ; it != m_objects.end; ++it) (*it)->Render();}
@And given this example where I'm calling new (I actually use a model library that manages creation/destruction of objects as well as instancing.) when you are done though.
@myApp::~myApp {
std::vector<MyDrawClass*>::iterator it;
for(it = m_objects.begin() ; it != m_objects.end; ++it) delete *it;
m_objects.clear();
}@