GJS/GTK为什么可以';是否替换gtk treeview模型(列)?

GJS/GTK为什么可以';是否替换gtk treeview模型(列)?,gtk,gjs,Gtk,Gjs,我从一个空表开始(liststore有一列) 我希望用户能够导入CSV文件并显示内容。 导入文件有效,CSV数据确实显示出来,但原始列(标题为“无数据”)保持不变。 我怎样才能摆脱它 我已经尝试删除树状视图元素,甚至容器(但当我这样做时,我无法让它们再次显示 我在gtk文档中看到,替换treeView.set_model(listStore)应该完全替换现有的模型和列,但它似乎没有 我现在做的是: this._listStore = new Gtk.ListStore(); let col

我从一个空表开始(liststore有一列)

我希望用户能够导入CSV文件并显示内容。 导入文件有效,CSV数据确实显示出来,但原始列(标题为“无数据”)保持不变。 我怎样才能摆脱它

我已经尝试删除树状视图元素,甚至容器(但当我这样做时,我无法让它们再次显示

我在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);

}
this._listStore=new Gtk.listStore();
//this.\u treeView.add(this.\u listStore);
设coltypes=[];
this.data.headers.forEach((h)=>{
coltypes.push(GObject.TYPE_字符串);
});
打印(coltypes);
此._listStore.set_column_类型(coltypes);
//更换树视图
this.\u treeView.set\u model(this.\u listStore);
/*
这个._treeView=新的Gtk.treeView({
是的,
型号:this.\u listStore});
*/
//创建单元渲染器
让normal=new Gtk.cellRenderText();
let bold=new Gtk.cellRenderText({
重量:Pango.weight.BOLD
});
//为通讯簿创建列
对于(k=0;k
为什么最初的专栏还在那里?我如何摆脱它

提前感谢您的帮助或指点。

您需要的是。在Python中,这将是:

for column in this._treeView.get_columns():
    this._treeView.remove_column(column)
当然,每次在从CSV添加列之前需要删除树视图中的所有列时,您都会这样做