"Allocating size to..." GTK Warning when using Gtk.TreeView inside Gtk.ScrolledWindow
Asked Answered
M

1

8

I'm receiving the following warnings in my GTK 3 application:

Gtk-WARNING **: Allocating size to __main__+MCVEWindow 0000000004e93b30 without calling gtk_widget_get_preferred_width/height(). How does the code know the size to allocate?

The warnings occurs when Gtk.ScrolledWindow containing Gtk.TreeView is attached to the grid, the grid itself is attached to the gtk.ApplicationWindow and there are enough elements for the scrollbar to actually appear. If there aren't enough elements to make it scrollable, the warning doesn't appear.

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk as gtk


class MCVEWindow(gtk.ApplicationWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self._tree_view = gtk.TreeView()
        self._tree_view.set_hexpand(True)
        self._tree_view.set_vexpand(True)

        self.populate_tree_view()  # populate tree view with fake items

        window_column = gtk.TreeViewColumn(
            "Window", gtk.CellRendererText(),
            text=0
        )
        window_column.set_resizable(True)
        handle_column = gtk.TreeViewColumn(
            "Handle", gtk.CellRendererText(),
            text=1
        )
        class_column = gtk.TreeViewColumn(
            "Class name", gtk.CellRendererText(),
            text=2
        )
        self._tree_view.append_column(window_column)
        self._tree_view.append_column(handle_column)
        self._tree_view.append_column(class_column)

        scrolled_tree_view = gtk.ScrolledWindow()
        scrolled_tree_view.add(self._tree_view)

        toolbar = gtk.Toolbar()

        expand_tree_view_button = gtk.ToolButton(icon_name="list-add")
        expand_tree_view_button.connect(
            "clicked",
            lambda e: self._tree_view.expand_all()
        )
        collapse_tree_view_button = gtk.ToolButton(icon_name="list-remove")
        collapse_tree_view_button.connect(
            "clicked",
            lambda e: self._tree_view.collapse_all()
        )
        toolbar.insert(expand_tree_view_button, -1)
        toolbar.insert(collapse_tree_view_button, -1)

        status_bar = gtk.Statusbar()
        status_bar.push(
            status_bar.get_context_id("Status message"),
            "A status message."
        )

        self._master_grid = gtk.Grid()
        self._master_grid.attach(toolbar, 0, 0, 1, 1)
        self._master_grid.attach(scrolled_tree_view, 0, 1, 1, 1)
        self._master_grid.attach(status_bar, 0, 2, 1, 1)
        self.add(self._master_grid)

        self.connect("delete-event", gtk.main_quit)

        self.show_all()

    def populate_tree_view(self):
        tree_store = gtk.TreeStore(str, str, str)
        # Warnings don't occur when there are less than 100 "root" items
        for i in range(100):
            item1 = tree_store.append(
                None,
                ["Window " + str(i + 1), "12345678", "ClassName"]
            )
            for j in range(3):
                item2 = tree_store.append(
                    item1,
                    ["Window " + str(i + 1) + str(i + 2),
                     "12345678",
                     "ClassName"]
                )
                for k in range(5):
                    tree_store.append(
                        item2,
                        ["Window " + str(i + 1) + str(j + 1) + str(k + 1),
                         "12345678",
                         "ClassName"]
                    )

        self._tree_view.set_model(tree_store)


class MCVEApp(gtk.Application):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    def do_activate(self):
        MCVEWindow()
        gtk.main()


if __name__ == "__main__":
    MCVEApp().run()

You should be able to copy, paste and run this code if you have environment set up.

The warnings don't follow any specific pattern, sometimes there is one warning, sometimes two or more. The warrnings also pop up whenever I expand all tree items.

GTK version is 3.22.18

What could cause these warnings?

Martelli answered 16/9, 2017 at 11:36 Comment(6)
Do you have a MCVE?Hsinking
Yes, I edited the question. You should be able to run it if you have the environment set up.Martelli
Sorry, I can't create that error using Gtk 3.18. Are they affecting the usability of your app?Hsinking
They don't affect the usability, but they're spamming the terminal. My suspicion based on googling is that it's a GTK bug, and since you can't reproduce it in earlier version I may be right although I can't tell for sure. Thank you very much for trying, I hope someone wants to take a shot at this too.Martelli
I translated your program into C language and the warning is present. gist.github.com/mywtfmp3/bf17df5a3d13c889acd673740c9c2898. I am running Gtk 3.22.21.Hoeg
Seems like it's a problem with GTK(and not PyGObject) and the way it handles layout/reszing. I'll probably ask on GTK mailing list and update this question. Thank you for the effort.Martelli
M
1

I received the answer on the GTK App dev mailing list which lead me to the solution:

Attaching TreeView to GTK Grid which is then added to the ScrolledWindow solved the issue for me.

Instead of this

scrolled_tree_view = gtk.ScrolledWindow()
scrolled_tree_view.add(self._tree_view)

you need to do the following

scrolled_tree_view = gtk.ScrolledWindow()
grid = gtk.Grid()
grid.attach(self._tree_view, 0, 0, 1, 1)
scrolled_tree_view.add(grid)

Unfortunately, this isn't documented anywhere.

Martelli answered 5/10, 2017 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.