c++ - QGraphicsScene清晰显示

标签 c++ qt qgraphicsview qgraphicsscene

我是新来的,我无法解决这个问题。 我有一个 QGraphicScene(它首先初始化一个笛卡尔平面),它在其中使用 AddLine\ecc 方法绘制点\线。一切正常。但我想要一个方法来删除场景中绘制的所有“对象”并重新加载笛卡尔平面。有什么建议吗?

在这里你可以找到我的类(class):

class draw: public QWidget
{
    Q_OBJECT

private:

    QGraphicsScene scene;
    int x1;
    int y1;
    int x2;
    int y2;

public:
    disegna (QWidget *parent = 0);
    void setdot(QString,QString);
    void setsegment(QString,QString,QString,QString);
    ~disegna(){}
    };

draw::draw(QWidget *parent) : QWidget(parent) { 
    scene.setBackgroundBrush(Qt::white);
    scene.addRect(QRectF(0,0, 600, 200));
    scene.addLine(0, 100 ,600, 100);
    scene.addLine(300, 0 ,300, 200);
    scene.addEllipse(297.5,97.5,5,5,QPen(), QBrush(Qt::red));

    int i=0;
    for (int a=0;a<120;a++) {
        scene.addLine(i+5, 98 ,i+5, 102);
        i=i+5;
    }

    int j=0;
    for (int a=0;a<40;a++) {
        scene.addLine(298,j+5,302,j+5);
        j=j+5;
    }

    QGraphicsView * view = new QGraphicsView(&scene,this);
    view->show();
}

void draw::setdot(QString x1,QString y1){
    scene.addEllipse(x1.toInt()+298, 98-y1.toInt(),4,4,QPen(), QBrush(Qt::blue));
}

void draw::setsegment(QString x1,QString y1,QString x2,QString y2) {
    scene.addLine(x1.toInt()+300, 100-y1.toInt(),x2.toInt()+300, 100-y2.toInt(),QPen());
    scene.addEllipse(x1.toInt()+298, 100-y1.toInt(),4,4,QPen(), QBrush(Qt::blue));
    scene.addEllipse(x2.toInt()+298, 100-y2.toInt(),4,4,QPen(), QBrush(Qt::blue));
}

最佳答案

为了清理现场,你试过clear吗?方法?

如果你想清除和重绘,我建议你将绘图代码从你的构造函数移到一个方法中。 然后你可以调用 clear 然后你的绘图方法。

希望对你有帮助

关于c++ - QGraphicsScene清晰显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12246267/

相关文章:

c++ - 将 std::get 作为参数传递给函数的函数对象

c++ - OpenGL 平截头体、透视

qt - 将 Borland C++ Builder 移植到 Qt

python - MousePressEvent,QGraphicsView中的位置偏移

c++ - 字符串还没有声明,QT

c++ - 聚丙稀 | .h 文件 (C++)

qt - 如何告诉 Qt 始终在 QTableView 中显示编辑器?

c++ - 使 QGraphicsView 大小适合

c++ - 如何打印 QGraphicsView 的内容

c++ - 此代码是否会导致内存泄漏(Arduino)