0

I would replace the button in the bottom (unter the x-axes of the graph). I read How do I add a QPushButton to PyQtGraph using addItem? but I have no clue how I can replace the button.

Best,

import pandas as pd 
import numpy as np 
import glob 
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

app = pg.mkQApp()
mw = QtGui.QMainWindow()
mw.setWindowTitle('Spectrum analysis')
#mw.resize(400,400) #Window size

cw = QtGui.QWidget()
proxy = QtGui.QGraphicsProxyWidget()
button = QtGui.QPushButton("Clip")
proxy.setWidget(button)
mw.setCentralWidget(cw)
l = QtGui.QVBoxLayout()
cw.setLayout(l)

pw3 = pg.PlotWidget()
l.addWidget(pw3)

mw.show()

#Plot
curve = pw3.plot(np.random.normal(size=100)*1e0, clickable=True)
curve.curve.setClickable(True)
curve.setPen('w')  ## white pen
curve.setShadowPen(pg.mkPen((70,70,30), width=6, cosmetic=True))

def clicked():
    print("curve clicked")
curve.sigClicked.connect(clicked)

def regionUpdate(regionItem):
    lo,hi = regionItem.getRegion()
    print(lo, hi)

lr = pg.LinearRegionItem([1, 30], movable=True)
lr.sigRegionChanged.connect(regionUpdate)
pw3.addItem(lr)
pw3.addItem(proxy)

if __name__ == '__main__':
    pg.exec()
Cosis94
  • 63
  • 7

1 Answers1

1

Because you already have a QVBoxLayout, you don't really need to mess with a QGraphicsProxyWidget. The code below displays the button properly. I moved the lines cw.setLayout(l) and mw.show() towards the end of script so they don't get called before you add the button to the layout.

#import pandas as pd
import numpy as np
import glob
from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg

app = pg.mkQApp()
mw = QtGui.QMainWindow()
mw.setWindowTitle('Spectrum analysis')
#mw.resize(400,400) #Window size

cw = QtGui.QWidget()
proxy = QtGui.QGraphicsProxyWidget()
button = QtGui.QPushButton("Clip")
#proxy.setWidget(button)
mw.setCentralWidget(cw)
l = QtGui.QVBoxLayout()


pw3 = pg.PlotWidget()
l.addWidget(pw3)



#Plot
curve = pw3.plot(np.random.normal(size=100)*1e0, clickable=True)
curve.curve.setClickable(True)
curve.setPen('w')  ## white pen
curve.setShadowPen(pg.mkPen((70,70,30), width=6, cosmetic=True))

def clicked():
    print("curve clicked")
curve.sigClicked.connect(clicked)

def regionUpdate(regionItem):
    lo,hi = regionItem.getRegion()
    print(lo, hi)

lr = pg.LinearRegionItem([1, 30], movable=True)
lr.sigRegionChanged.connect(regionUpdate)
pw3.addItem(lr)
# ~ pw3.addItem(proxy)
l.addWidget(button)
cw.setLayout(l)
mw.show()

if __name__ == '__main__':
    pg.exec()

This code is really creating a PyQt application, and I suggest you familiarize yourself with layouts in PyQt. The official docs for Qt Layouts is here and there are quite a few tutorials out there for PyQt. I've always liked ZetCode.

bfris
  • 5,272
  • 1
  • 20
  • 37