12.8. Embedding More Than One Part in the Same Window
The previous example showed how to embed a part as the single widget of a window. KParts also makes it possible to embed more than one part in the same window, and it handles the activation of a part when the user clicks it (or uses Tab to give it the focus). This is the task of the PartManager.
To display more than one part in a window, the solution is usually to use a splitter, or even nested splitters, such as in Konqueror. KOffice has another way of embedding several parts—by using frames for the child parts—but it still uses PartManager.
Now modify the example to make it display, in addition to the PostScript document, the PostScript code for it. To display the text, the application uses the Notepad part in read-only mode. The two widgets will be hosted by a splitter.
Displaying raw PostScript is not very useful, but this example could, for instance, be turned into an application showing the LaTeX source and the PostScript result side by side.
ghostviewtest.h needs to be modified slightly to add the following private members:
ghostviewtest.cpp needs to be more modified. To include the PartManager definition, use the following:
In the constructor, create the part manager and connect its main signal, activePartChanged, to your createGUI slot. This means you don't need to call createGUI directly; it is called every time the active part changes.
Then create the splitter and transform the setView statement into the following:
so that the main widget is now the splitter. Both parts need to be created with the splitter as a parent (instead of the window):
After the parts are created, they should be added to the part manager. At the same time, you can specify which one should initially be active:
Then the splitter can be set to a minimum size, as shown:
Finally, add the following line to openURL() to open the same URL in both parts:
As you can see, the main idea is that the mainwindow creates a main widget (here, the splitter), creates all parts inside it, and registers the part to a part manager. Try clicking one part and then the other; each time the active part changes, the GUI is updated (both menus and toolbars) to show the GUI of the active part.
Note also the change in the window caption. This is handled by the Part class, which receives the GUIActivateEvent from the mainwindow when the part is activated or deactivated. To set a different caption for a part, you need to emit setWindowCaption both in openFile() and in guiActivateEvent().