0

I want to move the following red cross in the canvas with the mouse events. it should only move when we click on it and drag it with the move. it should stop moving when we release the mouse. I do get the events of the mouse. but I don't know how i can detect that I clicked on the object to make it move. also for example i can't set self.plot1.pos to change its position. we don't have access to that attribute. does anybody have an idea?

I am using python 3.5 and OpenGL Canvas with vispy and a QtWidgets window.

import sys
from PySide2 import QtWidgets
from vispy import scene
from PySide2.QtCore import QMetaObject
from PySide2.QtWidgets import *
import numpy as np


class my_canvas(scene.SceneCanvas):
    def __init__(self):
        super().__init__(keys="interactive")

        self.unfreeze()

        self.view = self.central_widget.add_view()

        self.view.bgcolor = '#ffffff'  # set the canva to a white background

        window_size_0 = 800, 400
        window_center = window_size_0[0] / 2, window_size_0[1] / 2
        crosshair_max_length = 50

        data_1 = np.random.normal(size=(2, 2))
        data_1[0] = window_center[0] - crosshair_max_length, window_center[1]
        data_1[1] = window_center[0] + crosshair_max_length, window_center[1]

        data_2 = np.random.normal(size=(2, 2))
        data_2[0] = window_center[0], window_center[1] - crosshair_max_length
        data_2[1] = window_center[0], window_center[1] + crosshair_max_length

        self.plot1 = scene.Line(data_1, parent=self.view.scene, color="r")
        self.plot2 = scene.Line(data_2, parent=self.view.scene, color="r")

        self.selected_object = None
        self.freeze()



    def on_mouse_press(self, event):

        if event.button == 1:
            print("pressed left")

        if event.button == 2:
            print("pressed right")

    def on_mouse_move(self, event):
        pass

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName("MainWindow")
        MainWindow.resize(748, 537)

        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout = QGridLayout(self.centralwidget)
        self.gridLayout.setObjectName("gridLayout")

        self.groupBox = QGroupBox(self.centralwidget)
        self.groupBox.setObjectName("groupBox")

        self.gridLayout.addWidget(self.groupBox, 0, 0, 1, 1)

        MainWindow.setCentralWidget(self.centralwidget)

        QMetaObject.connectSlotsByName(MainWindow)


class MainWindow(QtWidgets.QMainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # OpenGL drawing surface
        self.canvas = my_canvas()
        self.canvas.create_native()
        self.canvas.native.setParent(self)

        self.setWindowTitle('MyApp')

def main():
    import ctypes
    ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID('my_gui')

    app = QtWidgets.QApplication([])

    main_window = MainWindow()
    main_window.show()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
EricD1990
  • 33
  • 1
  • 5
  • I can easily the region around it. but my actual problem is moving it... how can I force the position to something else – EricD1990 Nov 24 '22 at 10:09

1 Answers1

0

i found the solution for the ones who are interested.

self.plot1.set_data(pos=...)

with this method we can move it easily

EricD1990
  • 33
  • 1
  • 5
  • You may want to also look at using `my_vis.transform = STTransform(...)`. This would allow you to transform the coordinates on the GPU (scale and translate) without having to reupload the vertices to the GPU every update. If you do all future updates with `my_vis.transform.translate = (x, y, z)` then you'll also avoid recompiling the shader for every update. – djhoese Nov 28 '22 at 15:30