# How to get VTK9 + Qt6 working together?

**URL:** https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927
**Category:** Support
**Tags:** proposal
**Created:** [December 30, 2020, 2:27pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927 "2020-12-30T14:27:19Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![NameRakes](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/b4bc9f/32.png) [@NameRakes](https://discourse.vtk.org/u/NameRakes)
#### Post date: [December 30, 2020, 2:27pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/1 "2020-12-30T14:27:19Z")

</div>

I managed to get VTK9 to compile and work with Qt 5.15.2. But my goal is to use Qt6 which was released earlier this month. Key questions are:

1. Can I modify CMake variables (like Qt5\_DIR, etc.) and get it to work with Qt6? [Currently VTK-CMake complains about expecting Qt5 version].
2. Will QVTKOpenGLNativeWidget work with Qt6?

Thanks and Happy New Year!

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [December 30, 2020, 3:17pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/2 "2020-12-30T15:17:20Z")

</div>

CMake changes are needed to work with Qt6. I believe @sankhesh is investigating Qt6, but hasn’t gotten to VTK yet (he started with CMake itself). I do not know what kinds of changes will be necessary, but `QVTKOpenGLNativeWidget` is notoriously touchy to internal Qt changes (@mwestphal for that).

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [December 30, 2020, 3:44pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/3 "2020-12-30T15:44:55Z")

</div>

I have a WIP branch for vtk here: [https://gitlab.kitware.com/vtk/vtk/-/merge\_requests/7483](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/7483)

Please feel free to test it and report issues.

---

<div class="post-metadata">

### Author: ![NameRakes](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/b4bc9f/32.png) [@NameRakes](https://discourse.vtk.org/u/NameRakes)
#### Post date: [December 30, 2020, 6:30pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/4 "2020-12-30T18:30:07Z")

</div>

Thanks @sankhesh for the quick response. Will check it out.  
Sekar

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [December 30, 2020, 8:29pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/5 "2020-12-30T20:29:39Z")

</div>

> [@ben.boeckel](#):
>
> `QVTKOpenGLNativeWidget` is notoriously touchy to internal Qt changes (@mwestphal for that).

And @utkarshayachit as well.

---

<div class="post-metadata">

### Author: ![NameRakes](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/b4bc9f/32.png) [@NameRakes](https://discourse.vtk.org/u/NameRakes)
#### Post date: [December 31, 2020, 3:54am UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/6 "2020-12-31T03:54:37Z")

</div>

@sankhesh got it to _working (barely)_, locally of course, after these fixes in the CMake `Configure` phase (Mac OS 10.14.6 Mojave, Clang 10.0.1):

- Update `git tag -a 9.0.1 -m "my msg"` – could have been a bad local tag.
- I had to hand-update `Qt6XYZ_DIR` variables for say, 5 out of 7 or so (so I had to `Configure` five times just for these variables).
- Replace `Qt5` with `Qt6` in `GUISupport/QtSql/CMakeLists.txt`.
- Couldn’t get rid of Qt5\_DIR errors while building `Testing` for the above, so disabled build of any VTK test.

That meant I was able to `Generate`. I had to do these fixes in the `make` phase:

- `QFontMetricsF::width()` has been obsoleted, and so replaced with its equivalent `horizontalAdvance()` method in `Rendering/Qt/vtkQtTreeRingMapper.cxx, .h` files.
- Likewise in `Views/Qt/vtkQtListView.h, .cpp, vtkQtTreeView.h, .cpp`, replace `class QRegExp` instance with that of `class QRegularExpression`, and `QSortFilterProxyModel::setFilterRegExp()` method with its `setFilterRegularExpression()`.
- We are almost there … `Views/Qt/CMakeLists.txt` had `Qtn:Widgets` instead of `Qtn::Widgets` in its module link command!

Voila! Finally, I was on my way.

So I was able to build VTK9 + Qt6, and run Draw Sphere, the Hello World of VTK, _but with a twist_. @ben.boeckel was right (not able to mention Mathieu or Utkar): `QVTKOpenGLNativeWidget` is not positioned properly in the Main Window (always at the left bottom as opposed to where I put it), and the sphere keeps on spinning (should spin only when you click and drag).

If it helps, I am happy to put my little Qt+Vtk project on GitHub and send you the link. Thank you all for responding in the middle of holidays. Happy New Year!!

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [December 31, 2020, 3:26pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/7 "2020-12-31T15:26:16Z")

</div>

@NameRakes All those changes exist on my branch that I pointed you to. Use the checkout instructions on the MR to get them. Go to the MR at [https://gitlab.kitware.com/vtk/vtk/-/merge\_requests/7483](https://gitlab.kitware.com/vtk/vtk/-/merge_requests/7483) and click the **Check out branch** button to see the instructions.

---

<div class="post-metadata">

### Author: ![NameRakes](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/b4bc9f/32.png) [@NameRakes](https://discourse.vtk.org/u/NameRakes)
#### Post date: [December 31, 2020, 5:27pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/8 "2020-12-31T17:27:12Z")

</div>

Hi @sankhesh, thanks for the response. Yes, I followed the instructions (again today). The output after performing the final step in the MR checkout instructions:

```
 Merge made by the 'recursive' strategy.
 CMake/vtk-config.cmake.in | 5 +++++
 Documentation/release/dev/qt6-support.md | 6 ++++++
 GUISupport/Qt/CMakeLists.txt | 29 ++++++++++++++++++++++++-----
 GUISupport/Qt/QFilterTreeProxyModel.cxx | 4 ++++
 GUISupport/Qt/QVTKInteractor.cxx | 5 +++++
 GUISupport/Qt/QVTKInteractorAdapter.cxx | 6 +++---
 GUISupport/Qt/QVTKOpenGLNativeWidget.cxx | 1 -
 GUISupport/Qt/QVTKOpenGLStereoWidget.cxx | 1 -
 GUISupport/Qt/QVTKOpenGLWindow.cxx | 1 -
 GUISupport/Qt/QVTKRenderWindowAdapter.cxx | 3 +--
 GUISupport/Qt/Testing/Cxx/CMakeLists.txt | 4 ++--
 GUISupport/Qt/vtkQWidgetTexture.cxx | 8 ++++----
 GUISupport/Qt/vtkQtDebugLeaksView.cxx | 8 ++++++++
 Rendering/Qt/CMakeLists.txt | 6 +++---
 Views/Qt/CMakeLists.txt | 6 +++---
 15 files changed, 68 insertions(+), 25 deletions(-)

```

This is exactly the number of changes on the MR page. With this, I had to choose 4 Qt6 directories (`Widgets, WidgetsTools, CoreTools, GuiTools`) in the CMake `Configure` phase, and then got this error:

```
CMake Error at CMake/vtkModule.cmake:4228 (message):
  Could not find the Qt5 external dependency.
Call Stack (most recent call first):
  GUISupport/QtSQL/CMakeLists.txt:11 (vtk_module_find_package)

```

You can see the hard-coded `Qt5::` in the above line, and a single `:` in another file:

```
(base) % grep -n Qt5 GUISupport/QtSQL/CMakeLists.txt 
12: PACKAGE Qt5
15: VERSION_VAR Qt5_VERSION)
16:vtk_module_link(VTK::GUISupportQtSQL PUBLIC Qt5::Sql Qt5::Widgets)

(base) % grep -n ":Widgets" Views/Qt/CMakeLists.txt 
21:vtk_module_link(VTK::ViewsQt PRIVATE Qt${VTK_QT_MAJOR_VERSION}:Widgets)
                                                                ^^^

```

_Full disclaimer: today I haven’t tried to fix these errors_, but I expect the result to be the same as yesterday’s exercise (did it twice!). For example, now I grepped for _some_ (not all) of the code lines that didn’t compile yesterday:

```
(base) % grep -n "fontMetric.width(" Rendering/Qt/vtkQtTreeRingLabelMapper.cxx
366: if (sdimDC[0] < fontMetric.width(minString))
                                     ^^^
374: if (sdimDC[1] < fontMetric.width(minString))
411: delta_x = -(fontMetric.width(testString)) / 2.;
415: delta_x = -fontMetric.width(testString);

(base) % grep -n QRegExp Views/Qt/vtkQtListView.cxx
163:void vtkQtListView::SetFilterRegExp(const QRegExp& pattern)
                                              ^^^

```

Today, the downloaded MR `git tag` looks correct – as I admitted, I had somehow messed up the tag yesterday. I believe all the _other_ necessary changes (_over and above the 15 on the MR page_) are outlined in my previous response, at least on Mac OS.

If I build it again today, I will post back. Thanks again!

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [December 31, 2020, 6:21pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/9 "2020-12-31T18:21:50Z")

</div>

@NameRakes Thanks for testing.

Yes, it is still a work-in-progress (WIP). I haven’t yet added all the changes for `RenderingQt`, `QtSQL` and `ViewsQt` modules. However, `GUISupportQt` and by extension, QVTKOpenGLNativeWidget should be working fine. Were you able to test that? I’d love to see your results. You’d have to disable the other modules for testing, though.

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [December 31, 2020, 6:23pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/10 "2020-12-31T18:23:51Z")

</div>

One more thing - You shouldn’t have to keep merging the branch into master for testing, unless you want to. Just a checkout should do -

```auto
git fetch "git@gitlab.kitware.com:sankhesh/vtk.git" "qt6-support"
git checkout -b "sankhesh/vtk-qt6-support" FETCH_HEAD

```

---

<div class="post-metadata">

### Author: ![NameRakes](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/b4bc9f/32.png) [@NameRakes](https://discourse.vtk.org/u/NameRakes)
#### Post date: [December 31, 2020, 8:24pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/11 "2020-12-31T20:24:28Z")

</div>

@sankhesh, yes, initially I stopped with `git fetch` and `git checkout` as you point out - but when I saw the errors, I wasn’t sure, so I merged. Yes, I do realize that it is a WIP branch, and appreciate you responding in the middle of the holiday season! 👍. Also, I am glad to be able to test the WIP branch.

Here is my little test program:

> **[sekargovindarajan/vtkQt6](https://github.com/sekargovindarajan/vtkQt6)**
>
> Test the combination of Qt6 and Vtk9. Contribute to sekargovindarajan/vtkQt6 development by creating an account on GitHub.

It is QAD program (Quick and Dirty) - the data source, actor etc. don’t have state - I just put them all into one SLOT function. Let me know if my connectivity part needs any fixes.

---

<div class="post-metadata">

### Author: ![StephPierret](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@StephPierret](https://discourse.vtk.org/u/StephPierret)
#### Post date: [February 14, 2021, 11:53am UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/12 "2021-02-14T11:53:20Z")

</div>

@NameRakes, I see that you get VTK9 and Qt5.15.2 working. Would it be possible for you to share some code? It would be great. I’m trying on my side but could not make it so far.

---

<div class="post-metadata">

### Author: ![sankhesh](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sankhesh/32/73_2.png) [@sankhesh](https://discourse.vtk.org/u/sankhesh)
#### Post date: [February 15, 2021, 1:24pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/13 "2021-02-15T13:24:05Z")

</div>

The Qt6 support branch has been merged into master. VTK supports both Qt5 and Qt6. You can choose the version of Qt to use via the variable `VTK_QT_VERSION` during the CMake configure step.

---

<div class="post-metadata">

### Author: ![StephPierret](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@StephPierret](https://discourse.vtk.org/u/StephPierret)
#### Post date: [February 15, 2021, 4:34pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/14 "2021-02-15T16:34:34Z")

</div>

Thanks for your answer. Today I was able to get it working with vtk 8.2 and Qt 5.15. I was able to display a renderer in a QML. However, it still does not work with vtk9. So something must be changed to work with vtk9 but I do not know what. Would you have a small piece of code that is able to do that: vtk9, Qt5.15, a renderer into QML?

---

<div class="post-metadata">

### Author: ![NameRakes](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/n/b4bc9f/32.png) [@NameRakes](https://discourse.vtk.org/u/NameRakes)
#### Post date: [February 15, 2021, 5:53pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/15 "2021-02-15T17:53:43Z")

</div>

@StephPierret See my GitHub link above your message in this thread. Take the GitHub test, replace Qt6 with Q5.15.2, and also change Qt6 references to Qt5 in CMake files. That’s it.

---

<div class="post-metadata">

### Author: ![StephPierret](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/s/d78d45/32.png) [@StephPierret](https://discourse.vtk.org/u/StephPierret)
#### Post date: [February 15, 2021, 7:08pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/16 "2021-02-15T19:08:47Z")

</div>

It works. Many thanks for your help.

---

<div class="post-metadata">

### Author: ![codeling](https://discourse.vtk.org/user_avatar/discourse.vtk.org/codeling/32/3792_2.png) [@codeling](https://discourse.vtk.org/u/codeling)
#### Post date: [July 2, 2021, 10:36am UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/17 "2021-07-02T10:36:50Z")

</div>

While Qt 6 support works well with VTK master (THANKS!), it does not seem to be included in the 9.0.2 release.

Does anyone know, is it planned only for 10.x, or maybe also for 9.0.3?

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)
#### Post date: [July 2, 2021, 10:40am UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/18 "2021-07-02T10:40:58Z")

</div>

It should be available in VTK 9.1

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.vtk.org/u/ben.boeckel)
#### Post date: [July 2, 2021, 12:04pm UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/19 "2021-07-02T12:04:54Z")

</div>

Qt 6 support was too large to backport, sorry.

---

<div class="post-metadata">

### Author: ![dong-shufeng](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dong-shufeng/32/6004_2.png) [@dong-shufeng](https://discourse.vtk.org/u/dong-shufeng)
#### Post date: [November 9, 2022, 9:01am UTC](https://discourse.vtk.org/t/how-to-get-vtk9-qt6-working-together/4927/20 "2022-11-09T09:01:50Z")

</div>

Thanks, this example helped me a lot
