Results 1 to 5 of 5

Thread: How to get color of pixel or point by QMouseEvent

  1. #1
    Join Date
    Mar 2006
    Posts
    20
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default How to get color of pixel or point by QMouseEvent

    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:aintEvent(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

  2. #2
    Join Date
    Jan 2006
    Location
    Earth (Terra)
    Posts
    87
    Thanks
    4
    Thanked 6 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: How to get color of pixel or point by QMouseEvent

    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
    Last edited by rickbsgu; 27th May 2006 at 18:51.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to get color of pixel or point by QMouseEvent

    Quote Originally Posted by rickbsgu
    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.

  4. #4
    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 get color of pixel or point by QMouseEvent

    Or simple maths...

  5. #5
    Join Date
    Jan 2006
    Location
    Earth (Terra)
    Posts
    87
    Thanks
    4
    Thanked 6 Times in 4 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: How to get color of pixel or point by QMouseEvent

    Quote Originally Posted by jacek
    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

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.