QtCreator内部加载examples是这样子的:
1.先找Qt Doc目录(可以使用qmake -query QT_INSTALL_DOCS命令查看相关路径),然后从它的每个子目录下查找'examples-manifest.xml'和'demos-manifest.xml'文件。
2.找到了之后,以qt gui 的'Analog Clock Window Example'为例,比如Qt Doc是在/usr/share/qt5/doc下,那么该示例就在/usr/share/qt5/doc/qtgui/examples-manifest.xml文件中记录着,如:
<example name="Analog Clock Window Example" docUrl="qthelp://org.qt-project.qtgui.5113/qtgui/qtgui-analogclock-example.html" projectPath="gui/analogclock/analogclock.pro" imageUrl="qthelp://org.qt-project.qtgui.5113/qtgui/images/analogclock-window-example.png" isHighlighted="true">
<description><![CDATA[The Analog Clock Window example shows how to draw the contents of a custom window.]]></description> <tags>analog,android,clock,gui,ios,window</tags>
<fileToOpen mainFile="true">gui/analogclock/main.cpp</fileToOpen>
</example>
3.然后还要去验证该示例的对应值' projectPath'、'imageUrl'和'docUrl'是否有效,注意projectPath是指相对于example/demo本身的路径,可以使用qmake -query QT_INSTALL_EXAMPLES和qmake -query QT_INSTALL_DEMOS查看具体示例所在路径,我这边这两个是一样的,好像没什么区别。而imageUrl和docUrl则是相对于doc路径,比如该示例绝对路径分别为/usr/share/qt5/doc/qtgui/images/analogclock-window-example.png和/usr/share/qt5/doc/qtgui/qtgui-analogclock-example.html。
4.这样的话如果QtCreator不显示示例,可以去看看Qt Doc目录是否为空,还有使用qmake -query QT_INSTALL_EXAMPLES看下示例具体的目录是否为空,如果为空那显示不出来就正常。
5.QtCreator源码src/plugins/qtsupport/exampleslistmodel.cpp中有使用到环境变量'QTC_DEBUG_EXAMPLESMODEL'来调试examples加载过程:
static bool debugExamples()
{
static bool isDebugging = qEnvironmentVariableIsSet("QTC_DEBUG_EXAMPLESMODEL");
return isDebugging;
}
......
// read extra example sets settings
QSettings *settings = Core::ICore::settings();
const QStringList list = settings->value("Help/InstalledExamples", QStringList()).toStringList();
if (debugExamples())
qWarning() << "Reading Help/InstalledExamples from settings:" << list;
会去QtCreator.ini配置文件中查看'Help/InstalledExamples'对应example值,当然一般是没有的,因为我就没有看到。不过可以考虑把自定义的example放到这里去在QtCreator中显示。这个变量主要是调试输出使用的,比如qtcreator启动时输出:
Reading Help/InstalledExamples from settings: ()
"Reading file \":/qtsupport/qtcreator_tutorials.xml\"..."
"ERROR: Could not parse file as XML document (:/qtsupport/qtcreator_tutorials.xml)"
"Reading file \"/usr/share/qt5/doc/qdoc/examples-manifest.xml\"..."
"ERROR: Item \"QML Documentation Example\" broken: projectPath \"/usr/share/qt5/doc/qdoc/componentset/componentset.pro\" empty or does not exist"
"Reading file \"/usr/share/qt5/doc/qt3d/examples-manifest.xml\"..."
"Reading file \"/usr/share/qt5/doc/qtassistant/examples-manifest.xml\"..."
"Reading file \"/usr/share/qt5/doc/qtbluetooth/examples-manifest.xml\"..."
"Reading file \"/usr/share/qt5/doc/qtcharts/examples-manifest.xml\"..."
"ERROR: Item \"Audio Example\" broken: projectPath \"/usr/share/qt5/doc/qtcharts/charts/audio/audio.pro\" empty or does not exist"
"Reading file \"/usr/share/qt5/doc/qtconcurrent/examples-manifest.xml\"..."
"Reading file \"/usr/share/qt5/doc/qtcore/examples-manifest.xml\"..."
"Reading file \"/usr/share/qt5/doc/qtdbus/examples-manifest.xml\"..."
"Reading file \"/usr/share/qt5/doc/qtdesigner/examples-manifest.xml\"..."
"Reading file \"/usr/share/qt5/doc/qtdoc/examples-manifest.xml\"..."
"ERROR: Item \"Coffee Machine Example\" broken: projectPath \"/usr/share/qt5/doc/qtdoc/demos/coffee/coffee.pro\" empty or does not exist"
"Reading file \"/usr/share/qt5/doc/qtgui/examples-manifest.xml\"..."
"ERROR: Item \"Hello Vulkan Cubes Example\" broken: projectPath \"/usr/share/qt5/doc/qtgui/gui/hellovulkancubes/hellovulkancubes.pro\" empty or does not exist"
"ERROR: Item \"Hello Vulkan Texture Vulkan Example\" broken: projectPath \"/usr/share/qt5/doc/qtgui/gui/hellovulkantexture/hellovulkantexture.pro\" empty or does not exist"
"ERROR: Item \"Hello Vulkan Triangle Example\" broken: projectPath \"/usr/share/qt5/doc/qtgui/gui/hellovulkantriangle/hellovulkantriangle.pro\" empty or does not exist"
"ERROR: Item \"Hello Vulkan Widget Example\" broken: projectPath \"/usr/share/qt5/doc/qtgui/gui/hellovulkanwidget/hellovulkanwidget.pro\" empty or does not exist"
"ERROR: Item \"Hello Vulkan Window Example\" broken: projectPath \"/usr/share/qt5/doc/qtgui/gui/hellovulkanwindow/hellovulkanwindow.pro\" empty or does not exist"