PDA

View Full Version : How to get color of pixel or point by QMouseEvent



Krishnacins
27th May 2006, 13:27
Hi,

Im implementing a Analog Clock (like widgets/analogclock/analogclock.cpp Qt4 example) & using Qt4.1.2 & windows................
Can anybody tell me If i click on clock niddle (i get points by QmouseEvent ......) so how can i get the color of that points...................

Or can anybody tell me how can i move the niddles of clock clicking on niddle by mouse or by gragging the niddle.

Please help me ..........

This is example (widgets/analogclock/analogclock.cpp)

//analogclock.cpp
#include <QtGui>

#include "analogclock.h"

AnalogClock::AnalogClock(QWidget *parent)
: QWidget(parent)
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(1000);

setWindowTitle(tr("Analog Clock"));
resize(200, 200);
}

void AnalogClock::paintEvent(QPaintEvent *)
{
static const QPoint hourHand[3] = {
QPoint(7, cool ,
QPoint(-7, cool ,
QPoint(0, -40)
};
static const QPoint minuteHand[3] = {
QPoint(7, cool ,
QPoint(-7, cool ,
QPoint(0, -70)
};

QColor hourColor(127, 0, 127);
QColor minuteColor(0, 127, 127, 191);

int side = qMin(width(), height());
QTime time = QTime::currentTime();

QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.translate(width() / 2, height() / 2);
painter.scale(side / 200.0, side / 200.0);

painter.setPen(Qt::NoPen);
painter.setBrush(hourColor);

painter.save();
painter.rotate(30.0 * ((time.hour() + time.minute() / 60.0)));
painter.drawConvexPolygon(hourHand, 3);
painter.restore();

painter.setPen(hourColor);

for (int i = 0; i < 12; ++i) {
painter.drawLine(88, 0, 96, 0);
painter.rotate(30.0);
}

painter.setPen(Qt::NoPen);
painter.setBrush(minuteColor);

painter.save();
painter.rotate(6.0 * (time.minute() + time.second() / 60.0));
painter.drawConvexPolygon(minuteHand, 3);
painter.restore();

painter.setPen(minuteColor);

for (int j = 0; j < 60; ++j) {
if ((j % 5) != 0)
painter.drawLine(92, 0, 96, 0);
painter.rotate(6.0);
}
}


// .h
#ifndef ANALOGCLOCK_H
#define ANALOGCLOCK_H

#include <QWidget>

class AnalogClock : public QWidget
{
Q_OBJECT

public:
AnalogClock(QWidget *parent = 0);

protected:
void paintEvent(QPaintEvent *event);
};

#endif


//main.cpp
#include <QApplication>

#include "analogclock.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
AnalogClock clock;
clock.show();
return app.exec();
}



Thanks & regards
Krishna

rickbsgu
27th May 2006, 18:47
Tough problem.

One way is you could set up a shadow pixel buffer the same size as your widget, and draw into that and the clock simultaneously. Then you could use the point to index into your pixelbuffer relatively easily.

Barring that, you'll have to get to the screen pixel buffer. I'm not sure if you can do that in Qt. You might have to go down to raw windows to get to it.


I'm not sure what a 'niddle' is, but dragging and dropping a part of the graphic means you're going to have to not only set up the capacity to select (and change) that part of the graphic, but also redraw it in its changed state on subsequent redraws. Implies more structuring for the graphics than the simple draw algorithm presented, here.

rickb

jacek
27th May 2006, 19:21
Barring that, you'll have to get to the screen pixel buffer. I'm not sure if you can do that in Qt. You might have to go down to raw windows to get to it.
One can always capture the widget using QPixmap::grabWidget(), but IMO it's better to just know where clock's hands are instead of checking pixel's colour. QPainterPath::contains() might be usefull.

wysota
27th May 2006, 22:06
Or simple maths...

rickbsgu
28th May 2006, 01:46
One can always capture the widget using QPixmap::grabWidget(), but IMO it's better to just know where clock's hands are instead of checking pixel's colour. QPainterPath::contains() might be usefull.
I agree. The 'grabWidget' func is good to know, though.

rickb