I´m trying to add some text for a note in a matplotlib plot, and i want to get that text from a QTextEdit
, which is in a QDialog
that pops-up with a button in the toolbar.
This is my code so far:
class TextoMatplotlib(QDialog): def __init__(self): QDialog.__init__(self) uic.loadUi("insertartextoenMatplotlib.ui", self) # Created in QtDesigner #Matplotlib figure fig = plt.figure(1, figsize=(5,5)) fig.clf() ax = fig.add_subplot(111) ax.plot() self.connect(self.textEdit, SIGNAL("returnPressed()"),self.InsertText)
Here is the method that i want to connect with the QTextEdit
(it´s all in the same class):
def InsertText(self):
nota.insertPlainText(self.textEdit.text())
nota.setText('')
notes = ax.annotate(nota, xy=(), bbox = dict(facecolor="red"))
listadenotas = []
for note in notes:
nota = DraggableNote(note)
nota.connect()
listadenotas.append(nota)
class DraggableNote:
def __init__(self, note):
self.note = nota
self.press = None
def connect(self):
self.cidpress = self.note.figure.canvas.mpl_connect(
"button_pressed_event", self.on_press)
self.cidrelease = self.note.figure.canvas.mpl_connect(
"button_release_event", self.on_release)
self.cidmotion = self.note.figure.canvas.mpl_connect(
"motion_notify_event", self.on_motion)
def on_press(self, event):
if event.inaxes != self.note.axes:
return
contains, attrd = self.note.contains(event)
if not contains:
return
x0, y0 = self.note.xy
self.press = x0, y0, event.xdata, event.ydata
def on_motion(self, event):
if self.press is None:
return
if event.inaxes != self.note.axes:
return
x0, y0, xpress, ypress = self.press
dx = event.xdata - xpress
dy = event.ydata - ypress
self.note.set_x(x0+dx)
self.note.set_y(y0+dy)
self.note.figure.canvas.draw()
def on_release(self, event):
self.press = None
self.note.figure.canvas.draw()
def disconnect(self):
self.note.figure.canvas.mpl_disconnect(self.cidpress)
self.note.figure.canvas.mpl_disconnect(self.cidrelease)
self.note.figure.canvas.mpl_disconnect(self.cidmotion)
I´ve got the "DraggableNote" class from an example in the Matplotlib documentation.
I don´t know where the problem is. I may have copy/paste it wrong in here, but the indentation is correct. Sorry if there is a mistake of that kind.
I hope you can help me. Thank you for your answers.
------------------------------ EDIT------------------------------------------
I tried to make it more easier. This is what i´ve done:
class TextoMatplotlib(QDialog):
def __init__(self):
QDialog.__init__(self)
uic.loadUi("insertartextoenMatplotlib2.ui", self)
self.setWindowTitle("Insertar Nota")
self.setMinimumSize(250, 120)
self.setMaximumSize(250, 120)
self.label.setText("Insertar texto: ")
x = np.arange(0, 5, 0.1);
y = np.sin(x)
fig, ax = plt.subplots()
ax.plot(x,y)
nota = self.textEdit.toPlainText()
self.connect(self.textEdit, SIGNAL("returnPressed()"), self.insertText)
def insertText(self):
note = ax.annotate(nota, xy=(2, 0), bbox=dict(facecolor='yellow'))
note.draggable()
It still does not work. I really don´t know what i´m missing here