简体   繁体   English

在graphicsview中绘画

[英]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. 但是当我使用向量存储点时,它不会追加,将其容量限制为2个。 Following is my output and the code circle.cpp 以下是我的输出和代码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. 当我删除信号DrawFinished()时,这些点可以完美存储,但是该项目仅绘制一次。 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() . 我真的不明白您的信号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. 如果我总结出您真正想要的东西,您将拥有一个QGraphicView ,您可以在其中绘制一些形状。 Next to it, you have at least one (let's say 3) buttons to select which shapes you want to draw (Circle, Triangle, Rectangle). 在它旁边,您至少有一个(假设3个)按钮可以选择要绘制的形状(圆形,三角形,矩形)。

Lets say you want to draw some circles, you click on the CircleButton, and then, click on the QGraphicView. 假设您要绘制一些圆,单击CircleButton,然后单击QGraphicView。

To my mind, I would create something like this: 在我看来,我将创建以下内容:

Two classes, MainWindow and View , view which inherits from QGraphicView . 继承自QGraphicView两个类MainWindowView Your three buttons are defined with Qt designer in your MainWindow class. 您的三个按钮是通过MainWindow类中的Qt设计器定义的。 So When you click on a button, you can emit a signal to notify the View . 因此,当您单击按钮时,您可以发出信号通知View

In the View class you could have one vector for each shapes. View类中,每个形状可以有一个向量。

MainWindow.h 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 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 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. 更新视图时,您只需要旅行就扔掉3个向量并绘制圆形,矩形或三角形。

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. 我没有运行代码,因此可能存在一些小错误,请不要忘记进行连接和初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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