QtAda - 将图像从一个表视图拖放到另一个表视图

时间:2014-07-23 11:07:12

标签: qt drag-and-drop qt4 tableview ada

我是编程的新手,刚开始使用Qtada 3.2,所以请放纵:D

我的目标是将图片(png)从一个表格视图模型拖到另一个。

虽然在下面的最小例子中拖动时已经显示了图像,但我认为重写鼠标按下和鼠标释放事件应该是解决方案吗? 还有什么我需要做的,比如创建掉落动作,哑剧数据或项目委托? 虽然我查看了QtAda教程,但我仍然不知道如何为我的表视图正确设置覆盖事件。 真的尝试了很多,但没有任何效果。现在我感到困惑和困惑。 如果有人能指出我正确的方向,或者甚至更好,向我展示一个有效的例子,那将是很好的!

奖金问题#1:是否有更好/更正确的方法将图片加载到商品模型中?

奖金问题#2:如何摆脱表中的行编辑?我只需要将图像显示在单元格中,而不是其他任何内容。

提前致谢!

迈克尔

我的例子:

main.adb

with Qt4.Core_Applications;
with Qt4.Objects;
with Qt4.Push_Buttons.Constructors;
with Qt4.Splitters.Constructors;
with Qt4.Splitters;
with Qt4.Strings;
with Qt_Ada.Application;
with Table_View_Big;
with Table_View_Small;

procedure Main is

begin

   Qt_Ada.Application.Initialize;

   declare

      Quit   : constant not null access Qt4.Push_Buttons.Q_Push_Button'Class
        := Qt4.Push_Buttons.Constructors.Create
          (Qt4.Strings.From_Utf_16 ("Quit"));

      Splitter : constant not null Qt4.Splitters.Q_Splitter_Access
        := Qt4.Splitters.Constructors.Create(The_Orientation => Qt4.Vertical);

      Big_Table : constant not null Table_View_Big.Big_Table_Access
        := Table_View_Big.Constructors.Create;

      Small_Table : constant not null Table_View_Small.Small_Table_Access
        := Table_View_Small.Constructors.Create;

   begin

      Splitter.Add_Widget(Big_Table);
      Splitter.Add_Widget(Small_Table);
      Splitter.Add_Widget(Quit);

      Qt4.Objects.Connect (Quit,
                           Qt4.Signal ("clicked()"),
                           Qt4.Core_Applications.Instance,
                           Qt4.Slot ("quit()"));
      Splitter.Set_Fixed_Size(640,480);

      Splitter.Show;
      Qt_Ada.Application.Execute;

   end;

end Main;

table_view_small.ads:

with Qt4.Table_Views.Constructors;
with Qt4.Table_Views.Directors;
with Qt4.Abstract_Item_Models;
--with Qt4.Mouse_Events;

package Table_View_Small is

   type Small_Table is limited new Qt4.Table_Views.Q_Table_View with private;

   type Small_Table_Access is access all Small_Table'Class;

   package Constructors is

      function Create return not null Small_Table_Access;

   end Constructors;

private

   type Small_Table is new Qt4.Table_Views.Directors.Q_Table_View_Director with record

      Small_Table_Item_Model    : Qt4.Abstract_Item_Models.Q_Abstract_Item_Model_Access;

   end record;

   --     overriding procedure Mouse_Press_Event
   --       (Self  : not null access Table_Small;
   --        Event : not null access Qt4.Mouse_Events.Q_Mouse_Event'Class);


   --     overriding procedure Mouse_Move_Event
   --       (Self  : not null access Table_Small;
   --        Event : not null access Qt4.Mouse_Events.Q_Mouse_Event'Class);


   --     overriding procedure Mouse_Release_Event
   --       (Self  : not null access Table_Small;
   --        Event : not null access Qt4.Mouse_Events.Q_Mouse_Event'Class);

end Table_View_Small;

table_view_small.adb

with Qt4.Icons;
with Qt4.Model_Indices;
with Qt4.Standard_Item_Models.Constructors;
with Qt4.Strings;
with Qt4.Variants;
with Table_View_Small.MOC;
pragma Warnings (Off, Table_View_Small.MOC);


package body Table_View_Small is

   use Qt4;

   package body Constructors is

      function Create return not null Small_Table_Access is

         Self   : constant Table_View_Small.Small_Table_Access := new Table_View_Small.Small_Table;

      begin

         Qt4.Table_Views.Directors.Constructors.Initialize (Self);

         declare

            Icon : Qt4.Icons.Q_Icon;

            Data_Role : Qt4.Item_Data_Role := qt4.Decoration_Role;

            Index : Qt4.Model_Indices.Q_Model_Index;

         begin

            Self.Small_Table_Item_Model := Qt4.Abstract_Item_Models.Q_Abstract_Item_Model_Access
              (Qt4.Standard_Item_Models.Constructors.Create (1, 6, Self));

            Icon := Qt4.Icons.Create(Qt4.Strings.From_Ucs_4("anypng.png"));

            Index := Self.Small_Table_Item_Model.Index(0,0);

            Qt4.Abstract_Item_Models.Set_Data(Self.Small_Table_Item_Model,index,icon.To_Q_Variant,Data_Role);

            Self.Set_Model(Self.Small_Table_Item_Model);


            -- Drag & Drop

            Self.Set_Accept_Drops(true);

            Self.Set_Drag_Enabled(true);

            Self.Set_Drop_Indicator_Shown(true);

            -- Self.Set_Drag_Drop_Mode(Qt4.Abstract_Item_Views.Internal_Move);

            return Self;

         end;

      end Create;

   end Constructors;

   --     overriding procedure Mouse_Press_Event
   --       (Self  : not null access Small_Table;
   --        Event : not null access Qt4.Mouse_Events.Q_Mouse_Event'Class)
   --     is
   --     null;
   --     end Mouse_Press_Event;

end Table_View_Small;

table_view_big.ads

with Qt4.Table_Views.Constructors;
with Qt4.Table_Views.Directors;
with Qt4.Abstract_Item_Models;

package Table_View_Big is

   type Big_Table is limited new Qt4.Table_Views.Q_Table_View with private;

   type Big_Table_Access is access all Big_Table'Class;

   package Constructors is

      function Create return not null Big_Table_Access;

   end Constructors;

private

   type Big_Table is new Qt4.Table_Views.Directors.Q_Table_View_Director with record

      Big_Table_Item_Model    : Qt4.Abstract_Item_Models.Q_Abstract_Item_Model_Access;

   end record;

   --     overriding procedure Mouse_Press_Event
   --       (Self  : not null access Big_Table;
   --        Event : not null access Qt4.Mouse_Events.Q_Mouse_Event'Class);
   --
   --     overriding procedure Mouse_Move_Event
   --       (Self  : not null access Big_Table;
   --        Event : not null access Qt4.Mouse_Events.Q_Mouse_Event'Class);

   --     overriding procedure Mouse_Release_Event
   --       (Self  : not null access Big_Table;
   --        Event : not null access Qt4.Mouse_Events.Q_Mouse_Event'Class);

end Table_View_Big;

table_view_big.adb

with Qt4.Abstract_Item_Views;
with Qt4.Sizes;
with Qt4.Standard_Item_Models.Constructors;
with Table_View_Big.MOC;
pragma Warnings (Off, Table_View_Big.MOC);


package body Table_View_Big is

   package body Constructors is

      function Create return not null Big_Table_Access is

         Self   : constant Table_View_Big.Big_Table_Access := new Table_View_Big.Big_Table;

      begin

         Qt4.Table_Views.Directors.Constructors.Initialize (Self);

         declare

         begin

            Self.Big_Table_Item_Model := Qt4.Abstract_Item_Models.Q_Abstract_Item_Model_Access
              (Qt4.Standard_Item_Models.Constructors.Create (25, 25, Self));

            Self.Set_Model(Self.Big_Table_Item_Model);

            Self.Set_Selection_Mode(Qt4.Abstract_Item_Views.No_Selection);


            -- Drag & Drop

            Self.Set_Accept_Drops(true);

            Self.Set_Drag_Enabled(false);

            Self.Set_Drop_Indicator_Shown(true);

            -- Self.Set_Drag_Drop_Mode (Qt4.Abstract_Item_Views.Drop_Only);

            return Self;

         end;

      end Create;

   end Constructors;

end Table_View_Big;

table_view_view_test_dd.gpr

with "qt_gui";

project Table_View_Test_DD is

   type Build_Modes is ("Application", "Metadata");
   Build_Mode : Build_Modes := external ("BUILD_MODE");

   for Source_Dirs use (".", ".amoc");

   case Build_Mode is
      when "Application" =>
         for Main use ("main.adb");
         for Object_Dir use ".objs";
         for Exec_Dir use ".";

      when "Metadata" =>
         for Languages use ("Amoc");
         for Object_Dir use ".amoc";
         for Source_Files use ("table_view_small.ads",
                               "table_view_big.ads");

   end case;

   package Compiler is
      for Default_Switches ("Ada") use ("-g", "-gnat05");
   end Compiler;

   package IDE is
      for QtAda_Amoc_Invocation_Switch use "-XBUILD_MODE=Metadata";
   end IDE;

   -- for Languages use ("Ada");

   package Naming is
      for Casing use "lowercase";
   end Naming;

end Table_View_Test_DD;

0 个答案:

没有答案