Results 1 to 5 of 5

Thread: QSlider Update Problem

  1. #1
    Join Date
    Sep 2006
    Posts
    68
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Question QSlider Update Problem

    Hi All,

    I am fairly new QT, having slowly moved from Windows to Linux, I am now re-writing one of my apps from scratch in QT with KDevelop.

    I have writing a program that controls stage / club lighting, and uses a MIDI controller as a tactile interface. In the main I have 8 QSliders that correspond to 8 physical sliders on the MIDI controller. I have a QThread that receives MIDI and then passes a QCustomEvent to the main window for appropriate MIDI messages.

    When I move a fader on the MIDI controller, my main form is correctly receiving the messages, but here I have run into a problem. When I use the values that the Event is passing to update the position of the slider.. things are pretty bad. The sliders seem to flicker a lot, and lag behind the physical faders by a fair bit.

    Thebit of code that updates the QSlider:
    Qt Code:
    1. if ( e->type() == 2001 ) { // It must be a DataEvent
    2. d_e = (MyDataEvent *) e;
    3. if( d_e->param() > 80 && d_e->param() < 89 ) { // Playback Slider
    4. slider = lPlaySliders.at( d_e->param() - 80 );
    5. if(slider->value() != d_e->value() )
    6. slider->setValue( d_e->value() );
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

    e is the event, param and value are integers from the MIDI messages. The range of value and therefor the QSliders is 0 to 127 for now. lPlaySliders is an array containing all the 'playback' sliders in the form.

    I though it might be the Event mechanism not keeping up (even though the MIDI messages are not coming in at any crazy rate), but having the Event Handler update the text on a QPushButton with the value instead of moving a slider works as it should.. the number keeps up well with the MIDI.

    Anyone have any ideas what could be wrong here? Is QSlider prone to update problems.. is there another component I could use instead?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSlider Update Problem

    Quote Originally Posted by December
    even though the MIDI messages are not coming in at any crazy rate
    Are you sure? What is the maximum number of messages per second?

    How does that data-collecting thread works? Maybe it starves the GUI thread?

  3. #3
    Join Date
    Sep 2006
    Posts
    68
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSlider Update Problem

    Well, even if I push the fader up real slow.. say 6 seconds from bottom to top, the same problem occurs.

    That would mean around 16 messages per second.The code in the MIDI thread is actually still a bit of a mystery to me.. as its taken from some other o/s projects

    Qt Code:
    1. void midiThread::run()
    2. {
    3. snd_seq_t *seq_handle;
    4. int npfd;
    5. struct pollfd *pfd;
    6.  
    7. seq_handle = open_seq();
    8. npfd = snd_seq_poll_descriptors_count(seq_handle, POLLIN);
    9. pfd = (struct pollfd *)alloca(npfd * sizeof(struct pollfd));
    10. snd_seq_poll_descriptors(seq_handle, pfd, npfd, POLLIN);
    11. while (1) {
    12. if (poll(pfd, npfd, 100000) > 0) {
    13. midi_action(seq_handle);
    14. }
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    This is the run part of the QThread. The other relevant bit is:

    Qt Code:
    1. void midiThread::midi_action(snd_seq_t *seq_handle) {
    2.  
    3. snd_seq_event_t *ev;
    4. MyDataEvent dataEvent(0,0);
    5.  
    6. do {
    7. snd_seq_event_input(seq_handle, &ev);
    8. switch (ev->type) {
    9. case SND_SEQ_EVENT_CONTROLLER:
    10. dataEvent.setParam(ev->data.control.param);
    11. dataEvent.setValue(ev->data.control.value);
    12. QApplication::sendEvent( parent, &dataEvent );
    13. break;
    14. }
    15.  
    16. snd_seq_free_event(ev);
    17. } while (snd_seq_event_input_pending(seq_handle, 0) > 0);
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    If this is a horrible way to do things.. I would much appreciate any ideas on how to handle MIDI better.

    On windows I used Borland C++ Builder.. and found a great component that pretty much did it all for me, passing the messages to my callback function. I guess I have to learn more C++ to program on Linux ;-)

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSlider Update Problem

    Quote Originally Posted by December
    That would mean around 16 messages per second.
    Then it shouldn't be a problem, unless you do a busy-wait somewhere. You check with top if your program doesn't eat whole CPU power.

    poll() is a good way to go, but in midi_action() you have a problem --- you use sendEvent(), but only postEvent() is thread-safe.

    Try:
    Qt Code:
    1. void midiThread::midi_action(snd_seq_t *seq_handle) {
    2. ...
    3. do {
    4. snd_seq_event_input(seq_handle, &ev);
    5. switch (ev->type) {
    6. case SND_SEQ_EVENT_CONTROLLER:
    7. MyDataEvent *dataEvent = new MyDataEvent(0,0);
    8. dataEvent.setParam(ev->data.control.param);
    9. dataEvent.setValue(ev->data.control.value);
    10. QApplication::postEvent( parent, dataEvent ); // QApplication takes ownership of the event
    11. break;
    12. }
    13. ...
    To copy to clipboard, switch view to plain text mode 

  5. The following user says thank you to jacek for this useful post:

    December (10th September 2006)

  6. #5
    Join Date
    Sep 2006
    Posts
    68
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSlider Update Problem

    Wow, that was the problem.

    Changing from using sendEvent to postEvent solved it. I feel stupid now, I even remember reading that sendEvent is not thread safe a while back

    Thanks for the help.. I think QT is going to be a lot of fun with such knowledgeable people around

Similar Threads

  1. View update problem
    By prakash in forum Qt Programming
    Replies: 6
    Last Post: 17th March 2006, 10:13
  2. Replies: 16
    Last Post: 7th March 2006, 15:57
  3. hide/show screen update problem
    By SkripT in forum Qt Programming
    Replies: 2
    Last Post: 6th February 2006, 17:49
  4. Problem with screen update...
    By mysearch05 in forum Qt Programming
    Replies: 2
    Last Post: 27th January 2006, 18:24
  5. Record update windowd entered data saving
    By MarkoSan in forum Qt Programming
    Replies: 56
    Last Post: 18th January 2006, 18:50

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.