Page 1 of 1

QScript - event information?

Posted: Thu Jul 19, 2018 3:56 pm
by wombats
In freak's example of the QScript command (viewtopic.php?p=522895#p522895), is it possible to access the event (QMouseEvent?) to get information about it, like the position of the mouse, etc.?

Specifically, I'd like to detect if the user clicks on the image of a TreeGadget.

Re: QScript - event information?

Posted: Thu Jul 19, 2018 7:36 pm
by ccode
Hello!

1 =TreeGadget
gadget(1).itemEntered(\"2\") ???

If you had access to "QTreeWidgetItem"?!

I am also interested in "Embedding Python in Qt Applications" next to QML, but I do not know yet how to use it with PureBasic.

Re: QScript - event information?

Posted: Fri Jul 20, 2018 1:23 am
by wombats
ccode wrote:Hello!

1 =TreeGadget
gadget(1).itemEntered(\"2\") ???

If you had access to "QTreeWidgetItem"?!
Yes, but I'm trying to detect if the user clicks on the item image. I want to change the image when it is clicked, but I don't want it to change if the user clicks on the text or not. On Windows and macOS I'm able to figure it out based on the mouse position, but I can't find a way to get that with Qt.

Re: QScript - event information?

Posted: Sun Jul 22, 2018 5:26 pm
by ccode
Yes, but I'm trying to detect if the user clicks on the item image. I want to change the image when it is clicked, but I don't want it to change if the user clicks on the text or not. On Windows and macOS I'm able to figure it out based on the mouse position, but I can't find a way to get that with Qt.
Do you mean something like that?

Code: Select all

For i = 0 To 3
  If CreateImage(i,32,32,24)
    If StartDrawing(ImageOutput(i))
      If i = 0 : Box(0,0,32,32,RGB(255,0,0))
      ElseIf i = 1 : Box(0,0,32,32,RGB(0,255,0))
      ElseIf i = 2 : Box(0,0,32,32,RGB(0,0,255))
      ElseIf i = 3 : Box(0,0,32,32,RGB(255,0,255))
      EndIf
      StopDrawing()
    EndIf
  EndIf
Next i

Structure MPos
  x.d
  y.d
EndStructure

Global TreeMouse.MPos

Global ItemState.b = #False

Runtime Procedure QtSignalHandler()
  If GetGadgetState(1) = 1 And TreeMouse\x < 13 ;Test
    MessageRequester("", "Hallo!")
    If ItemState = #False
      SetGadgetItemImage(1, 1, ImageID(3))
      ItemState = #True
    Else
      SetGadgetItemImage(1, 1, ImageID(1))
      ItemState = #False
    EndIf
  EndIf
EndProcedure 

Procedure MouseFrame(win, *mRet.MPos, mx, my, mw, mh)
  Protected mxx, myy
  *mRet\x = 0 : *mRet\y = 0
  mxx = WindowMouseX(win) : myy = WindowMouseY(win)
  If (mxx > mx And mxx < (mw+mx)) And (myy > my And myy < (mh+my))
    *mRet\x = 100 / mw * (mxx-mx)
    *mRet\y = 100 / mh * (myy-my)
  EndIf
EndProcedure

If OpenWindow(0, 0, 0, 320, 250, "FrameGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ButtonGadget(0, 10,  10, 300, 50, "Original Text")
  TreeGadget(1, 10, 80, 300, 140)
  AddGadgetItem(1, -1, "Text 1", ImageID(0),0)
  AddGadgetItem(1, -1, "Text 2", ImageID(1),0)
  AddGadgetItem(1, -1, "Text 3", ImageID(2),0)
  
  ; show the TreeGadget object's content
  Debug QtScript("dump(gadget(1).clicked)")
  
  Debug QtScript(~"gadget(1).clicked.connect(function() { runtime.call(\"QtSignalHandler()\"); })")
  
  ; modify the gadget text   
  QtScript(~"gadget(0).text = \"Click on the green box.\"")   
  
  Repeat
    MouseFrame(0, @TreeMouse, 10, 80, 300, 140)
    Debug "X: " + StrD(TreeMouse\x)
    ;Debug "Y: " + StrD(TreeMouse\y)
  Until WindowEvent() = #PB_Event_CloseWindow
EndIf