简体   繁体   中英

Painting in graphicsview

I am using the graphics view to paint the graphicsitem in it. Earlier when I clicked the button the respective item was painted only once, to again paint the same entity I had topush the button again. To overcome this I constructed the signal to allow to add the entities multiple times without having the need to push the button again. But when I using vector to store the points.It does not append, limiting its capacity to 2 only. Following is my output and the code circle.cpp

void circle::mousePressEvent(QGraphicsSceneMouseEvent *e)
{
    if(e->button()==Qt::LeftButton) {
        if(mFirstClick){
            x1 = e->pos().x();
            y1 = e->pos().y();
            mFirstClick = false;
            mSecondClick = true;
        }

        else if(!mFirstClick && mSecondClick){
            x2 = e->pos().x();
            y2 = e->pos().y();
            mPaintFlag = true;
            mSecondClick = false;
            update();
            emit DrawFinished();
 _store.set_point(e->pos());
        store_point.push_back(_store);
        qDebug() << _store.getValue();
        qDebug() << "Size of vector =" << store_point.size() << "and" << store_point.capacity();
        update();

        }
    }

mainwindow.cpp

void MainWindow::drawCircle(){
    item2 = new circle;
    scene->addItem(item2);
    qDebug() << "Circle Created";
    connect(item2, SIGNAL(DrawFinished()), this, SLOT(drawCircle()));
}

output

Circle Created
QPointF(60, 87)
Size of vector = 1 and 1
Circle Created
QPointF(77, 221)
Size of vector = 2 and 2
QPointF(333, 57)
Size of vector = 1 and 1

When I remove the signal DrawFinished(), the points store perfectly but the item gets painted only once. I need to pushthe button again:(. Following is the output after removing the signal.

QPointF(74, 80)
Size of vector = 1 and 1
QPointF(118, 165)
Size of vector = 2 and 2
QPointF(335, 97)
Size of vector = 3 and 4

What needs to be done to perfectly store the points as well as allow repainting. Please do help me to sort out all this.

Well, not sure if this would answer your request but a comment is too small to write what i want to tell you.

I don't really get what is the purpose of your signal DrawFinished() . Even if it's obvious thanks to the name, I don't think you need it.

If I sum up what you really want, you have a QGraphicView where you want to draw some shapes. Next to it, you have at least one (let's say 3) buttons to select which shapes you want to draw (Circle, Triangle, Rectangle).

Lets say you want to draw some circles, you click on the CircleButton, and then, click on the QGraphicView.

To my mind, I would create something like this:

Two classes, MainWindow and View , view which inherits from QGraphicView . Your three buttons are defined with Qt designer in your MainWindow class. So When you click on a button, you can emit a signal to notify the View .

In the View class you could have one vector for each shapes.

MainWindow.h

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

signals:
    void drawCircle();
    void drawRectangle();
    void drawTriangle();

private:
    Ui::MainWindow *ui;
    View view;

private slots:
    void slotOnCircleButton();
    void slotOnRectangleButton();
    void slotOnTriangleButton();
};

MainWindow.cpp

[...]
void MainWindow::slotOnCircleButton()
{
    emit(drawCircle());
}
[...]

View.h

class View : public QGraphicsView
{
    Q_OBJECT
public:
    explicit View(QWidget *parent = 0);

    enum DrawingMode
    {
        UNDEFINED,
        CIRCLE,
        TRIANGLE,
        RECTANGLE
    }

signals:

public slots:
    void slotOnDrawCircle();
    void slotOnDrawRectangle();
    void slotOnDrawTriangle();

private:
    DrawingMode mode;
    QVector<QPointF> vectorCircle;
    QVector<QPointF> vectorTriangle;
    QVector<QPointF> vectorRectangle;
};

View.cpp

[...]
void View::slotOnDrawCircle()
{
    this->mode = CIRCLE;
}
[...]

void View::mousePressEvent(QGraphicsSceneMouseEvent *e)
{
    if(e->button()==Qt::LeftButton && this->mode != UNDEFINED)
    {
        qreal x1 = e->pos().x();
        qreal y1 = e->pos().y();

        if(this->mode == CIRCLE)
        {
            this->vectorCircle.append(e->pos());
        }
        else if(...)
        [...]

        // updatePainting();
    }
}

When updating the view, you just have to travel throw your 3 vectors and drawing circle, rectangle or triangle.

This way you don't have such a spaghetti code, it's quite clear.

I didn't run the code so there is probable some minor mistakes, don't forget to make your connections and your initializations.

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