C ++ and Qt - a problem with 2D graphics

Mission: draw two lines with different colors on the same chart with automatic stitching, adding dots in parts.

So what am I doing. Create the GraphWidget class inherited from QGraphicsView. Create a QGraphicsScene Member. Create 2 instances of QPainterPath and add them to graphicsScene.

Then I end up calling graphWidget.Redraw (), where it is called for QPainterPath.lineTo () for both instances. And I expect the appearance of these lines of graphic representation, but this is not so.

I am tired of reading Qt doc and forums. What am I doing wrong?

+3
source share
2 answers

, ? ? ? , . : , .

#include ...

class QUpdatingPathItem : public QGraphicsPathItem {
    void advance(int phase) {
        if (phase == 0)
            return;
        int x = abs(rand()) % 100;
        int y = abs(rand()) % 100;
        QPainterPath p = path();
        p.lineTo(x, y);
        setPath(p);
    }
};

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QGraphicsScene s;
    QGraphicsView v(&s);

    QUpdatingPathItem item;
    item.setPen(QPen(QColor("red")));
    s.addItem(&item);
    v.show();

    QTimer *timer = new QTimer(&s);
    timer->connect(timer, SIGNAL(timeout()), &s, SLOT(advance()));
    timer->start(1000);

    return a.exec();
}

- :

meh ...

QGraphicsPathItem, , . , -, , ( , QPainterPath ...)

QPainterPath p = gPath.path();
p.lineTo(0, 42);
gPath.setPath(p);

, - / . Qt . QGraphicsPathItem, advance(), . , , s.advance() .

http://doc.trolltech.com/4.5/qgraphicsscene.html#advance

+4

, .

// Constructor:
GraphWidget::GraphWidget( QWidget *parent ) :
        QGraphicsView(parent),
        bounds(0, 0, 0, 0)
{
    setScene(&scene);
    QPen board_pen(QColor(255, 0, 0));
    QPen nature_pen(QColor(0, 0, 255));
    nature_path_item = scene.addPath( board_path, board_pen );
    board_path_item  = scene.addPath( nature_path, nature_pen );
}

// Eventually called func:
void GraphWidget::Redraw() {
    if(motion) {
        double nature[6];
        double board[6];
        // Get coords:
        motion->getNature(nature);
        motion->getBoard(board);
        if(nature_path.elementCount() == 0) {
            nature_path.moveTo( nature[0], nature[1] );
        } else {
            nature_path.lineTo( nature[0], nature[1] );
        }
        if(board_path.elementCount() == 0) {
            board_path.moveTo( board[0], board[1] );
        } else {
            board_path.lineTo( board[0], board[1] );
        }
    }
}
+1

Source: https://habr.com/ru/post/1713380/


All Articles