# How to inherit from a vtk class and a QObject

**URL:** https://discourse.vtk.org/t/how-to-inherit-from-a-vtk-class-and-a-qobject/12973
**Category:** Support
**Created:** [December 27, 2023, 9:01am UTC](https://discourse.vtk.org/t/how-to-inherit-from-a-vtk-class-and-a-qobject/12973 "2023-12-27T09:01:38Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![baker-Xie](https://discourse.vtk.org/user_avatar/discourse.vtk.org/baker-xie/32/8169_2.png) [@baker-Xie](https://discourse.vtk.org/u/baker-Xie)
#### Post date: [December 27, 2023, 9:01am UTC](https://discourse.vtk.org/t/how-to-inherit-from-a-vtk-class-and-a-qobject/12973/1 "2023-12-27T09:01:38Z")

</div>

For example, the following code doesn’t work. (cannot compile)

```auto
class CustomMouseInteractorStyle : public vtkInteractorStyleTrackballCamera, QObject
{
Q_Object
public:
    static CustomMouseInteractorStyle* New();
vtkTypeMacro(CustomMouseInteractorStyle, vtkInteractorStyleTrackballCamera);

    virtual void OnKeyPress() override
    {

    }
};

vtkStandardNewMacro(CustomMouseInteractorStyle);

```

---

<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 27, 2023, 12:54pm UTC](https://discourse.vtk.org/t/how-to-inherit-from-a-vtk-class-and-a-qobject/12973/2 "2023-12-27T12:54:27Z")

</div>

[`QOb]ect` must be first](https://doc.qt.io/qt-5/moc.html#multiple-inheritance-requires-qobject-to-be-first), so that limits Qt’s position here. VTK also likes to be first, so that’s a bit of a conflict. It appears that [VTK’s approach](https://gitlab.kitware.com/vtk/vtk/-/tree/master/GUISupport/Qt) is to have Qt-derived classes and `vtkObject`-derived classes and use them from each other. [ParaView seems to do this too](https://gitlab.kitware.com/paraview/paraview/-/tree/master/Qt/Core).

---

<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 27, 2023, 12:56pm UTC](https://discourse.vtk.org/t/how-to-inherit-from-a-vtk-class-and-a-qobject/12973/3 "2023-12-27T12:56:01Z")

</div>

Cc: @sankhesh

---

<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 27, 2023, 1:56pm UTC](https://discourse.vtk.org/t/how-to-inherit-from-a-vtk-class-and-a-qobject/12973/4 "2023-12-27T13:56:25Z")

</div>

Generally speaking, the approach that most applications use, is to have QObject classes separate from vtkObject classes and have the QObject classes take ownership of vtk classes because the Qt provides the GUI application layer.

---

<div class="post-metadata">

### Author: ![Paulo\_Carvalho](https://discourse.vtk.org/user_avatar/discourse.vtk.org/paulo_carvalho/32/370_2.png) [@Paulo\_Carvalho](https://discourse.vtk.org/u/Paulo_Carvalho)
#### Post date: [January 7, 2024, 10:43pm UTC](https://discourse.vtk.org/t/how-to-inherit-from-a-vtk-class-and-a-qobject/12973/5 "2024-01-07T22:43:03Z")

</div>

Hello,

Why do you need it to be a `QObject`? Do you need Qt’s signals/slots, Qt’s RTTI, Qt’s dynamic property system and/or use Qt’s garbage collection in your class?

take care,

PC

---

<div class="post-metadata">

### Author: ![baker-Xie](https://discourse.vtk.org/user_avatar/discourse.vtk.org/baker-xie/32/8169_2.png) [@baker-Xie](https://discourse.vtk.org/u/baker-Xie)
#### Post date: [January 8, 2024, 12:58am UTC](https://discourse.vtk.org/t/how-to-inherit-from-a-vtk-class-and-a-qobject/12973/6 "2024-01-08T00:58:37Z")

</div>

Yes, I need to use QT’s signal and slot. I have solved this problem myself, thanks

---

<div class="post-metadata">

### Author: ![geoffw](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/g/898d66/32.png) [@geoffw](https://discourse.vtk.org/u/geoffw)
#### Post date: [January 9, 2024, 5:02am UTC](https://discourse.vtk.org/t/how-to-inherit-from-a-vtk-class-and-a-qobject/12973/7 "2024-01-09T05:02:35Z")

</div>

You don’t need to have a QObject to use slot/signal. We had a similar problem and we handled it by just subclassing from vtk, then explicitly handing the connect/disconnect ourselves and keeping track of the connection ourselves, for example:

```
_timerConn = QObject::connect(&_kbChecker, &QTimer::timeout, [this]() { // do something }});

```

When you are done with the connection, or on destruction, you just disconnect using the return value from connect.
