QGraphicsScene change color of Pixels



  • Hi everyone, I'm new here with Qt and picture work.
    What I'm doing is load a png file in my GUI and work with every pixel (change color)
    Now I've achieved my goal in two ways, but the result seems also a little bit different.

    first way
    In Mouse Press Event
    x = ev->scenePos().x();
    //xpos = ev->pos().x();//pos() is item coordinate it shows always 0
    y = ev->scenePos().y();
    down = true;
    box = new QGraphicsRectItem(ev->scenePos().x(),ev->scenePos().y(),1,1);
    box->setFlag(QGraphicsItem::ItemIsMovable,true);
    box->setFlag(QGraphicsItem::ItemIsSelectable,true);
    QPen pen2(Qt::red);
    pen2.setWidth(0);
    box->setPen(pen2);
    box->setBrush(QBrush(Qt::red));
    addItem(box);
    In Mouse Move Event
    if((box != 0) && (down)){
    box = new QGraphicsRectItem(ev->scenePos().x(),ev->scenePos().y(),1,1);
    box->setFlag(QGraphicsItem::ItemIsMovable,true);
    box->setFlag(QGraphicsItem::ItemIsSelectable,true);
    QPen pen2(Qt::red);
    pen2.setWidth(0);
    box->setPen(pen2);
    box->setBrush(QBrush(Qt::red));
    addItem(box);
    }
    This methode doesn't work quite right, it covers all the places where my Mouse moves over. That means the box isn't exactly fixed into the pixels.

    Second way
    In Mouse Press Event
    x = ev->scenePos().x();
    y = ev->scenePos().y();
    down = true;
    emit Mouse_left_pressed();
    In Mouse Move Event also just set the parameter x and y and emit the same Signal
    In my Mainwindow.cpp I connect the Signal with a slot like so:
    int posx,posy;
    posx = scene->x;
    posy = scene->y;
    rect = new QGraphicsRectItem(posx,posy,1,1);
    QPen pen1(Qt::red);
    pen1.setWidth(0);
    rect->setPen(pen1);
    rect->setBrush(QBrush(Qt::red));
    //scene->addRect(rect);
    scene->addItem(rect);
    //by default QGraphicsItems are not movale and selectable, set rectangle to be movable and selectable
    rect->setFlag(QGraphicsItem::ItemIsSelectable,true);
    rect->setFlag(QGraphicsItem::ItemIsMovable,true);

    Second way works perfectly. The RectItem covers always the pixels, that means it doesn't follow the mouse pos.

    I dont understand why. Can anyone help me?

    Thanks in advance


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.