pyqtgraph custom tick labels potential bug - qt

I'm using PyQtGraph and embedding a graph into a Qt program. I've added axisItems to the PlotWidget's constructor. It seems to do something a bit weird; I get some off line with 0___1 in the top right-hand corner. Does anyone know what is causing this and/or how to get rid of it?
I've attached a picture showing this and my code to generate what's in the picture.
If I don't add the AxisItem to the constructor (hence not getting my custom tick labels), the issue doesn't occur.
import sys
import numpy as np
from PySide import QtCore, QtGui, QtUiTools, QtXml
from ui import *
import pyqtgraph as pg
class PlotWidgetBarGraph(pg.PlotWidget):
def __init__(self, **opts):
# Add custom tick strings.
xDict = {1.:'1', 2.:'2', 3.:'3', 4.:'4',
5.:'5', 6.:'6', 7.:'7', 8.:'8',
9.:'1/2', 10.:'1/3', 11.:'1/4',
12.:'2/3', 13.:'2/4', 14.:'3/4',
15.:'1/2/3', 16.:'2/3/4',
17:'1/2/3/4'}
xAxis = pg.AxisItem(orientation='bottom')
xAxis.setTicks([xDict.items(), []])
super().__init__(axisItems={'bottom': xAxis})
# Create and add bar graph.
bg = pg.BarGraphItem(**opts)
self.addItem(bg)
class MainWindow(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
splitter = QtGui.QSplitter(QtCore.Qt.Vertical)
x = np.arange(20)
y = np.abs(np.sin(x))+0.1
plot1 = PlotWidgetBarGraph(x=x, height=y, width=0.8, brush='b')
plot2 = PlotWidgetBarGraph(x=x, height=y, width=0.8, brush='b')
splitter.addWidget(plot1)
splitter.addWidget(plot2)
splitter.setStretchFactor(0, 1)
splitter.setStretchFactor(1, 2)
self.verticalLayout.addWidget(splitter)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

Related

qt: qlayout do not correctly add widget

I want do dynamically change the layout in Qt. For example, I want to change the QHBoxLayout to QVBoxLayout through a button. My test code is:
from PyQt5.QtWidgets import *
import sys
class SubWidget(QWidget):
def __init__(self):
super().__init__()
self.lay = QHBoxLayout()
self.label1 = QLabel('left')
self.label2 = QLabel('right')
self.lay.addWidget(self.label1)
self.lay.addWidget(self.label2)
self.setLayout(self.lay)
def change(self):
self.lay.removeWidget(self.label1)
self.lay.removeWidget(self.label2)
self.lay = QVBoxLayout()
self.setLayout(self.lay)
self.lay.addWidget(self.label2)
self.lay.addWidget(self.label1)
class Widget(QWidget):
def __init__(self):
super().__init__()
lay = QVBoxLayout()
self.btn = QPushButton('change layout')
self.btn.clicked.connect(self.btnClick)
self.subWidget = SubWidget()
lay.addWidget(self.btn)
lay.addWidget(self.subWidget)
self.setLayout(lay)
def btnClick(self, check=False):
self.subWidget.change()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Widget()
win.show()
app.exec_()
The code output GUI is:
And I hope it change to the following picture after click change layout button:
Any suggestion is appreciated~~~
The point of the solution is
Make sure old layout is deleted, included both python wrapping reference and core Qt object, that's for deleteLater() is used.
Make sure new layout is assigned strictly after old was deleted, that's for need to use destroyed()-switchlayout() signal-slot chain.
I reproduced example with PySide6 (don't forget to switch on your version of PyQt or PySide package):
# from PyQt5.QtWidgets import *
from PySide6.QtWidgets import *
import sys
class SubWidget(QWidget):
def __init__(self):
super().__init__()
self.lay = QVBoxLayout()
self.label1 = QLabel('left')
self.label2 = QLabel('right')
self.lay.addWidget(self.label1)
self.lay.addWidget(self.label2)
self.setLayout(self.lay)
def change(self):
self.lay.removeWidget(self.label1)
self.lay.removeWidget(self.label2)
self.lay.deleteLater()
self.lay.destroyed.connect(self.switchlayout)
def switchlayout(self):
# print("***destroyed")
self.lay = QHBoxLayout()
self.lay.addWidget(self.label2)
self.lay.addWidget(self.label1)
self.setLayout(self.lay)
self.adjustSize()
class Widget(QWidget):
def __init__(self):
super().__init__()
lay = QVBoxLayout()
self.btn = QPushButton('change layout')
self.btn.clicked.connect(self.btnClick)
self.subWidget = SubWidget()
lay.addWidget(self.btn)
lay.addWidget(self.subWidget)
self.setLayout(lay)
def btnClick(self, check=False):
self.subWidget.change()
if __name__ == '__main__':
app = QApplication(sys.argv)
win = Widget()
win.show()
app.exec_()

QMovie setting filename second time wont work

I am setting the Initial Movie File like this:
self.movie = QMovie("dotGreenBlack.gif", QByteArray(), self)
At runtime, i want to change it by setting:
self.movie.setFileName("dotGreyStatic.gif")
But this leads to frame-freeze of the primarly set file "dotGreenBlack.gif"
Anything else i have to do to change the Gif-Animation on runtime?
Here is the fullcode:
from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
class ImagePlayer(QWidget):
def __init__(self, filename, title, parent=None):
QWidget.__init__(self, parent)
# Load the file into a QMovie
self.movie = QMovie(filename, QByteArray(), self)
size = self.movie.scaledSize()
self.setGeometry(200, 200, size.width(), size.height())
self.setWindowTitle(title)
self.movie_screen = QLabel()
# Make label fit the gif
self.movie_screen.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.movie_screen.setAlignment(Qt.AlignCenter)
# Create the layout
main_layout = QVBoxLayout()
main_layout.addWidget(self.movie_screen)
self.setLayout(main_layout)
# Add the QMovie object to the label
self.movie.setCacheMode(QMovie.CacheAll)
self.movie.setSpeed(100)
self.movie_screen.setMovie(self.movie)
self.movie.start()
self.movie.loopCount()
self.movie.setFileName("dotGreyStatic.gif")
self.movie_screen.setMovie(self.movie)
if __name__ == "__main__":
gif = "dotGreenBlack.gif"
app = QApplication(sys.argv)
app.setStyleSheet("QWidget { background-color: black }")
player = ImagePlayer(gif, "was")
player.show()
sys.exit(app.exec_())
Gif Icons used in this Example:
You just need to stop and restart the movie:
self.movie.stop()
self.movie.setFileName("dotGreyStatic.gif")
self.movie.start()

cannot expand the progress bar to the full width of status bar

I want the progress bar to expand to the full width of status bar,but why there is a gap there?
PS. can I add some text on the progress bar,there is no function like setText(), how can I do this ?
Is there a widget there or something else ?
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.resize(800, 600)
# self.lb=QLabel('finding resource ')
self.pb = QProgressBar()
self.pb.setRange(0, 0)
# self.pb.setTextVisible(False)
# self.statusBar().addPermanentWidget(self.lb)
self.statusBar().setSizeGripEnabled(False)
# print(self.statusBar().layout() )
self.statusBar().setStyleSheet("QStatusBar::item {border: none;}")
self.statusBar().addPermanentWidget(self.pb, 1)
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = MainWindow()
ui.show()
sys.exit(app.exec_())
just set
self.pb.setTextVisible(False)
I think this solves your problem.
import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.pb = QProgressBar()
# To make the text visible, you must not setRange(0, 0) and
# you must setValue(something valid). Otherwise, the text is
# hidden.
#
# In the default Windows style, the blank space to the right
# of the progress bar is room for this text. Calling
# setTextVisible(False) removes this space. Other styles will
# place the text in the middle of the progress bar.
#
# Unfortunately, I don't see any (simply) way to display a
# spinning progres bar AND text at the same time.
self.pb.setRange(0, 9)
self.pb.setValue(1)
self.pb.setFormat('finding resource...')
self.statusBar().setSizeGripEnabled(False)
self.statusBar().addPermanentWidget(self.pb, 1)
if __name__ == "__main__":
app = QApplication(sys.argv)
ui = MainWindow()
ui.show()
sys.exit(app.exec_())

Python code to change displayed image with pyqt4 on request

I have the following code to display am image using pyQt:
app = QtGui.QApplication(sys.argv)
window = QtGui.QMainWindow()
window.setGeometry(opts.posx, opts.posy, opts.width, opts.height)
pic = QtGui.QLabel(window)
pic.setGeometry(5, 5, opts.width-10, opts.height-10)
pixmap = QtGui.QPixmap(opts.filename)
pixmap = pixmap.scaledToHeight(opts.height)
pic.setPixmap(pixmap)
window.show()
sys.exit(app.exec_())
I would like to wrap up this code possibly in the form of a class, and be able to set a different image during runtime, using signals, socket, threads I really do not know. I would imagine something like:
class MyImage(object):
def __init(self, args):
some setup code
self.pic = whatever
def set_image(self, filename):
pixmap = QtGui.QPixmap(opts.filename)
pixmap = pixmap.scaledToHeight(opts.height)
pic.setPixmap(pixmap)
With the original code I just call sys.exit(app.exec_()) which makes the code 'freeze'. But I want to send a signal (and a filename) from a different running python code. Any suggestion how this can be handled easily and straightforward? Maybe overwriting the app.exec_ method?
Something like this should work for you:
#!/usr/bin/env python
#-*- coding:utf-8 -*-
import sip
sip.setapi('QString', 2)
sip.setapi('QVariant', 2)
from PyQt4 import QtGui, QtCore
class ImageChanger(QtGui.QWidget):
def __init__(self, images, parent=None):
super(ImageChanger, self).__init__(parent)
self.comboBox = QtGui.QComboBox(self)
self.comboBox.addItems(images)
self.layout = QtGui.QVBoxLayout(self)
self.layout.addWidget(self.comboBox)
class MyWindow(QtGui.QWidget):
def __init__(self, images, parent=None):
super(MyWindow, self).__init__(parent)
self.label = QtGui.QLabel(self)
self.imageChanger = ImageChanger(images)
self.imageChanger.move(self.imageChanger.pos().y(), self.imageChanger.pos().x() + 100)
self.imageChanger.show()
self.imageChanger.comboBox.currentIndexChanged[str].connect(self.changeImage)
self.layout = QtGui.QVBoxLayout(self)
self.layout.addWidget(self.label)
#QtCore.pyqtSlot(str)
def changeImage(self, pathToImage):
pixmap = QtGui.QPixmap(pathToImage)
self.label.setPixmap(pixmap)
if __name__ == "__main__":
import sys
images = [ "/path/to/image/1",
"/path/to/image/2",
"/path/to/image/3",
]
app = QtGui.QApplication(sys.argv)
app.setApplicationName('MyWindow')
main = MyWindow(images)
main.show()
sys.exit(app.exec_())

Controling a matplotlib plot from a separate window

I'm trying to create the simplest example of controlling a plot in one window from buttons and functions in another window. I know I'm going to need more things in the window with the plot, so, I need to create my plot figure withing a QT window.
This is what I have so far:
My control window:
import sys
from PyQt4.QtCore import (QSize, SIGNAL)
from PyQt4.QtGui import (QApplication, QDialog, QPushButton, QVBoxLayout)
import PlotWindow
class Window(QDialog):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.pushButton1 = QPushButton()
self.pushButton1.setMaximumSize(QSize(110, 24))
self.pushButton1.setObjectName("pushButton1")
self.pushButton1.setText("Open Plot")
self.pushButton2 = QPushButton()
self.pushButton2.setMaximumSize(QSize(110, 24))
self.pushButton2.setObjectName("pushButton2")
self.pushButton2.setText("Update Plot")
layout = QVBoxLayout()
layout.addWidget(self.pushButton1)
layout.addWidget(self.pushButton2)
self.setLayout(layout)
self.setWindowTitle("Plot Control")
self.connect(self.pushButton1,SIGNAL("clicked()"),self.plotNew)
def plotNew(self):
print "new plot window"
self.dialog = PlotWindow.PlotWindow()
self.dialog.show()
app = QApplication(sys.argv)
form = Window()
form.open()
app.exec_()
My plot window :
import matplotlib
import numpy as np
from PyQt4 import QtGui
from matplotlib.lines import Line2D
import matplotlib.pyplot as plt
import matplotlib.animation as animation
class PlotWindow(QtGui.QDialog):
def __init__(self, parent=None):
super(PlotWindow, self).__init__(parent)
self.fig = plt.figure()
self.ax = self.fig.add_subplot(111)
plt.draw()
Obviously my plot window is where I'm having the biggest problems. I've gone through lots of examples, but none of them seem to show how to place a plot within another window.
Thanks
Well, I can answer part of my own question, but I would like comments on whether or not this is the best way to accomplish. The following code shows the changes I've made to bring up a second window with a plot inside.
import sys
from PyQt4 import QtGui
import numpy as np
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
class PlotCanvas(FigureCanvas):
def __init__(self, parent):
self.fig = Figure()
self.axes = self.fig.add_subplot(111)
# self.x = np.arange(0.0,0.3,0.01)
# self.y = np.cos(2*np.pi*self.x)
# self.axes.plot(self.x,self.y)
FigureCanvas.__init__(self, self.fig)
self.setParent(parent)
FigureCanvas.setSizePolicy(self,QtGui.QSizePolicy.Expanding,QtGui.QSizePolicy.Expanding)
class PlotWindow(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.setWindowTitle("Plot Figure")
self.main_widget = QtGui.QWidget(self)
vbl = QtGui.QVBoxLayout(self.main_widget)
qmc = PlotCanvas(self.main_widget)
ntb = NavigationToolbar(qmc, self.main_widget)
vbl.addWidget(qmc)
vbl.addWidget(ntb)
self.main_widget.setFocus()
self.setCentralWidget(self.main_widget)
I still need help with setting the data for the plot from the other window.
Thanks

Resources