PDA

View Full Version : change colors using Qpainter during run time



sandeep_hyd123
9th June 2011, 07:28
Hi guys,
i want the color of a rectangle drwan on central widget to change depending on the input i give, the code is wrkin if i embed the color value in de code, but I have no clue how to pass this value at run time and I want the color to change instantaneously....Thanx in advance

code:



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include"QWidget"
#include <QtGui>
#include "QPixmap"
#include"QPen"
#include<iostream>
using namespace std;
class MyWidget : public QWidget
{
public:
MyWidget();

protected:
void paintEvent(QPaintEvent *);

};

MyWidget::MyWidget()
{

}


void MyWidget::paintEvent(QPaintEvent *event)
{
QRectF rectangle(50,50,300,300);

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QPen pen(Qt::green, 30, Qt::SolidLine, Qt::RoundCap, Qt::RoundJoin);

int input=75;
int level=input/20;
int rem=input-level*20;
float a;
switch(level)
{
case 0:



pen.setColor(QColor(0,255,0,255));

painter.setPen(pen);

a=.1*rem;
painter.setOpacity(a);
painter.drawRect(50, 50, 300, 300);
break;


case 1:

pen.setColor(QColor(rem*6,255,0,255));

painter.setPen(pen);
a=.4;
painter.setOpacity(a);
painter.drawRect(50, 50, 300, 300);
break;
case 2:

pen.setColor(QColor(127+rem*6,255,0,255));

painter.setPen(pen);
a=.6;
painter.setOpacity(a);
painter.drawRect(50, 50, 300, 300);
break;
case 3:

pen.setColor(QColor(255,255-rem*6,0,255));

painter.setPen(pen);
a=.8;
painter.setOpacity(a);
painter.drawRect(50, 50, 300, 300);
break;
case 4:

pen.setColor(QColor(255,127-rem*6,0,255));

painter.setPen(pen);
a=.8;
painter.setOpacity(a);
painter.drawRect(50, 50, 300, 300);
break;

default:
pen.setColor(QColor(255,0,0,255));

painter.setPen(pen);
a=1;
painter.setOpacity(a);
painter.drawRect(50, 50, 300, 300);
break;




}


}

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//setStyleSheet("MainWindow {background-image:url('/Users/sandeep_hyd123/Qt sandy/first/photo.jpeg');background-repeat: repeat;background-position: top right;}");
setStyleSheet("MainWindow {border-width: 4px; border-image:url('/Users/sandeep_hyd123/Qt sandy/first/photo.jpeg') 4 4 4 4 stretch stretch;}");


MyWidget* my_widget = new MyWidget();
QWidget::setFixedSize ( 400, 400 );

setCentralWidget(my_widget);
setWindowTitle(QApplication::translate("toplevel", "Main widget"));



}

MainWindow::~MainWindow()
{
delete ui;
}

ChrisW67
9th June 2011, 07:55
What's the difficult bit? Store the desired input as a member variable when your input changes, then call QWidget::update(). In your paint() method use that member variable as your input.

Santosh Reddy
9th June 2011, 08:03
store the color in your class an use it while painting



class MyWidget : public QWidget
{
...
public slots:
void setMyPenColor(int x);
private:
QColor myPenColor;
...

};

MyWidget::MyWidget()
{
myPenColor = QColor(0,255,0,255);// set default color
}


void MyWidget::paintEvent(QPaintEvent *event)
{
...
switch(level)
{
case 0:
pen.setColor(myPenColor); //note this
painter.setPen(pen);
...
case 1:
pen.setColor(myPenColor); //note this
painter.setPen(pen);
...
}
...
}

void MyWidget::setMyPenColor(int x)
{
swithc(x)
{
// change color here
case 2: myPenColor = QColor(255,255,0,255); break;
case 4: myPenColor = QColor(255,0,0,255); break;
}
}


now call / connect slot to your input widget, take care to modify the slot signature as required by the signal


then call QWidget::update().
I missed to mention this, call this in setMyPenColor();

sandeep_hyd123
9th June 2011, 22:57
@santosh:
But i am bit confused over the usage of two switch cases... de switch case inside setmypencolor() you mentioned shud be sufficient rite...?
My doubt is over de usage of signals and slots, will update() function be called evrytime a signal is sent by input widget(shud i create this widget?)..." I missed to mention this, call this in setMyPenColor();" ; i cudnt get this pt... thnx in advance

wysota
9th June 2011, 23:13
I will suggest yet another solution that doesn't involve any extra variables and special code. Simply use QPalette -- let your painting code use one of the colors stored in the palette and then the user can change the color by modifying the palette.


void MyWidget::paintEvent(QPaintEvent *pe){
QPainter painter(this);
QPen p;
p.setColor(palette().color(QPalette::Highlight));
painter.setPen(p);
//...
}

and somewhere else:


QPalette p = widget->palette();
p.setColor(QPalette::Highlight, Qt::red);
widget->setPalette(p); // causes update() of the widget and redraw with a modified color

Santosh Reddy
10th June 2011, 00:05
But i am bit confused over the usage of two switch cases... de switch case inside setmypencolor() you mentioned shud be sufficient rite...?
No, I just gave an simple implementation example using switch, you should write your code in there


My doubt is over de usage of signals and slots, will update() function be called evrytime a signal is sent by input widget(shud i create this widget?)...
No, you have to call update() explicitly in setMyPenColor(), which will cause the widget to be painted with new color

I missed to mention update() in the previous code example.


void MyWidget::setMyPenColor(int x)
{
...
update();
}


I will suggest yet another solution that doesn't involve any extra variables and special code. Simply use QPalette...
This is a simple yet good solution, you can also try exploring this