#gtk #gjs
#gtk #gjs
Вопрос:
Я начинаю с пустой таблицы (liststore с одним столбцом)
Я хочу, чтобы пользователи могли импортировать CSV-файл и отображать его содержимое. Импорт файла работает, и данные CSV действительно отображаются, но исходный столбец (с названием «Нет данных») остается. Как мне избавиться от этого?
Я попытался удалить элемент tree view и даже контейнеры (но когда я это делаю, я не могу заставить их отображаться снова…
Я видел в документах gtk, что замена TreeView.set_model(ListStore) должна полностью заменить существующую модель и столбцы, но, похоже, этого не происходит…
что я сейчас делаю, так это:
this._listStore = new Gtk.ListStore();
let coltypes = [GObject.TYPE_STRING];
this._listStore.set_column_types(coltypes);
// Create the treeview
this._treeView = new Gtk.TreeView({
expand: true
});
this._treeView.set_model(this._listStore);
// Create a cell renderer for when bold text is needed
let bold = new Gtk.CellRendererText({
weight: Pango.Weight.BOLD
});
// Create a cell renderer for normal text
let normal = new Gtk.CellRendererText();
// Create the columns for the address book
let defCol = new Gtk.TreeViewColumn({
title: "No Data"
});
// Pack the cell renderers into the columns
defCol.pack_start(bold, true);
// Set each column to pull text from the TreeView's model
defCol.add_attribute(bold, "text", 0);
// Insert the columns into the treeview
this._treeView.insert_column(defCol, 0);
Затем, когда файл csv загружен, я пытаюсь обновить таблицу чем-то вроде этого:
this._listStore = new Gtk.ListStore();
// this._treeView.add(this._listStore);
let coltypes = [];
this.data.headers.forEach((h) => {
coltypes.push(GObject.TYPE_STRING);
});
print(coltypes);
this._listStore.set_column_types(coltypes);
// Replace the treeview
this._treeView.set_model(this._listStore);
/*
this._treeView = new Gtk.TreeView ({
expand: true,
model: this._listStore });
*/
// Create cell renderers
let normal = new Gtk.CellRendererText();
let bold = new Gtk.CellRendererText({
weight: Pango.Weight.BOLD
});
// Create the columns for the address book
for (k = 0; k < this.data.headers.length; k ) {
print('***key is : ' k ', val is : ' this.data.headers[k] ' of type : ' typeof(this.data.headers[k]));
// let col=k;
this[`col_${k}`] = new Gtk.TreeViewColumn({
title: this.data.headers[k]
});
this[`col_${k}`].pack_start(normal, true);
if (k == 0) {
this[`col_${k}`].add_attribute(normal, "text", k);
} else {
this[`col_${k}`].add_attribute(normal, "text", k);
}
try {
this._treeView.insert_column(this[`col_${k}`], k);
} catch (err) {
print(err);
}
}
// Put the data in the table
let i;
for (i = 0; i < this.data.csva.length; i ) {
let row = this.data.csva[i];
print('trying to push : ' row[0].toString());
print('... the data is of type : ' typeof(row[1]));
let iter = this._listStore.append();
// this._listStore.set (iter, [0, 1, 2],
// [contact[0].toString(), contact[1].toString(), contact[2].toString()]);
this._listStore.set(iter, Object.keys(this.data.headers), row);
}
Почему этот начальный столбец все еще там? как мне избавиться от этого?
Заранее спасибо за любую помощь или указания.
Ответ №1:
Что вам нужно, так это Gtk.TreeView.remove_column(). В Python это было бы:
for column in this._treeView.get_columns():
this._treeView.remove_column(column)
Вы, конечно, будете делать это каждый раз, когда вам нужно удалить все столбцы в treeview перед добавлением столбцов из CSV.