Cannot place any QGraphicsItems on QGraphicsScene anywhere but at 0,0

  • Hello guys, Let me start by thanking you for your help. I am trying to create a pong game. I have a QGraphicsView on the QGraphicsScene. I m trying to add a QGraphicsEllipseItem to make a ball but no what I use for coordiates it WILL NOT move or be created anywhere but 0,0. I have tried the same with a QRectf.

    class MainWindow : public QMainWindow

    MainWindow(QWidget *parent = 0);

    void createActions( );
    void createMenus( );

    void initializeGL();
    void paintGL();
    void resizeGL(int width, int height);

    // void levelUp();

    //functions public ????
    void drawBall( );
    void moveBall(qreal speed);

    QRadialGradient *m_gradient;
    QWidget *m_central_widget;
    QGraphicsView *m_view;
    QGraphicsScene *m_scene;
    QGraphicsEllipseItem *m_ball;
    QTimer *m_timer;
    QVBoxLayout *m_layout;
    QSpacerItem *m_spacer;
    // Menu widgets
    QMenuBar *m_menubar;
    QMenu *m_levelMenu;
    QMenu *m_helpMenu;
    QMenu *m_fullScreenMenu;
    QMenu *m_aboutMenu;
    // Actions for menu
    QActionGroup *m_level_actions;
    QAction *m_firstLevelAct;
    QAction *m_secondLevelAct;
    QAction *m_ThirdLevelAct;
    QAction *m_fullScreenAct;
    QAction *m_helpAct;
    QAction *m_aboutAct;
    qreal m_speed;

    #include "mainwindow.h"

    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    // ste central widget to add others
    m_central_widget = new QWidget;
    setCentralWidget (m_central_widget);

    // graphics necesssities
    m_view = new QGraphicsView(m_central_widget);
    m_scene = new QGraphicsScene(m_view);
    m_view->setScene (m_scene);
    // Vertical layout
    m_layout = new QVBoxLayout(m_central_widget);
    // this might need to be changed to allow for the walls later
    m_layout->setMargin (10);
    m_layout->addWidget (m_view);
    //set layout
    m_central_widget->setLayout (m_layout);
    // TODO: Play with this latter
    QString TEMP("This is just a place holer");
    this->statusBar ()->showMessage(TEMP);
    // TODO:Play with this later
    m_levelMenu = this->menuBar ()->addMenu ("Level");
    m_scene->setItemIndexMethod (QGraphicsScene::NoIndex);
    m_view->show ();
    drawBall ();


    MainWindow::~MainWindow() { }

    // TODO:
    void MainWindow::createActions( )
    m_fullScreenAct = new QAction("FullScreen", this);
    m_fullScreenAct->setStatusTip ("Open in FullScreen");
    connect(m_fullScreenAct, &QAction::triggered, this, &MainWindow::showFullScreen);

    void MainWindow::createMenus( ) { }

    void MainWindow::drawBall( )
    // m_gradient = new QRadialGradient(30, 30, 150);
    // m_gradient->setColorAt (0, Qt::white);
    // m_gradient->setColorAt (.25, Qt::black);
    // QBrush brush(*m_gradient);
    QBrush brush(Qt::blue);

    m_ball = m_scene->addEllipse (30, 30, 30, 30, QPen(Qt::white), brush);
    qDebug() << m_ball->x ();
    qDebug() << m_ball->y ();


    //TODO: Needs to be redone
    void MainWindow::moveBall(qreal speed)
    qreal newX = m_ball->x () + 20;
    qreal newY = m_ball->y () + 20;

    m_ball->moveBy (newX, newY);
    m_ball->setPos (newX, newY);
    m_view->repaint ();
    m_scene->update ();


  • @arortell ... There seems to be some pieces missing. Like sceneRect. You should also not have to call m_view->repaint () since you are updating the scene with m_scene->update ().

    The sceneRect will automatically update when items are added based on the union of all the boundingRects for the QGraphicsItems. You could try using setSceneRect and give your canvas a large area to play with. If the item goes outside the rect it will be clipped and not drawn. So, a small sceneRect will clip items fast.

    An easy way to make the sceneRect the size of the client area is to set it to the viewport rectangle in resizeEvent of the QGraphicsView. In your example, you would override QMainWindow::resizeEvent as follows:

    void MainWindow::resizeEvent (QResizeEvent* event)
    QMainWindow::resizeEvent (event); // This allows the main window to pass it on and handle it before you use it
    if (m_scene) m_scene->setScenRect (QRect (QPoint (0, 0), event->size ()));

    This should give you a good size sceneRect to play with.

  • @Buckwheat Thank you very much. I wil give it a try.

  • @Buckwheat that gives me
    error: no matching constructor for initialization of 'QRectF'
    ^ ~~~

  • I just don't understand I cannot get this thing to show up anywhere but 0,0. And it will not move.

  • Oky, new development. I just created a QGraphicsRectItem with all the same properties and it works.
    How weird is that?

  • Oky I had the QGraphicsEllipseItem pointer created in the header file. I move that to the cpp file and now it WORKS! If some could please explain to me why, I would be forever greatful. I am obviously very new to QT but I am loving it. It truly is incredible, the documentation is great, but I am having a hard time finding help. Thanks

  • Its not moving the pointer that is making it work. It is the QRect that I created to test. If I comment out
    the rect it all goes back to not moving and only being created at 0,0

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