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 to have two exclusive main windows using the same instance of a class?
Forum Updated to NodeBB v4.3 + New Features

How to have two exclusive main windows using the same instance of a class?

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 2 Posters 1.2k 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.
  • L Offline
    L Offline
    Lynxerious
    wrote on last edited by
    #1

    Hello, I'm a Qt beginner. I want to make a desktop application which is a music player.

    It has two main windows, the small one has the player interface, the big one has the player interface(same as the small one) and the music library manager. They both use the audio player instance to play music, but never show on desktop together. User can switch to either windows with a button (the Restore Down/Maximize custom button on my custom Title Bar). When user closes one, the application is closed. It's the same as AIMP3.

    How can I call two windows with same instance but not show at the same time? Do I have to use Singleton pattern with the audio player class? Do I need a class to manage the two windows?

    raven-worxR 1 Reply Last reply
    0
    • L Lynxerious

      Hello, I'm a Qt beginner. I want to make a desktop application which is a music player.

      It has two main windows, the small one has the player interface, the big one has the player interface(same as the small one) and the music library manager. They both use the audio player instance to play music, but never show on desktop together. User can switch to either windows with a button (the Restore Down/Maximize custom button on my custom Title Bar). When user closes one, the application is closed. It's the same as AIMP3.

      How can I call two windows with same instance but not show at the same time? Do I have to use Singleton pattern with the audio player class? Do I need a class to manage the two windows?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Lynxerious said:

      How can I call two windows with same instance but not show at the same time?

      what do you exactly mean with "call two windows with the same instance"?

      Or do you just want to make your code cleaner? If so then yes a wrapper/delegate-class would be good. Since every window/widget is a separate instance.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      L 1 Reply Last reply
      0
      • raven-worxR raven-worx

        @Lynxerious said:

        How can I call two windows with same instance but not show at the same time?

        what do you exactly mean with "call two windows with the same instance"?

        Or do you just want to make your code cleaner? If so then yes a wrapper/delegate-class would be good. Since every window/widget is a separate instance.

        L Offline
        L Offline
        Lynxerious
        wrote on last edited by Lynxerious
        #3

        @raven-worx sorry I'm not being very clear. I meant two windows will be using the same static instance of the class "Audio Player", and they never show up at the same time, you can only switch between them. I don't understand what do you mean by "wrapper/delegate-class", can you elaborate? And can I reuse ui elements and methods in a windows for another windows so I don't have to prevent copy paste code?

        1 Reply Last reply
        0
        • raven-worxR Offline
          raven-worxR Offline
          raven-worx
          Moderators
          wrote on last edited by
          #4

          yes, for the AudioPlayer use either a singleton pattern (return a pointer) or simply store it in a static/global variable.
          With wrapper/delegate class i meant a class which does some work for you. E.g. setting some attributes/properties on the 2 windows at the same time, etc.
          But thats not the case?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          L 1 Reply Last reply
          1
          • raven-worxR raven-worx

            yes, for the AudioPlayer use either a singleton pattern (return a pointer) or simply store it in a static/global variable.
            With wrapper/delegate class i meant a class which does some work for you. E.g. setting some attributes/properties on the 2 windows at the same time, etc.
            But thats not the case?

            L Offline
            L Offline
            Lynxerious
            wrote on last edited by
            #5

            @raven-worx I guess it is the case, thank you.

            Two more questions: Is it better to create 2 windows or just one window with two states (the ui elements are dynamically change in code)? If I am to create 2 windows, is there some way to use the same ui elements (for example, I have a btn_Play on window1 and want window2 to have the same button with same look and functionality without copying too much code)?

            1 Reply Last reply
            0
            • raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by raven-worx
              #6

              hard to tell without knowing your application in detail.
              This is question of code and visual design. Probably you can subclass one window from another? E.g. the FullMainWindow derived from SmallMainWindow?
              Also you can create reoccuring modules as custom widgets and instantiate them in each window.
              But an exact advise is hard. Reuse as much code as possible.

              Reusing the same elements is only possible when you reparent them each time you switch between windows. And i doubt that this is a ideal solution.

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1

              • Login

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