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. Cannot place any QGraphicsItems on QGraphicsScene anywhere but at 0,0

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
qgraphicsviewqgraphicssceneqgraphicsellips
8 Posts 2 Posters 2.6k Views
  • 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.
  • arortellA Offline
    arortellA Offline
    arortell
    wrote on last edited by
    #1

    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.

    //Header
    <code>
    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();

    protected:
    void createActions( );
    void createMenus( );

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

    //slots:
    // void levelUp();

    private:
    //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 ();
    

    }
    </code>

    1 Reply Last reply
    0
    • BuckwheatB Offline
      BuckwheatB Offline
      Buckwheat
      wrote on last edited by
      #2

      @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.

      Dave Fileccia

      arortellA 2 Replies Last reply
      0
      • BuckwheatB Buckwheat

        @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.

        arortellA Offline
        arortellA Offline
        arortell
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • BuckwheatB Buckwheat

          @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.

          arortellA Offline
          arortellA Offline
          arortell
          wrote on last edited by
          #4

          @Buckwheat that gives me
          error: no matching constructor for initialization of 'QRectF'
          m_scene->setSceneRect((QRectF(0,0),event->size()));
          ^ ~~~

          1 Reply Last reply
          0
          • arortellA Offline
            arortellA Offline
            arortell
            wrote on last edited by
            #5

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

            1 Reply Last reply
            0
            • arortellA Offline
              arortellA Offline
              arortell
              wrote on last edited by
              #6

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

              1 Reply Last reply
              0
              • arortellA Offline
                arortellA Offline
                arortell
                wrote on last edited by
                #7

                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

                arortellA 1 Reply Last reply
                0
                • arortellA arortell

                  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

                  arortellA Offline
                  arortellA Offline
                  arortell
                  wrote on last edited by
                  #8

                  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

                  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