Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Drawing QLineF objects from centre of QGraphicsScene. Keep start of both as origin

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

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 746 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    coffee-bean
    wrote on last edited by
    #1

    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 :)

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved