Who wasn’t trapped by this problem ? If you are one of these lucky coders, please raise your hand in order that we give you a standing ovation.
OK, we can now sit back down (or take any position you feel more comfortable with).
The problem I’m talking about is this so annoying QML Import that keeps underlined in red although you’re using a kit including this exact version of the module.
For instance, I’m using below the version 5.10.1 of Qt. I create a new QML project and… QtCreator gets it wrong ! It tells me it has made a mistake generating the template code.
How the hell could this happen ?
Well, maybe that’s not entirely Qt Creator’s fault, and we can try to help it understanding what we are doing.
Setting the code model
In the following, I make the assumption that everything is OK with your installation of Qt. The only problem you encounter is this syntax checking (and code completion) problem. In particular you’re actually using a correctly configured kit.
If not, have an eye there.
So, let’s RTFM a bit. Yes, I know that’s insane, but admit that you also do from time to time, maybe in secret, not telling your fellows, and, perhaps, not even confessing to yourself that you actually like it.
In the previous documentation page, you may have spotted this interesting paragraph about resetting the core model. I personnaly find it barely understandable, but I fail reformulating in it a better way.
First, it warns you that things can go wrong. That’s life :
If you change the build and run kit when you have QML files open in the code editor, the code model might become corrupt.
In order to set things right, it encourages you to reset the code model :
To reset the code model, select Tools > QML/JS > Reset Code Model.
As you feel so happy to have found a simple solution, you obey and click. If it happens that things are now right for you (which I doubt, otherwise you wouldn’t be reading this), you may just stop investigating and continue your happy coding. I don’t say farewell, but see you next time.
So, the Qt documentation continues, asking us for a change :
If this does not help, try changing the QML emulation layer to the one that was built with the same Qt version as the one selected in the build and run kit.
OK, let’s do that. Then, the documentation redirects you to some Qt Quick Designer stuff. I’m not using Qt Designer, so why on earth should I consider reading this part of the manual further ?
Well, let’s do it anyway. It begins telling us things about how Qt Designer works :
Qt Quick Designer uses a QML emulation layer (also called QML Puppet) to render and preview images and to collect data. To be able to render custom types correctly from QML modules, the emulation layer must be built with the same Qt version as the QML modules.
I don’t care at all, as I do not use it. But pay attention to this paragraph :
To use an emulation layer that is built with the Qt configured in the build and run kit for the project, select Tools > Options > Qt Quick > Qt Quick Designer > Use QML emulation layer which is built by the selected Qt radio button.
OK, let’s do it.
Apply, close the dialog and you’re done : you have configured a tool that you maybe do not use (Qt Designer) in order to configure a part of Qt Creator that you indeed use (syntax checking). Well, at least, that should be fixed, now.
Using the code model set
If you now look at your code you can see that… the error remains here, warming the light of your screen.
The reason is simple : setting things right has had absolutely no impact on the code model used.
I have intentionnaly jumped this sentence in the previous documentation excerpt :
Qt Creator builds the emulation layer when you select the Design mode.
Obviously, as you felt in a Qt Designer area, there are actions linked to this tool to make. Entering the Design mode is one of it.
But it is not sufficient (I had several projects open in Qt Creator, and the error below occurs in another project, not the brand new one created for this article) :
Open your project file (extension .pro) and notice that the
QML_IMPORT_PATH variable is not set.
Set it to the path of your Qt version, the one path you’ve set to be used as the emulation layer.
And that works !
At last, you now can see your code with no red line telling you that QtCreator created incorrect code, and, probably more importantly, you gained access again to code completion related to the version of Qt you’re using, and not an old or wrong one.
Hey, it does not work for me !
Sometimes, for a reason, it ceases to work. I encourage you to go into a step by step analysis of what’s going wrong and to pay attention to the smallest details.
In my case, everything seemed all right until I reset the code model. Why did I reset it ? To be sure that I was in a stable situation. And I wasn’t.
If you look again at the previous error message, you may notice the import path I had set. You may also see that there are actually two values used for the import path. In my case, the second one is set by another project. This project is open. But it is neither the active one nor the project containing the file for which an error is incorrectly reported.
I would have guessed that, if you define a
QML_IMPORT_PATH for a project, it would be used for that project, and only that project. But it looks like it is not working this way. It seems that all the
QML_IMPORT_PATH defined in the different projects are concatenated and shared in a one global
QML_IMPORT_PATH. At least in this Designer error message, and we know that the Designer behaviour is a special case as it has got its own variable to handle it :
QML_DESIGNER_IMPORT_PATH. But this particular variable is set nowhere in my projects.
Well, I’ve not investigated further, as I’ve got a workaround for this particular case. It would ask to go deeper in the internals of this functionnality, but I do not want to spend more time on the subject, as it works for me right now.
If I admit that an incorrect value for a project may lead to problems for this project having an erroneous configuration, I do not understand why it contaminates a correctly configured project edited in a correctly configured Qt Creator.
Actually, playing with these values in order to discover what was the root cause of the problem, I unintentionally made QtCreator crash.
If you know more about this, please leave a comment.