Setting up Macvim and Qt 5.x on OSX

I really love the Qt toolkit. Over the years I’ve started many a cross-platform GUI project, from GTK+ (and its nice gtkmm C++ bindings) to fltk. Even disregarding the cross-platform capabilities, Qt is a great toolkit full stop.

I keep coming back to Qt due to a variety of reasons. The support community is large, with a decent ~48k questions on Stack Overflow. If you’re stuck, a quick Google usually reveals the problem. Frequently, the solution is a decade or so old, which is some testament to the stability of Qt’s API. Qt also provides really usable templated container classes like list, vector, map, etc., that are comparable to the those in the STL, but arguably less obtuse to use and with more readable documentation.

What disappoints me a little however is Qt Creator, the IDE component of Qt. It seems to be plagued by stability issues on my OSX Mavericks setup, and although it does provide FakeVim for Vim-like behaviour, this comes with an irritating lag on nearly every operation. Also, I much prefer Vim’s tab and buffer management, and overall customisability.

Qt Creator
Qt Creator – Nice, but it’s just not Macvim is it?

I decided to see if I could set up something nice in Macvim so that I wouldn’t have to rely on Qt Creator for everyday development. I say everyday development: whilst I don’t use Qt Designer (part of Creator; allows drag-and-drop UI design), I do occasionally use the debugger. Debugging in a GUI for me is just plainly easier than any other method.

In this post I’m going to show you how to set up Macvim for Qt 5.x development. By the end, you’ll have a great Macvim setup with autocomplete for all Qt classes. We’ll be using YouCompleteMe for the autocomplete functionality. Importantly, it interfaces with Clang to give us nice C++ support. If we point it at our Qt installation directory, then it’ll provide autocomplete for the whole of Qt’s namespaces too. Neat!

Let’s get started.

Step 1: Install Pathogen

I installed YouCompleteMe using Pathogen, a simple Vim package manager. Install according to the instructions on their Github:

You’ll also need to add execute pathogen#infect() to your .vimrc. If you don’t have one, now is the time to create one in your home directory and insert this into it. Vim will read this every time it starts up for commands to execute in order to configure itself for your session.

Step 2: YouCompleteMe Grab/Build

Clone YouCompleteMe into your bundle directory:

YouCompleteMe now needs to be built from source and installed. Luckily, there’s a bash script included for that:

If you’re interested in other languages too, check out the flags on YouCompleteMe‘s Github. It even does JavaScript. Quite how it manages that is quite the mystery.

Step 3: Install and Configure NerdTree

NERDTree is a great file browser for Vim. Essentially it’ll replace the left-hand project navigator that Qt Creator gives you. It’s also really useful for most other types of project.

Screen Shot 2016-04-05 at 19.21.00

Install it with Pathogen:

I tend to bind it to \e to toggle a file browser whenever I need it. Add the following to your .vimrc for that:

Also, you don’t really want NERDTree to be showing you object files and Qt’s generated moc files as you won’t be editing them. To hide them, add the following:

If you’re doing a lot of Python these days, put .pyc$ in there too. It’s also a good idea to have these in your .gitignore:

Step 3: Configure your Qt Project

Right! We have some useful tools installed now. You should have Macvim able to restart and read in your .vimrc without errors. Now it’s time to configure your project.

For C/C++ projects, YouCompleteMe (through Macvim) looks for a Python script called .ycm_extra_conf.py in your project directory in order to set itself up for that project. We’re going to be creating our own configuration for Qt projects.

A good default .ycm_extra_conf.py can be found at Valloric’s Github. Put it into your Qt project’s directory (where the .pro file is, etc), and we can make some additions. (Note: if you’d rather just download my configuration, you can grab it here).

At the top of the script, below the imports, define your Qt installation directory:

In the case of Qt 5.x, everything you need is in the clang_64 directory. Now we need to ensure that Clang gets the compilation and linking flags that it needs to analyse Qt’s sources.

Add the following include flags to the flags array:

This should cover most Qt projects, unless you’ll be using Qt’s OpenGL classes or need additional platform support. Take a look inside the include directory in a terminal for any additional headers that you might require.

Add the following linker flags to the flags array:

This will cover most bases for Qt development. If you want to use this for all Macvim/YouCompleteMe projects, you can move it to ~/.vim/ and insert let g:ycm_global_ycm_extra_conf = "~/.vim/.ycm_extra_conf.py" into your .vimrc. I personally would not recommend this however unless you’re sure you’re only going to use Macvim/YouCompleteMe for Qt.

Try it out and build!

The finished product!
The finished product!

You should have an autocompleting Macvim setup for Qt development. Use \e to toggle the file browser. To compile your project, open a terminal and just make it, since Qt has already provided a makefile.

YouCompleteMe will sometimes struggle to complete if there’s an error that stops Clang in its tracks. Execute :YcmDiags to see where the problem is if this occurs.

For more information on YouCompleteMe and how to use it, consult Valloric’s Github. For NERDTree, check out Scrooloose’s Github.

Leave a Reply