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. Using graphicsview in a class and link to MainWindow form

Using graphicsview in a class and link to MainWindow form

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 4.8k 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.
  • N Offline
    N Offline
    ngc224
    wrote on last edited by
    #1

    Hi all, I'm working on a class that inherits a graphicsview and creates a new graphicsview instance. The problem is that the graphicsview comes out in a seperate window (apart from MainWindow), but I'd like to link it with the MainWindow form.

    Normally I would pass ui->centralwidget to the graphicsview from the MainWindow,

    [code]
    QGraphicsView *graphicsArea= new QGraphicsView(ui->centralwidget)
    [/code]

    in order to link the graphicsview with the MainWindow form. But since I'm doing it in a class, I've tried passing ui->centralwidget to the class but did not have any success. Can you please recommend any tips on a how to make this work? Thanks.

    [code]
    // Standard MainWindow.h generated by the qt compiler

    #include "mainwindow.h"
    #include "ui_mainwindow.h"

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

    }

    // Standard MainWindow.cpp generated by the qt compiler
    #include "mainwindow.h"
    #include "ui_mainwindow.h"

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

    //Declare new graphics window
    NewGraphicsWindow graphicsArea;

    graphicsArea.window->setGeometry(QRect(50, 50, 400, 200));
    graphicsArea.window->show();

    }

    // Class NewGraphicsWindow
    #ifndef NEWGRAPHICSWINDOW_H
    #define NEWGRAPHICSWINDOW_H

    #include <QGraphicsView>
    #include <QGraphicsScene>

    class NewGraphicsWindow:QGraphicsView,QGraphicsScene

    {
    public:
    NewGraphicsWindow();

    QGraphicsView *window = new QGraphicsView();
    

    };

    #endif // NEWGRAPHICSWINDOW_H

    [/code]

    1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      welcome to the forum.

      @ NewGraphicsWindow graphicsArea;

      graphicsArea.window->setGeometry(QRect(50, 50, 400, 200));
      graphicsArea.window->show();@

      You are calling show separately. That is why it launches it separatey. also you are creating the stack variable there.

      @Did you try MainWindow->setCentralWidget(graphicsView).@

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      1 Reply Last reply
      0
      • N Offline
        N Offline
        ngc224
        wrote on last edited by
        #3

        I see, thanks for the reply. I've tried it but it's giving an error "expected unqualified-id before '->' token."

        1 Reply Last reply
        0
        • dheerendraD Offline
          dheerendraD Offline
          dheerendra
          Qt Champions 2022
          wrote on last edited by
          #4

          Can you paste your code here on how you are doing. Here problem may be due to stack variable used with -> rather than ".". Just check. What you are hitting is compilation issue and you may solve the problem easily.

          Dheerendra
          @Community Service
          Certified Qt Specialist
          http://www.pthinks.com

          1 Reply Last reply
          0
          • N Offline
            N Offline
            ngc224
            wrote on last edited by
            #5

            Sure. Here is the MainWindow code. I'm not sure if I'm using your suggestion correctly.

            [code]
            #include "mainwindow.h"
            #include "ui_mainwindow.h"

            #include "newgraphicswindow.h"

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

            //Declare new graphics window
            NewGraphicsWindow graphicsArea;
            
            MainWindow->setCentralWidget(graphicsArea);
            
            graphicsArea.window->setGeometry(QRect(50, 50, 400, 200));
            graphicsArea.window->show();
            

            }
            [/code]

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              In addition, this:

              @
              class NewGraphicsWindow:QGraphicsView,QGraphicsScene

              {
              public:
              NewGraphicsWindow();

              QGraphicsView *window = new QGraphicsView();
              

              };
              @

              looks wrong in many ways. You can't subclass from both since they both inherit from QObject, and there's also no sense for. A QGraphicsScene is meant to be set on a QGraphicsView.

              Also, why do you have a public member QGraphicsView * ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • N Offline
                N Offline
                ngc224
                wrote on last edited by
                #7

                [quote author="SGaist" date="1409687502"]Hi,

                In addition, this:

                @
                class NewGraphicsWindow:QGraphicsView,QGraphicsScene

                {
                public:
                NewGraphicsWindow();

                QGraphicsView *window = new QGraphicsView();
                

                };
                @

                looks wrong in many ways. You can't subclass from both since they both inherit from QObject, and there's also no sense for. A QGraphicsScene is meant to be set on a QGraphicsView.

                Also, why do you have a public member QGraphicsView * ?[/quote]

                First I'd just like to create a QGraphicsView in a class and have it put in the MainWindow UI form, instead of it being launched separately. If it works, I can add the QGraphicsScene later.

                No reason why QGraphcicsView is public, the sample code is just a test program which creates a QGraphicsView separately, and MainWindow form separately. Making it private will not change it.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  It wasn't a public vs private question, but why are you inheriting from a class and keep a member variable of that same class in that class ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • dheerendraD Offline
                    dheerendraD Offline
                    dheerendra
                    Qt Champions 2022
                    wrote on last edited by
                    #9

                    @MainWindow::MainWindow(QWidget *parent)
                    : QMainWindow(parent)
                    {
                    QGraphicsView *view = new QGraphicsView;
                    QGraphicsScene *scene = new QGraphicsScene;
                    view->setScene(scene);
                    for (int x=0;x<100;x+=10){
                    for (int y=0;y<100;y+=10){
                    QGraphicsEllipseItem *item= new QGraphicsEllipseItem(x,y,8,8);
                    item->setBrush(Qt::magenta);
                    scene->addItem(item);
                    }
                    }
                    this->setCentralWidget(view);
                    }
                    @

                    Dheerendra
                    @Community Service
                    Certified Qt Specialist
                    http://www.pthinks.com

                    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