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



  • 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?


  • Moderators

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



  • @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?


  • Moderators

    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?



  • @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)?


  • Moderators

    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.


Log in to reply
 

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