Page 1 of 2 12 LastLast
Results 1 to 20 of 35

Thread: How to threading data in app?

  1. #1
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Question How to threading data in app?

    Hi all!
    I created simple app as picture below
    threading.png
    code in file main.qml
    Qt Code:
    1. import QtQuick 2.3
    2. import QtQuick.Controls 1.2
    3.  
    4. ApplicationWindow {
    5. visible: true
    6. width: 640
    7. height: 480
    8. Rectangle{
    9. color: "red"
    10. width: 100
    11. height: 100
    12. anchors.centerIn: parent
    13. Timer{
    14. interval: 30
    15. running: true
    16. repeat: true
    17. onTriggered: {
    18. _processdata.calculator()
    19. }
    20. }
    21. }
    22. }
    To copy to clipboard, switch view to plain text mode 
    I have one component is a rectangle. In rectangle have timer and 30ms it Triggered to call function _processdata.calculator() from class C++
    It's works fine if I put less rectangle(1- 100 rectangle). But I put about 500 - 1000 rectangle in my app, It became slow and when I move app, it is shaking shock
    Cause is all rectangle call function _processdata.calculator() time.
    How to it works fine when I put 500 - 1000 rectangle in my app? Please help me!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to threading data in app?

    Why do you need to call the same function on the same object with multiple timers?
    If you want to update whatever it is that the function is doing, just to it with a single timer.

    Cheers,
    _

  3. #3
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: How to threading data in app?

    Because a rectangle will call the function and send possition of it to get pixel color. The same function but return different results
    Every 30ms. I need the return value of each rectangle.

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to threading data in app?

    Quote Originally Posted by tanthinh1510 View Post
    Because a rectangle will call the function and send possition of it to get pixel color. The same function but return different results
    Then this was a very bad choice of example code.

    Quote Originally Posted by tanthinh1510 View Post
    Every 30ms. I need the return value of each rectangle.
    And each rectangle needs its own update interval? Or are they updated at the same time?

    Cheers,
    _

  5. #5
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: How to threading data in app?

    Quote Originally Posted by anda_skoa View Post
    And each rectangle needs its own update interval? Or are they updated at the same time?

    Cheers,
    _
    They updated at the same time. But I try both cases and results is the sames. It became slow.

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to threading data in app?

    Quote Originally Posted by tanthinh1510 View Post
    They updated at the same time
    So you only need one timer.

    Ok, then lets ignore your now irrelevant example and concentrate on the actual use case:

    - you have a number of rectangles.
    - they can change position
    - their color depends on that position
    - you want to update color for all rectangles every 30 seconds, not on every posiiton change

    Correct?

    Cheers,
    _

  7. #7
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: How to threading data in app?

    Quote Originally Posted by anda_skoa View Post
    - you have a number of rectangles.
    Cheers,
    _
    Number of rectangles is random. I can enter any number
    Quote Originally Posted by anda_skoa View Post
    - they can change position
    Cheers,
    _
    yes, they can change position
    Quote Originally Posted by anda_skoa View Post
    - their color depends on that position
    Cheers,
    _
    Yes, their color depends on that position
    Quote Originally Posted by anda_skoa View Post
    - you want to update color for all rectangles every 30 seconds, not on every posiiton change
    Cheers,
    _
    Yes. because color screen always change, so I want to update color for all rectangles every 30 miliseconds(not 30 senconds)
    I think cause is all rectangle call function at the same time. But I have not found a way to solve.

  8. #8
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to threading data in app?

    One approach is to register all rectangles with a controller object.
    It runs the timer and on timeout gets the positions from the registered objects and updates the color.

    Another approach is to have a model that gets the number of rectangles as its input and generates as many reference objects.
    Each such object has a read/write for the position and a read property for the color.

    The retangles are then generated using a Repeater, bind their position and color to the given reference object.
    The model then updates each reference object with a timer.

    Cheers,
    _

  9. The following user says thank you to anda_skoa for this useful post:

    tanthinh1510 (26th March 2015)

  10. #9
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: How to threading data in app?

    Quote Originally Posted by anda_skoa View Post
    One approach is to register all rectangles with a controller object.
    It runs the timer and on timeout gets the positions from the registered objects and updates the color.

    Another approach is to have a model that gets the number of rectangles as its input and generates as many reference objects.
    Each such object has a read/write for the position and a read property for the color.

    The retangles are then generated using a Repeater, bind their position and color to the given reference object.
    The model then updates each reference object with a timer.

    Cheers,
    _
    Thanks you anda_skoa!
    I will try approach of you and update result as soon.
    Thanks again.

  11. #10
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: How to threading data in app?

    I try created list include information of rectangle (possition) to contain all rectangle.
    Every 30 milisecond. I get possition from list to get pixelcolor. Result is it slow the same as the last.
    Maybe 30 milisecond not enough to calculate all the rectangles in list.

  12. #11
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to threading data in app?

    Well, you don't have to guess, you can measure the time spent in update function, see QElapsedTimer.

    Cheers,
    _

  13. #12
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: How to threading data in app?

    Quote Originally Posted by anda_skoa View Post
    Well, you don't have to guess, you can measure the time spent in update function, see QElapsedTimer.

    Cheers,
    _
    I follow the above method and use see QElapsedTimer to measure the time spent in update function.
    I put 512 rectangle and result QElapsedTimer return is 151 milisecond.
    Is there a way for me to shorten the processing time down?

  14. #13
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to threading data in app?

    There is no generic answer to performance, there are way to many reasons for slowness.

    The first step is always to do a thorough analysis, profiling the code to see exactly where it spends the time.

    Can you post the code of the update function?

    Cheers,
    _

  15. #14
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: How to threading data in app?

    Code I save rectangle infomation to list
    Qt Code:
    1. void processData::get_light_info_control(int _channel, int _posX, int _posY, int _index)
    2. {
    3. controllercolor *lightcolor = new controllercolor;
    4. lightcolor->channel = _channel;
    5. lightcolor->index = _index;
    6. lightcolor->posX = _posX;
    7. lightcolor->posY = _posY;
    8. listcontroller.append(lightcolor);
    9. }
    To copy to clipboard, switch view to plain text mode 
    Here is code update function
    Qt Code:
    1. void processData::process_channel()
    2. {
    3. foreach (controllercolor *lightcolor, listcontroller) {
    4. getMyPixel(lightcolor->posX, lightcolor->posY, lightcolor->index, lightcolor->channel);
    5. }
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 
    Here is code function getMyPixel
    Qt Code:
    1. QColor processData::getMyPixel(int _x, int _y, int _index, int _channel)
    2. {
    3. HWND name = FindWindow(NULL,L"Lighting Player");
    4. HDC dc = GetDC(name);
    5. COLORREF color = GetPixel(dc, _x, _y );
    6.  
    7. int _red = GetRValue(color);
    8. int _green = GetGValue(color);
    9. int _blue = GetBValue(color);
    10. int _white = qMin(_red,_green);
    11. _white = qMin(_white,_blue);
    12.  
    13. int _dimmer = _red*0.35 + _green*0.5 + _blue*0.15;
    14. QColor c1;
    15. c1.setRgb(_red, _green, _blue);
    16. ReleaseDC(name, dc);
    17. colorR = c1.name().mid(1,2);
    18. colorG = c1.name().mid(3,2);
    19. colorB = c1.name().mid(5,2);
    20.  
    21. colorD = QString::number( _dimmer, 16 );
    22. if(colorD.length() == 1) {
    23. colorD.insert(0,"0");
    24. }
    25. colorW = QString::number( _white, 16 );
    26. if(colorW.length() == 1) {
    27. colorW.insert(0,"0");
    28. }
    29.  
    30.  
    31. if(_index == 0){
    32. frame.replace(_channel*2 + 20,2,colorD);
    33. }
    34. else if(_index == 1){
    35. frame.replace(_channel*2 + 20,6,colorR+colorG+colorB);
    36. }
    37. else if (_index == 2){
    38. frame.replace(_channel*2 + 20,8,colorR+colorG+colorB+colorW);
    39. }
    40. send_data_channel();
    41. return c1.name();
    42. }
    To copy to clipboard, switch view to plain text mode 

    have anything idea?
    Last edited by tanthinh1510; 27th March 2015 at 11:01.

  16. #15
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to threading data in app?

    Finding the window, getting and releasing a DC and calling send_data_channel for every loop iteration looks really wasteful.

    Cheers,
    _

  17. #16
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to threading data in app?

    If color of an object depends on this object's position, can't you simply ask the object what color it has instead of using this hacky WinAPI approach?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  18. #17
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: How to threading data in app?

    Quote Originally Posted by anda_skoa View Post
    Finding the window, getting and releasing a DC and calling send_data_channel for every loop iteration looks really wasteful.

    Cheers,
    _
    I just do not call these functions, better results but still not solved the problem.
    I test and realized that GetPixel(dc, _x, _y ) is time-consuming. So when I put 500 - 1000 rectangle, I had 500 -1000 object in list and take a long time to process.
    however I only 30ms to process all.


    Added after 4 minutes:


    Quote Originally Posted by wysota View Post
    If color of an object depends on this object's position, can't you simply ask the object what color it has instead of using this hacky WinAPI approach?
    I get color pixel screen from object's position. I used WinAPI approach to get it. Anything approach?
    Last edited by tanthinh1510; 1st April 2015 at 08:55.

  19. #18
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to threading data in app?

    The object knows its own color, ask the object for its "color" property instead of asking the screen for a pixel color.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  20. #19
    Join Date
    Oct 2013
    Location
    VietNam
    Posts
    41
    Thanks
    3
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Windows Android

    Default Re: How to threading data in app?

    Quote Originally Posted by wysota View Post
    The object knows its own color, ask the object for its "color" property instead of asking the screen for a pixel color.
    I only used object get position to get pixel in screen. The object have "color" property is "transparent.
    As this video below

  21. #20
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to threading data in app?

    How is the gradient implemented?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Threading with rsh and rcp
    By jaxrpc in forum Newbie
    Replies: 2
    Last Post: 4th June 2010, 12:50
  2. Threading...?
    By sekatsim in forum Qt Programming
    Replies: 12
    Last Post: 10th June 2008, 02:14
  3. Qt Threading
    By avh in forum Newbie
    Replies: 7
    Last Post: 30th May 2008, 21:20
  4. I'm failing with Threading
    By thomaspu in forum Qt Programming
    Replies: 2
    Last Post: 11th January 2008, 20:40
  5. Sub-Threading
    By TheGrimace in forum Qt Programming
    Replies: 4
    Last Post: 7th June 2007, 17:38

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.