Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

treeview - Why I don't get any drag and drop signals in a Gtk.TreeeView? (python + glade)

I have a TreeView whose rows are reorderable. When I drag and drop a row, it moves appropiately but I don't get any signal related to the drag and drop proccess.

The UI is built in glade and I connected all the drag and drop signals to functions in my code which print the name of the signal, but it doesn't seem to work.

I think the signals should be fired by the CellRenderers of the TreeView instead of the tree itself, but those signals are not available for the CellRenderers in glade.

I think there is something I am not underestanding at all.

Here is my code:

main.glade:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkTreeStore" id="my_treestore">
    <columns>
      <!-- column-name name -->
      <column type="gchararray"/>
    </columns>
  </object>
  <object class="GtkWindow" id="main_window">
    <property name="can_focus">False</property>
    <property name="default_width">640</property>
    <property name="default_height">480</property>
    <signal name="destroy" handler="window_destroy" swapped="no"/>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkScrolledWindow">
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="shadow_type">in</property>
            <child>
              <object class="GtkTreeView" id="my_treeview">
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="model">my_treestore</property>
                <property name="headers_visible">False</property>
                <property name="reorderable">True</property>
                <property name="enable_search">False</property>
                <property name="enable_tree_lines">True</property>
                <child internal-child="selection">
                  <object class="GtkTreeSelection"/>
                </child>
                <child>
                  <object class="GtkTreeViewColumn" id="name_column">
                    <property name="title" translatable="yes">Name</property>
                    <child>
                      <object class="GtkCellRendererText" id="name_cell_renderer"/>
                      <attributes>
                        <attribute name="text">0</attribute>
                      </attributes>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

main.py:

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk



class Main:

    
    def __init__(self):

        builder = Gtk.Builder()
        builder.add_from_file("main.glade")
        builder.connect_signals(self)

        window = builder.get_object("main_window")        
        window.show_all()

        self.builder = builder
        
        self.fill_tree(None,0, 0)
        
    
    def window_destroy(self, button):

        Gtk.main_quit()
        
    
    def fill_tree(self, iter, parent_id, level): # Recursively fill the treeview model with some data
    
        model = self.builder.get_object('my_treestore')
              
        for x in range(3):
            id = 3*parent_id+x+1
            row = ('Node {}'.format(id),)
            new_iter = model.append(iter, row)
            if level < 3:
                self.fill_tree(new_iter, id, level+1)


    def drag_begin_cb(self, widget, context, user_data):
    
        print('drag data begin')
        
        
    def drag_data_delete_cb(self, widget, context, user_data):
    
        print('drag data delete')
        
        
    def drag_data_get_cb(self, widget, context, selection_data, info, time, user_data):  
    
        print('drag data get')
        
    
    def drag_data_received_cb(self, widget, context, x, y, selection_data, info, time, user_data): 
    
        print('drag data received')
        
    def drag_drop_cb(self, widget, context, x, y, time, user_data):      
        
        print('drag drop')
        
        
    def drag_end_cb(self, widget, context, user_data):
    
        print('drag end')
        
        
    def drag_failed_cb(self, widget, context, result, user_data):
    
        print('drag failed')
        
        
    def drag_leave_cb(self, widget, context, time, user_data):
    
        print('drag leave')
        
        
    def drag_motin_cb(self, widget, context, x, y, time, user_data):
    
        print('drag motion')
        
        
 
def main():

    main = Main()
    Gtk.main()


if __name__ == '__main__':
    main()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I suggest you look at the signals of the Treeview, and also the TreeStore, maybe the signal row-changed?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...