Krita().instance().activeWindow() is NoneType in __init__ of a Docker constructur

Here the code. Is there a special time/place where I can get the active window from the docker?

from krita import Krita, DockWidget

class MiniDocker(DockWidget):
    name = 'YMiniDocker'

    def __init__(self):
        super().__init__()
        self.setWindowTitle("My Docker")

        app = Krita.instance()
        print(type(app), app)
        win = app.activeWindow()
        print(type(win), win)
        doc = app.activeDocument()
        print(type(doc), doc)

    def canvasChanged(self, canvas):
        pass

The output

<class 'PyKrita.krita.Krita'> <PyKrita.krita.Krita object at 0x0000000014A3F040>
<class 'NoneType'> None
<class 'NoneType'> None

Thanks in advance :slight_smile:

Hi

You can user notifier signal I think:
https://api.kde.org/appscomplete-api/krita-apidocs/libs/libkis/html/classNotifier.html#a72ce2e22de9cfd3e327f9604a7cc9301

Something like this (not tested):

from krita import Krita, DockWidget

class MiniDocker(DockWidget):
    name = 'YMiniDocker'

    def __init__(self):
        super().__init__()
        self.setWindowTitle("My Docker")

        self.app = Krita.instance()
        print(type(self.app), self.app)

        self.app.instance().notifier().windowCreated.connect(self.testing)
         
    def testing(self):
        win = self.app.activeWindow()
        print(type(win), win)
     
    def canvasChanged(self, canvas):
        pass

But be aware that with Krita 4.x the signal is bugged; it work properly only with Krita 5.x

Grum999

1 Like

Got it! Thank you so much! :slight_smile:

Don’t you first have to turn the notifier on?

notify = Krita.instance().notifier()
notify.setActive(True)
notify.windowCreated.connect(self.windowCreatedSetup)

That’s an interesting remark

I think that in theory, you’re right.
But it also seems that Notifier is active by default…

Looking the source code, value is False by default (line 16), but it seems there’s no call to blockSignals
(as in line 53) and then it might be active

Not at home and can’t really test that from smartphone, but anyway it could be a good thing to activate it in all case :slight_smile:

Grum999

Hi

Did some test like:

    def setup(self):
        def windowCreated():
            print('windowCreated!')

        print("Notifier active: ", Krita.instance().notifier().active())
        Krita.instance().notifier().windowCreated.connect(windowCreated)

And result in terminal is:

Notifier active: False
windowCreated!

So, by default it should not be active, but in reality it’s active :slight_smile:
This result match with what I saw in source code.

By forcing a call to setActive(False) in setup()method, we can see that signal is not emitted anymore at startup.

I’ve created a bug for it 443248 – Notifier class not properly initialized

Grum999

1 Like

It seems has already been patched to be True by default.

Oh yes, I didn’t saw it :slight_smile:

Grum999

I think I had code reliant on none somewhere. I need to change.

Hi

The fix bug implies now initialization force notifier to be properly active by default.

Grum999