K
K
Kirill H2016-12-17 21:37:31
Qt
Kirill H, 2016-12-17 21:37:31

How to determine scene pixel color in Qt?

There is an instance of the QGraphicsScene class . It creates instances of class TFish , inherited from QGraphicsItem . A timer has been started with a value of 0.003 seconds. Each time it expires, a signal is sent to the advance() slot , it is overridden for the TFish class, if it is not equal to 0, then according to a random algorithm, TFish instances "float".
The goal is to make it so that the TFish class determines the color of the pixel in front of itself, if it does not match the specified condition, then the direction of the TFish instance changes.
This has already been implemented to avoid collision with other TFish instances:

QList<QGraphicsItem *> dangerMice = scene()->items(QPolygonF()
                                                       << mapToScene(0, 0)
                                                       << mapToScene(-30, -50)
                                                       << mapToScene(30, -50));
    foreach (QGraphicsItem *item, dangerMice) {
        if (item == this)
            continue;

        QLineF lineToMouse(QPointF(0, 0), mapFromItem(item, 0, 0));
        qreal angleToMouse = ::acos(lineToMouse.dx() / lineToMouse.length());
        if (lineToMouse.dy() < 0)
            angleToMouse = TwoPi - angleToMouse;
        angleToMouse = normalizeAngle((Pi - angleToMouse) + Pi / 2);

        if (angleToMouse >= 0 && angleToMouse < Pi / 2) {
            angle += 0.5;
        } else if (angleToMouse <= TwoPi && angleToMouse > (TwoPi - Pi / 2)) {
            angle -= 0.5;
        }
    }

I found a code that works according to the scheme to take a screenshot of a window -> determine the color of a pixel by coordinates.
It seems to me that this is not rational and I do not want to implement it for several reasons:
  1. Taking a screenshot 30 times per second is quite expensive in terms of resources.
  2. You will have to pass an image or a link to the scene to the advance() function, because from the TFish class, you won't be able to take a screenshot.
  3. I think there is a simpler solution to this problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Stepanov, 2017-01-14
@koronabora

Load the background into a separate area of ​​​​memory and look for the color of the pixel in peace.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question