Skip to content
  • Algorithm animation(QPainter)

    Solved General and Desktop algorith animat qpainter
    15
    0 Votes
    15 Posts
    4k Views
    Chris KawaC
    Ok, I think you're going deeper and deeper into the woods but it's a totally bad direction. So if I'm getting this right you are making some algorithm that does stuff (sorting?) to some items represented by rectangles and you want to visualize the steps. Is that right? In that case setting up hardcoded timers and single shots is going against the wind because you're effectively tying to predict the whole thing and set timers for it. That's just gonna give you a headache and you won't know what you did the next day. Step back and look at the problem. You've got some algorithm that operates on an array. Conceptually: while(!done()) { doAnotherStep(); } Now you want to visualize the steps, conceptually: while(!done()) { doAnotherStep(); visualizeTheStep(); } The problem is that the visualization should take time, so this approach is not gonna work. What you need to do is make it event driven, i.e. conceptually: stepEnded -> visualize visualizationEnded -> done ? exit : doAnotherStep For this you can use the signal/slot mechanism of Qt. Make a class that does the algorithm and has a step method. At the end of the method emit a signal. Make another class for visualization and give it a visualize method. At the end of it emit a signal. Then just connect the two. Rough concept: class Algo : public QObject { Q_OBJECT public: Algo(Vector<stuff>* data) : dataPtr(data) {} bool done() { return /* check if more steps needed */ } void doStep() { /* do stuff to dataPtr */; emit stepFinished(someStuffToSwap, someOtherStuffToSwap); } signal: void stepFinished(stuff* item1, stuff* item2); private: Vector<stuff>* dataPtr; }; class Vis : public QObject { Q_OBJECT public: Vis(Vector<stuff>* data) { /*create graphics items and whatnot */ } void visualizeStep(stuff* item1, stuff* item2) { /* do the animations here and emit finished() when they're done*/ } signals: void finished(); }; //and then you can use it like this: Vector<stuff> data = ... Algo algo(&data); Vis vis(&data); connect(algo, &Algo::stepFinished, vis, &Vis::visualizeStep); connect(vis, &Vis::finished(), [&]{ if(!algo.done()) algo.doStep(); }); algo.doStep(); /start the first step