定制 Qt Assistant

使用 Qt Assistant as custom help viewer requires more than just being able to display custom documentation. It is equally important that the appearance of Qt Assistant can be customized so that it is seen as a application-specific help viewer rather than Qt Assistant . This is achieved by changing the window title or icon, as well as some application-specific menu texts and actions. For a complete list of possible customizations, see 创建自定义帮助集合文件 .

Another requirement of a custom help viewer is the ability to receive actions or commands from the application it provides help for. This is especially important when the application offers context sensitive help. When used in this way, the help viewer may need to change its contents depending on the state the application is currently in. This means that the application has to communicate the current state to the help viewer. For more information, see 远程使用 Qt Assistant .

The 简单文本查看器 example uses the techniques described in this document to show how to use Qt Assistant as a custom help viewer for an application.

警告: In order to ship Qt Assistant in your application, it is crucial that you include the sqlite plugin. For more information on how to include plugins in your application, refer to the deployment documentation .

QHC (Qt 帮助集合) 文件

The first important point to know about Qt Assistant is that it stores all settings related to its appearance and a list of installed documentation in a help collection file. This means, when starting Qt Assistant with different collection files, Qt Assistant may look totally different. This complete separation of settings makes it possible to deploy Qt Assistant as a custom help viewer for more than one application on one machine without risk of interference between different instances of Qt Assistant .

To apply a certain help collection to Qt Assistant , specify the respective collection file on the command line when starting it. For example:

assistant -collectionFile mycollection.qhc
					

However, storing all settings in one collection file raises some problems. The collection file is usually installed in the same directory as the application itself, or one of its subdirectories. Depending on the directory and the operating system, the user may not have any permissions to modify this file which would happen when the user settings are stored. Also, it may not even be possible to give the user write permissions, for example when the file is located on a read-only medium like a CD-ROM.

Even if it is possible to give everybody the right to store their settings in a globally available collection file, the settings from one user would be overwritten by another user when exiting Qt Assistant .

To solve this dilemma, Qt Assistant creates user specific collection files which are more or less copied from the original collection file. The user-specific collection file will be saved in a subdirectory of the path returned by QDesktopServices::AppDataLocation. The subdirectory, or cache directory within this user-specific location, can be defined in the help collection project file. For example:

<?xml version="1.0" encoding="utf-8" ?>
<QHelpCollectionProject version="1.0">
    <assistant>
        <title>My Application Help</title>
        <cacheDirectory>mycompany/myapplication</cacheDirectory>
        ...
    </assistant>
</QHelpCollectionProject>
					

So, when calling

assistant -collectionFile mycollection.qhc
					

Qt Assistant actually uses the collection file:

%QDesktopServices::AppDataLocation%/mycompany/myapplication/mycollection.qhc
					

There is no need ever to start Qt Assistant with the user specific collection file. Instead, the collection file shipped with the application should always be used. Also, when adding or removing documentation from the collection file (see next section) always use the normal collection file. Qt Assistant will take care of synchronizing the user collection files when the list of installed documentation has changed.

显示自定义文档编制

Before Qt Assistant is able to show documentation, it has to know where it can find the actual documentation files, meaning that it has to know the location of the Qt compressed help file (*.qch). As already mentioned, Qt Assistant stores references to the compressed help files in the currently used collection file. So, when creating a new collection file you can list all compressed help files Qt Assistant should display.

<?xml version="1.0" encoding="utf-8" ?>
<QHelpCollectionProject version="1.0">
    ...
    <docFiles>
        <register>
            <file>myapplication-manual.qch</file>
            <file>another-manual.qch</file>
        </register>
    </docFiles>
</QHelpCollectionProject>
					

Sometimes, depending on the application for which Qt Assistant acts as a help viewer, more documentation needs to be added over time; for example, when installing more application components or plugins. This can be done manually in Qt Assistant by selecting 编辑 > 首选项 > 文档编制 . However, this approach has the disadvantage that every user has to do it manually to get access to the new documentation.

The preferred way of adding documentation to an already existing collection file is to use the -register command line flag of Qt Assistant . When starting Qt Assistant with this flag, the documentation will be added and Qt Assistant will exit right away displaying a message if the registration was successful or not.

The search indexing will only index your custom *.html, *.htm, and *.txt files.

assistant -collectionFile mycollection.qhc -register myapplication-manual.qch
					

The -quiet flag can be passed on to Qt Assistant to prevent it from writing out the status message.

注意: Qt Assistant shows the documentation in the 内容 view in the same order as it was registered.

更改 Qt Assistant 的外观

The appearance of Qt Assistant can be changed by passing different command line options on startup. However, these command line options only allow to show or hide specific widgets, like the contents or index view. Other customizations, such as changing the application title or icon, or disabling the filter functionality, can be done by creating a custom help collection file.

创建自定义帮助集合文件

The help collection file (*.qhc) used by Qt Assistant is created when running the qhelpgenerator tool on a help collection project file (*.qhcp). The project file format is XML and it supports the following tags:

Tag 简要描述
<title> Specifies a window title for Qt Assistant .
<homePage> Specifies the page to display when selecting 首页 Qt Assistant main window.
<startPage> Specifies the page to display initially when the help collection is used.
<currentFilter> Specifies the filter that is initially used. If this filter is not specified, the documentation will not be filtered. This has no impact if only one documentation set is installed.
<applicationIcon> Describes an icon that will be used instead of the normal Qt Assistant application icon. This is specified as a relative path from the directory containing the collection file.
<enableFilterFunctionality> Enables or disables user accessible filter functionality, making it possible to prevent the user from changing any filter when running Qt Assistant . It does not mean that the internal filter functionality is completely disabled. Set the value to false if you want to disable the filtering. If the filter toolbar should be shown by default, set the attribute visible to true .
<enableDocumentationManager> Shows or hides the 文档编制 tab in the 首选项 dialog. Disabling the 文档编制 tab allows you to limit Qt Assistant to display a specific documentation set or make it impossible for the end user to accidentally remove or install documentation. To hide the 文档编制 tab, set the tag value to false .
<enableAddressBar> Enables or disables the address bar functionality. By default it is enabled. To disable it, set the tag value to false . If the address bar functionality is enabled, the address bar can be shown by setting the tag attribute visible to true .
<aboutMenuText>, <text> Lists localized versions for the 关于 menu item in the 帮助 menu. For example, About Application . The text is specified within the text tags. The 语言 attribute takes the two letter language name. The text is used as the default text if no language attribute is specified.
<aboutDialog>, <file>, <icon> Specifies the text for the 关于 dialog that can be opened from the 帮助 menu. The text is taken from the file in the file tags. It is possible to specify a different file or any language. The icon defined by the icon tags is applied to any language.
<cacheDirectory>, <cacheDirectory base="collection"> Specifies the cache directory that is used to store index files needed for the full text search and a copy of the collection file. The copy is needed because Qt Assistant stores all its settings in the collection file, and therefore, it must be writable for the user. The directory is specified as a relative path. If the base attribute is set to "collection", the path is relative to the directory the collection file resides in. If the attribute is set to "default" or if it is missing, the path is relative to the directory given by QDesktopServices::AppDataLocation. The first form is useful for collections that are used in a mobile way, such as carried around on a USB stick.
<enableFullTextSearchFallback> Enables or disables the ability to fallback and use the full text search if a keyword cannot be found in the index. This functionality can be used while remote controlling Qt Assistant . To make it available for remote control, set the tag value to true .

In addition to those Qt Assistant specific tags, the tags for generating and registering documentation can be used. See QHC (Qt 帮助集合) 文件 文档编制,了解更多信息。

An example of a help collection file that uses all the available tags is shown below:

<?xml version="1.0" encoding="utf-8" ?>
<QHelpCollectionProject version="1.0">
    <assistant>
        <title>My Application Help</title>
        <startPage>qthelp://com.mycompany.1_0_0/doc/index.html</startPage>
        <currentFilter>myfilter</currentFilter>
        <applicationIcon>application.png</applicationIcon>
        <enableFilterFunctionality>false</enableFilterFunctionality>
        <enableDocumentationManager>false</enableDocumentationManager>
        <enableAddressBar visible="true">true</enableAddressBar>
        <cacheDirectory>mycompany/myapplication</cacheDirectory>
        <aboutMenuText>
            <text>About My Application</text>
            <text language="de">Über meine Applikation...</text>
        </aboutMenuText>
        <aboutDialog>
            <file>about.txt</file>
            <file language="de">ueber.txt</file>
            <icon>about.png</icon>
        </aboutDialog>
    </assistant>
    <docFiles>
        <generate>
            <file>
                <input>myapplication-manual.qhp</input>
                <output>myapplication-manual.qch</output>
            </file>
        </generate>
        <register>
            <file>myapplication-manual.qch</file>
        </register>
    </docFiles>
</QHelpCollectionProject>
					

To create the binary collection file, run the qhelpgenerator 工具:

qhelpgenerator mycollection.qhcp -o mycollection.qhc
					

To test the generated collection file, start Qt Assistant in the following way:

assistant -collectionFile mycollection.qhc
					

远程使用 Qt Assistant

Even though the help viewer is a standalone application, it will mostly be launched by the application it provides help for. This approach gives the application the possibility to ask for specific help contents to be displayed as soon as the help viewer is started. Another advantage with this approach is that the application can communicate with the help viewer process and can therefore request other help contents to be shown depending on the current state of the application.

So, to use Qt Assistant as the custom help viewer of your application, simply create a QProcess and specify the path to the Qt Assistant executable. In order to make Qt Assistant listen to your application, turn on its remote control functionality by passing the -enableRemoteControl 命令行选项。

The following example shows how this can be done:

QProcess *process = new QProcess;
QStringList args;
args << QLatin1String("-collectionFile")
    << QLatin1String("mycollection.qhc")
    << QLatin1String("-enableRemoteControl");
process->start(QLatin1String("assistant"), args);
if (!process->waitForStarted())
    return;
					

一旦 Qt Assistant is running, you can send commands by using the stdin channel of the process. The code snippet below shows how to tell Qt Assistant to show a certain page in the documentation.

QByteArray ba;
ba.append("setSource qthelp://com.mycompany.1_0_0/doc/index.html\n");
process->write(ba);
					

注意: The trailing newline character is required to mark the end of the input.

The following commands can be used to control Qt Assistant :

Command 简要描述
show <Widget> Shows the sidebar window (dock widget) specified by <Widget>. If the widget is already shown and this command is sent again, the widget will be activated, meaning that it will be raised and given the input focus. Possible values for <Widget> are "contents", "index", "bookmarks" or "search".
hide <Widget> Hides the dock widget specified by <Widget>. Possible values for <Widget> are "contents", "index", "bookmarks" and "search".
setSource <Url> Displays the given <Url>. The URL can be absolute or relative to the currently displayed page. If the URL is absolute, it has to be a valid Qt help system URL. That is, starting with "qthelp://".
activateKeyword <Keyword> Inserts the specified <Keyword> into the line edit of the index dock widget and activates the corresponding item in the index list. If such an item has more than one link associated with it, a topic chooser will be shown.
activateIdentifier <Id> Displays the help contents for the given <Id>. An ID is unique in each namespace and has only one link associated to it, so the topic chooser will never pop up.
syncContents Selects the item in the contents widget which corresponds to the currently displayed page.
setCurrentFilter <filter> Selects the specified filter and updates the visual representation accordingly.
expandToc <Depth> Expands the table of contents tree to the given depth. If depth is 0, the tree will be collapsed completely. If depth is -1, the tree will be expanded completely.
register <help file> Adds the given Qt compressed help file to the collection.
unregister <help file> Removes the given Qt compressed help file from the collection.

If you want to send several commands within a short period of time, it is recommended that you write only a single line to the stdin of the process instead of one line for every command. The commands have to be separated by a semicolon, as shown in the following example:

QByteArray ba;
ba.append("hide bookmarks;");
ba.append("hide index;");
ba.append("setSource qthelp://com.mycompany.1_0_0/doc/index.html\n");
process->write(ba);
					

使用 Qt Assistant 许可和归属