سؤال

I'm trying to write an interface around my program (using qt). Basically I have a main window, and then at certain points it should "ask the user for interaction". - I'm using a dialog for this.

Now when reading about dialog windowses I read somewhere that a dialog window should always have a parent window (the main window I guess?). Now I find this troublesome to implement in my project - as it seems I have to change code I do not wish to change.

Basically the main program used to be a command line interface. And for user interaction the program would just create a class which handles the displaying & user interaction. In the GUI version I derive from that class, and the derived class simply wraps the correct dialog window.

Now the trouble is that in the command line interface there is no talking about "windows" or even "qt". - The backbone application should know nothing of qt. The communication is simply like this:

MAIN_WINDOW -> tells backbone to start acting upon X 
    -> BACKBONE -> asks for UI (opens class with DIALOG).

Now the trouble is: how do I get MAIN_WINDOW pointer through BACKBONE (which knows nothing of qt) to DIALOG? Is this a case where I have to use a singleton/global variable for MAIN_WINDOW? I hope there's a better paradigm?

هل كانت مفيدة؟

المحلول

You can get the main window of your application in your dialog with QApplication::instance()->activeWindow(), intance() is a static function which can be called without having access to the QApplication object (which is actually internally implemented as singleton).

http://developer.qt.nokia.com/doc/qt-4.8/qapplication.html#activeWindow

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top