Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QGraphicsScene change color of Pixels
Forum Updated to NodeBB v4.3 + New Features

QGraphicsScene change color of Pixels

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    katana
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved