简体   繁体   中英

Color rectangles using QGraphicsRectItem

I'am stuck in one case. I have scene (using QGraphicsScene) and I fill that scene with squares (using QGraphicsRectItem). I want make every square color to black as I move mouse over squares with mouse button pressed. Can you please give me any idea how to make that happen ? I was trying to solve that using mousePressEvent, mouseMoveEvent, dragEnterEvent etc. and I think that this is a proper way to do that but I have no idea how to push that through. To put more light on my case I have added sample of my code. Thanks for help.

main.cpp

#include <QApplication>
#include <QGraphicsScene>
#include <QGraphicsView>
#include "square.h"
#include "background.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    // create a scene
    QGraphicsScene * scene = new QGraphicsScene(0,0,200,250);


    Background * background = new Background();
    background->fillBackgroundWithSquares(scene);



    // add a view
    QGraphicsView * view = new QGraphicsView(scene);

    view->show();


    return a.exec();
}

background.h

#ifndef BACKGROUND_H
#define BACKGROUND_H
#include <QGraphicsScene>
#include <square.h>
class Background
{
public:
    Background();
    void fillBackgroundWithSquares(QGraphicsScene *scene);

};

#endif // BACKGROUND_H

background.cpp

#include "background.h"

Background::Background()
{

}

void Background::fillBackgroundWithSquares(QGraphicsScene *scene)
{
    // create an item to put into the scene
    Square *squares[20][25];


    // add squares to the scene
    for (int i = 0; i < 20; i++)
        for (int j = 0; j < 25; j++) {
            squares[i][j] = new Square(i*10,j*10);
            scene->addItem(squares[i][j]);
        }

}

square.h (EDIT)

#ifndef SQUARE_H
#define SQUARE_H
#include <QGraphicsRectItem>
#include <QGraphicsView>


class Square : public QGraphicsRectItem
{
public:
    Square(int x, int y);

private:
    QPen pen;

protected:
    void hoverEnterEvent(QGraphicsSceneHoverEvent * event);


};

#endif // SQUARE_H

square.cpp (EDIT)

#include "square.h"

Square::Square(int x, int y)
{
    // draw a square
    setRect(x,y,10,10);
    pen.setBrush(Qt::NoBrush);
    setPen(pen);
    setBrush(Qt::cyan);
    setAcceptHoverEvents(true);
    setAcceptedMouseButtons(Qt::LeftButton);
    show();
}

void Square::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{

    if ( brush().color() != Qt::black && QApplication::mouseButtons() == Qt::LeftButton)
    {
        setBrush( Qt::black );
        update();
    }

}

Try calling:

square[j][j]->setAcceptedMouseButtons(...)

and

square[i][j]->show() 

after creating it.

You can also reimplement hoverEnterEvent() and hoverLeaveEvent() if you want to change the color on the hover event.

If the mouse butten needs to be pressed when you hover: you store button state within mouse down/up event in a variable for ex. bool isMouseButtonDown and check this in your hover event handler. You can also use: QApplication::mouseButtons() to check the buttons states.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM