PDA

View Full Version : How to change the existing color of an object



thirupathiuppu007
6th February 2008, 15:09
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...


QTimer *timer = new QTimer(this);
connect (timer, SIGNAL(timeout()), this, SLOT(getAmbientSensors()));
timer->start(1000);

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

Gopala Krishna
6th February 2008, 15:22
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

void SomeClass::changeColor()
{
pen.setColor(newColor);
update();
}

void SomeClass::paintEvent(..)
{
painter->setPen(pen);
pen.drawRect(..);

..
}

thirupathiuppu007
8th February 2008, 13:59
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

thirupathiuppu007
10th February 2008, 01:25
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