EDIT: 2009/12/04 - Fixed formatting, added a source code example
Hi All,
I have a TreeView with a single column that holds a CellRendererText component set to editable.
What I would like to do is, validate the users input into the text box, if it does not match some criteria, I would like to refocus the input on that cell so that the user cannot leave until it is correct.
As far as I can tell, this is pretty simple. I have a function "on_edited" that I connect to the text renderer "edited" event. If the users input does not match my criteria I reset the cursor and set the cell to editing using set_cursor(path,col,True). Eg:
(pseudo-code)
Code:
on_edited(cell,path,new_text)
{
if (new_text != "42")
{
col = treeView1.get_column(0);
treeView1.set_cursor(path,col,True);
return;
}
print "Success!"
}
However, when I do this, if the text is not "42" II get an internal GTK failure:
GtkWarning: _gtk_tree_view_column_start_editing: assertion 'tree_column->editable_widget == NULL' failedWhat am I doing wrong here? This seems like a perfectly logical control flow?
Thanks
Matt
Source code:
Glade file:
Code:
<?xml version="1.0"?>
<glade-interface>
<!-- interface-requires gtk+ 2.16 -->
<!-- interface-naming-policy project-wide -->
<widget class="GtkWindow" id="main">
<property name="visible">True</property>
<property name="title" translatable="yes">Main</property>
<property name="window_position">center</property>
<child>
<widget class="GtkTreeView" id="treeview1">
<property name="width_request">200</property>
<property name="height_request">300</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="search_column">0</property>
<property name="level_indentation">5</property>
</widget>
</child>
</widget>
</glade-interface>
Python source:
Code:
#!/usr/bin/env python
import gtk
import gtk.glade
import gobject
class MainWindow:
def __init__(self):
self.gladefile = "tree_test.glade"
self.wTree = gtk.glade.XML(self.gladefile)
self.window = self.wTree.get_widget("main")
self.window.connect("destroy",gtk.main_quit)
#Set up the tree view
self.treeView1 = self.wTree.get_widget("treeview1")
self.store = gtk.ListStore(gobject.TYPE_STRING)
textCell = gtk.CellRendererText()
textCell.set_property("editable",True)
self.treeView1.set_model(self.store)
self.treeView1.append_column(gtk.TreeViewColumn("Text",textCell,text=0))
textCell.connect("edited", self.on_edit)
#Add a line to the tree view
iter = self.store.append(["line1"])
path = self.store.get_path(iter)
#Select the line
self.treeView1.set_cursor(path)
def on_edit(self,cell,path,new_text):
if(new_text != "42"):
col = self.treeView1.get_column(0)
self.treeView1.set_cursor(path,col,True)
return
self.store.clear()
self.store.append(["OK"])
try:
main = MainWindow()
gtk.main()
except:
pass