1

I am trying to create a dot matrix in a QGraphicsScene. I have a button, which fills a 2d-array with random numbers and than i will paint a pixel on every position where the array has a 0. Now, when I wants to generate the matrix again i want to check every pixel and array-field whether they are empty or not. If the pixel is empty and the array not, i want to set a pixel. If there is a pixel but the array is empty i want to remove the pixel. Now the problem is, the function itemAt() always returns 0 even if i can clearly see existen pixels. What is my problem?

//creating the scene in the constructor
QPainter MyPainter(this);
scene = new QGraphicsScene(this);
ui.graphicsView->setScene(scene);

//generating matrix
void MaReSX_ClickDummy::generate(void)
{
    QGraphicsItem *item;
    int x, y;
    for(x=0; x< 400; x++)
    {
        for(y=0; y<400; y++)
        {
            dataArray[x][y] = rand()%1001;
        }
    }

    for(x=0; x < 400; x++)
    {
        for(y=0; y<400; y++)
        {
            item = scene->itemAt(x, y, QTransform());//supposed to check whether there is already a pixel on that place but always returns zero
            if(dataArray[x][y] == 0 && item == 0)
                scene->addEllipse(x, y, 1, 1);
            //does not work
            else if(dataArray[x][y] != 0 && item != 0)
                scene->removeItem(item);
        }
    }
}

Also the generating of the matrix is very slow. Since the matrix is supposed to show realtime data later, it should run as fast as possible. (and the scene will be bigger than 400*400 pixels like now). Any ideas how to improve the code?

And can somebody explain what the third parameter of itemAt() is supposed to do?

Thank you!

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
honiahaka10
  • 772
  • 4
  • 9
  • 29

2 Answers2

2

400x400 'dot matrix' is up to 16000 dots, or up to 2500 characters, which quite big. The QGraphicsScene is designed to handle a small number of large shapes, and was probably not designed to handle this many shapes. Using it in this way to create thousands of identical tiny 'pixel' objects is incredibly inefficent.

Could you create a 400x400 bitmap(QBitmap?) instead, and set the individual pixels that you want?

Jonathan
  • 206
  • 1
  • 4
  • OKay, thank you i will now try to figure out, how QImage works. I found this project which seems to be mostly that what i want [image](http://www.swharden.com/blog/2013-06-03-realtime-image-pixelmap-from-numpy-array-data-in-qt/) I just need to figure out how i can do this in c++. But this will only answer half of my question: Why do itemAt() not work? – honiahaka10 Sep 17 '15 at 08:50
0

You are supposed to be using a QGraphicsPixmapItem instead of an array of dots!

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313