Show/hide a Browser column programmatically

My addon adds new columns into the Browser.
I want to show the new columns after addon installation (then user can hide them if not needed).
What’s the proper way to show and hide a column from addon code?


The only working approach I found is

  • Show column: browser.form.tableView.showColumn("Due")
  • Hide column: browser.form.tableView.hideColumn("Due")

But it uses QTableView functions directly without updating Anki models.


Partially working approach is using Collection#set_browser_note_columns() and Collection#set_browser_card_columns(). Then I need to refresh Browser table like in Table#_on_column_toggled(), which involves using many inner functions.

1 Like

Found a better working approach, but it uses private property Table#_model:

from anki.collection import Collection
from aqt.browser import Browser, Table
from aqt.utils import show_info

__due_column: str = "cardDue"


def __on_show_due_column(browser: Browser):
    col: Collection = browser.col
    table: Table = browser.table
    if table.is_notes_mode():
        if __due_column not in col.load_browser_note_columns():
            table._model.toggle_column(__due_column)
    else:
        if __due_column not in col.load_browser_card_columns():
            table._model.toggle_column(__due_column)


def __on_hide_due_column(browser: Browser):
    col: Collection = browser.col
    table: Table = browser.table
    if table.is_notes_mode():
        if __due_column in col.load_browser_note_columns():
            table._model.toggle_column(__due_column)
    else:
        if __due_column in col.load_browser_card_columns():
            table._model.toggle_column(__due_column)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.