Results 1 to 4 of 4

Thread: How to change the existing color of an object

  1. #1
    Join Date
    Feb 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to change the existing color of an object

    Hi,

    I have a serious problem in changing the existing color of an object when a particular function is called.
    I have designed a GUI to control the Khepera Robot. I have designed this GUI using Qt Designer as well as coding. The object which for which I need to change the colour, I have drawn it using code. Actually I have drawn a circle with two rows of rectangles, each row with 8 rectangles along the circumfrence of the circle. These rectangles represent the sensors ( 8 Ambient sensors and 8 proximity Sensors). I have given two different colours to each row of rectangles. My aim is to change the colour of the rectangles in each row when their respective function for reading the ssensors is called. I am prsenting below my code for GUI which I have written. Fucntions to read sensors getAmbientSensors() and getProximitySensors() are implemented in KheperaiiInterface.cpp which is a robot functioning code.

    Actually the problem is where I should call these two functions. I thought of writing like this in MoveRobot constructor...

    Qt Code:
    1. QTimer *timer = new QTimer(this);
    2. connect (timer, SIGNAL(timeout()), this, SLOT(getAmbientSensors()));
    3. timer->start(1000);
    To copy to clipboard, switch view to plain text mode 

    For every 1000ms the timer updates and it calls the function getAmbientSensors() which returns the ambient sensors. Now my problem is where to write the function to change the existing color of the rectangles representing 'Ambient Sensors' of the robot. I think, I can write this function only in paintEvent() but how to write it and what is the synatax of this? Please help me in this regard. If you don't understand my question properly please reply me, I will try to explain you in a better way.



    Please help me by suggesting me a good solution in this regard.

    Your help would be a great relief to me as the final date to sub,mit my project is very near.

    With Regards
    Thirupathi Uppu
    Attached Files Attached Files
    Last edited by wysota; 7th February 2008 at 20:57. Reason: missing [code] tags

  2. #2
    Join Date
    Aug 2006
    Location
    Bangalore,India
    Posts
    419
    Thanks
    37
    Thanked 53 Times in 40 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to change the existing color of an object

    I am not sure whether i have understood your problem correctly or not but i'll try to answer.
    May be you can use a QColor or better QPen object as a member of the class of the sensor widget and while drawing rectangles , set this pen or color in paintEvent().
    Provide a slot to modify color and either directly connect the timer signal to this slot or just call the slot manually in getAmbientSensors() method.

    The slot just have to be have like this
    Qt Code:
    1. void SomeClass::changeColor()
    2. {
    3. pen.setColor(newColor);
    4. update();
    5. }
    6.  
    7. void SomeClass::paintEvent(..)
    8. {
    9. painter->setPen(pen);
    10. pen.drawRect(..);
    11.  
    12. ..
    13. }
    To copy to clipboard, switch view to plain text mode 
    The biggest difference between time and space is that you can't reuse time.
    -- Merrick Furst

  3. #3
    Join Date
    Feb 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to change the existing color of an object

    Dear Sir,

    Thanks for your reply. I will implement this and will get back to you, to thank you or ask you some more clarifications

    With Regards
    Thirupathi Uppu

  4. #4
    Join Date
    Feb 2008
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to change the existing color of an object

    Dear Sir,

    I have implemented your suggestion and I could change the color of the rectangle representing sensors. But I couldn't apply the same with brush as pen. When I replaced QPen pen with QBrush brush and reimplemented your suggestion with QBrush with the hope that it will change the color (fill) of the rectangle but I couldn't succeed. And finally what I wanted is "not just change of color of the rectangles" rather "blinking of the rectangles (fills)" whenever the 'getAmbientSensors() and getProximitySensors() functions are called. It is just to show the user that sensors are reading. I am here attaching my GUI image and my code. Please have a look at it and help me in making the rectangles blink. As I have to submit this project very soon, your help would be a great relief to me.

    With Regards
    Thirupathi Uppu
    Attached Images Attached Images
    Attached Files Attached Files

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.