Drawing QLineF objects from centre of QGraphicsScene. Keep start of both as origin



  • Hi all,

    This is more of a 'how do I do this' than anything else. Basically, I'd like to draw two lines from the centre of a graphics scene, but keep the centre of the QGrahpicsScene as the origin, without the centre of the two lines moving (better described here https://www.youtube.com/watch?v=b35JF4LqtBs at 6:20) making the centre of the two lines the origin. What I would like is the start X,Y of the two lines (where they meet) to be fixed the the centre of the QGraphicsView.

    Here's my code:

    dialog.h
    @
    #ifndef DIALOG_H
    #define DIALOG_H

    #include <QDialog>
    #include <QtCore>
    #include <QtGui>
    #include <QGraphicsScene>
    #include <QGraphicsItem>
    #include <QLineF>

    namespace Ui {
    class Dialog;
    }

    class Dialog : public QDialog
    {
    Q_OBJECT

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

    private:
    Ui::Dialog *ui;
    QGraphicsScene *scene;
    QLineF angleLine1;
    QLineF angleLine2;
    QPointF centre;

    };

    #endif // DIALOG_H
    @

    dialog.cpp
    @
    #include "dialog.h"
    #include "ui_dialog.h"

    Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
    {
    ui->setupUi(this);

    //Create the scene
    scene = new QGraphicsScene(this);
    //send it to the graphics view
    ui->graphicsView->setScene(scene);
    
    
    QPen blackPen(Qt::black);
    QPen redPen(Qt::red);
    redPen.setWidth(3);
    blackPen.setWidth(3);
    
    //get centre coordinates
    int centX, centY;
    centX = ( (ui->graphicsView->width()) / 2);
    centY = ( (ui->graphicsView->height()) / 2);
    
    //QPoint object for centre of graphics widget
    centre = QPointF(centX, centY);
    
    //set the start as the centre point etc
    angleLine1.setP1(centre);
    angleLine1.setAngle(45);
    angleLine1.setLength(100);
    scene->addLine(angleLine1, blackPen);
    
    angleLine2.setP1(centre);
    angleLine2.setAngle(-5);
    angleLine2.setLength(150);
    scene->addLine(angleLine2, redPen);
    

    }

    Dialog::~Dialog()
    {
    delete ui;
    }

    @

    angleLine1 and angleLine2 meet at the origin (centre of QGraphicsView), then move, making the centre of both lines at the origin

    !http://i1380.photobucket.com/albums/ah174/omarplymomarplym/ScreenShot2014-11-14at214132_zps414d21e4.png(QGraphicsScene)!

    What I'd like is where line1 and line2 start to stay at the centre of the QGraphicsScene, any idea how to do this?

    Many thanks
    Omar :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.