PDA

View Full Version : General approach for a document-based app



sdfisher
28th March 2009, 15:09
Here's what I'm trying to create:

Application without a document open is a single window with a menu bar.
The first document replaces that window.
Subsequence documents open an entirely new window.
The program doesn't close until all windows are closed.
On Mac OS X, the behavior is much the same, except I don't want to see an empty window. Instead, I just have the menu bar. Also, closing the last window wouldn't close the program.


What's the general approach for this kind of application?

wysota
28th March 2009, 23:02
It depends what you call an "approach". In general you want to implement an SDI interface with each document in a separate window. The answer is to keep all the document related data inside the window and spawn new windows (and documents) when a new document is requested. So you need a kind of "controller" object that will spawn windows and the windows themselves. Probably the "new", "open" and "close" actions of each window should be connected to the controller somehow (either they should reside in the controller and all windows would share the actions or each window has its own set of actions and emits a signal to the controller that a new document was requested).

sdfisher
29th March 2009, 06:32
Right, it's basically an SDI interface, but I want it to work well on Mac OS X, too. If you can imagine, say, Microsoft Word on Windows pre-ribbon and Microsoft Word on Mac you've basically got the idea. It's a very standard UI. :)

What I don't understand is which Qt classes to use.

If I use QMainWindow, I'll always have a window open, including on Mac OS X. This isn't a good solution, though maybe I can have some sort of welcome screen that hides itself when a document is opened.
If I use a QWidget subclass, closing the last window will close the application, plus I won't get an acceptable "empty" window on Windows and Linux. Plus I'll need to implement the menu bar entirely in code.

I have some experience writing Qt applications, but so far they've been a single main window configuration UI and I haven't dealt with a document-based application. On the other hand, I have experience writing document-based applications, just not on Qt. It's the intersection of how to do document-based with Qt's GUI classes that I'm drawing a blank at.

I've been looking through the examples, but haven't seen anything applicable yet. The webbrowser sample looks the closest to what i want, but when all windows are closed on the Mac it loses its menu bar.

wysota
29th March 2009, 09:18
Let's clear some things.
1. QMainWindow is a QWidget subclass
2. Closing last widget will not exit the application if you disconnect the lastWindowClosed() signal from the quit() slot in the application regardless of the window types you use
3. In Qt menu bar is related to some window, so without a window you won't have a menu bar
4. You might get away with having an invisible or out-of-screen window to have a menu bar but this is not an optimal solution. It might be better to spawn the menu with native code or to look for an attribute in Qt allowing to do so. I would also try instantiating a floating QMenuBar object and see if it is enough to move the menu bar to the top of the screen.