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. how we can define global QGraphicsScene *scene2; in our source?
QtWS25 Last Chance

how we can define global QGraphicsScene *scene2; in our source?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 793 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.
  • stackprogramerS Offline
    stackprogramerS Offline
    stackprogramer
    wrote on last edited by
    #1

    hi,in my source i had QGraphicsScene *scene2;
    my question is how i can convert it to global class that is a access able by other class?

    QGraphicsScene *scene2;
        QGraphicsView *view2;
    

    thanks for reply

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      hi
      There is no such thing as globally classes that are available to all classes.

      While it is possible to make them global to whole program its bad idea and leads to
      buggy code fast - so I wont talk about globals anymore. :)

      However, the correct/better way is via the constructor.

      example:
      mainwindow class has
      QGraphicsScene *scene2;
      QGraphicsView *view2;

      and we have this other class TheAmazingViewer dialog that need to use these.

      so in mainwindow we have the

      void OpenViewer() {
      TheAmazingViewer myAView ( scene2, view2); <---- we give via constructor
      myAView.exev();
      }

      the TheAmazingViewer constructor will then keep those 2 parameters for use in its functions.

      Hope it makes sense
      :)

      stackprogramerS 1 Reply Last reply
      1
      • mrjjM mrjj

        hi
        There is no such thing as globally classes that are available to all classes.

        While it is possible to make them global to whole program its bad idea and leads to
        buggy code fast - so I wont talk about globals anymore. :)

        However, the correct/better way is via the constructor.

        example:
        mainwindow class has
        QGraphicsScene *scene2;
        QGraphicsView *view2;

        and we have this other class TheAmazingViewer dialog that need to use these.

        so in mainwindow we have the

        void OpenViewer() {
        TheAmazingViewer myAView ( scene2, view2); <---- we give via constructor
        myAView.exev();
        }

        the TheAmazingViewer constructor will then keep those 2 parameters for use in its functions.

        Hope it makes sense
        :)

        stackprogramerS Offline
        stackprogramerS Offline
        stackprogramer
        wrote on last edited by
        #3

        @mrjj

        thanks for guiding me.your idea is not better even it's best:).

        mrjjM 1 Reply Last reply
        0
        • stackprogramerS stackprogramer

          @mrjj

          thanks for guiding me.your idea is not better even it's best:).

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @stackprogramer
          Hi
          The concept of using constructors for such things is part of something they call RAII
          http://www.tomdalling.com/blog/software-design/resource-acquisition-is-initialisation-raii-explained/

          :)

          1 Reply Last reply
          2

          • Login

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