Question

In my university, we are working with interfaces, using QT Jambi (Java) with the Eclipse Integration. I would like to improve my design of my Elevator interface.

The main problem is that I would like to update the QLCDNumber with the floor in real-time. What I do to simulate the elevator working, is to do a Thread.sleep(1000) between 2 floors, so that way, my QLCDNumber will display "an animation" saying "1...2...3...4". The problem is that the QLCDNumber only displays 1 and 4, no animation.

So, for example (resumed), the code I made is this one:

private void simulate(int floor){
    while(actualFloor < floor){
        try{
           Thread.sleep(1000);
        }catch(InterruptedException e){};
    actualFloor++;
    ui.LCDfloor.display(actualFloor);   
    }
}

Why this code only shows the 1st floor and the last one? Sorry if you didn't understand what I wanted, my English is improving every day :)

Thank you in advance.

*Please note that LCDFloor is the name of the QLCDNumber widget

Was it helpful?

Solution

It looks like you have two problems:

  1. (I assume) You're calling Thread.sleep() on the GUI thread. In other words, when you call simulate, you're doing so on the same thread as the rest of the gui operations. This causes the entire gui to pause.

  2. You've never given Qt the chance to actually update the UI. When you call ui.LCDfloor.display(actualFloor), the a paint event is queued so that the UI can be updated, but rather than giving the UI a chance to actually execute the paint event, you continue with your loop which prevents the UI from ever being updated until after your simulation is finished.

You have two basic fixes:

  1. Don't sleep, it's poor design. Instead, use timer's and signals to simulate the changes.
  2. Force events to be processed using processEvents.

Also keep in mind that you can't update a GUI element from a non gui thread. And as this is homework, I'll leave the rest to you :).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top