I have a usb barcode scanner which I am connecting to my computer. Everytime it scans a barcode, it types the data into the computer like a keyboard. My goal was to have the data be typed into PyQT5 Table widget.
I have created the table below and I simply scan the items into it. The problem is that when I scan an item, it edits the first cell, but the cursor does not move automatically to the next row so I can scan a new item into the table. I have to click on the second cell and then scan the item. Then click on the third cell and scan the item and so on.
I was wondering how I can automate it so that after an item is scanned into the first cell, it automatically moves to the next cell and waits for input from the scanner?
import sys
from PyQt5.QtWidgets import *
#Main Window
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'Specimen Dashboard'
self.setWindowTitle(self.title)
self.tableWidget = QTableWidget()
self.createTable()
self.tableWidget.itemChanged.connect(self.go_to_next_row)
self.layout = QVBoxLayout()
self.layout.addWidget(self.tableWidget)
self.setLayout(self.layout)
self.show()
def go_to_next_row(self):
#Not working
#Trying to see if I can automatically move to next cell, but editing it
self.tableWidget.setItem(1,0, QTableWidgetItem("Name"))
#Create table
def createTable(self):
self.tableWidget.setRowCount(4)
self.tableWidget.setColumnCount(2)
self.tableWidget.horizontalHeader().setStretchLastSection(True)
self.tableWidget.horizontalHeader().setSectionResizeMode(
QHeaderView.Stretch)
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
By default the scanners send an endline("\n") that is translated a Return or Enter key and this by default closes the editor, in this case that event must be intercepted, move the cursor and open the editor:
import sys
from PyQt5 import QtCore, QtWidgets
class TableWidget(QtWidgets.QTableWidget):
def keyPressEvent(self, event):
if (
event.key() in (QtCore.Qt.Key_Enter, QtCore.Qt.Key_Return)
and self.state() == QtWidgets.QAbstractItemView.EditingState
):
index = self.moveCursor(
QtWidgets.QAbstractItemView.MoveNext, QtCore.Qt.NoModifier
)
self.selectionModel().setCurrentIndex(
index, QtCore.QItemSelectionModel.ClearAndSelect
)
self.edit(index)
else:
super().keyPressEvent(event)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.tableWidget = TableWidget(4, 2)
self.setCentralWidget(self.tableWidget)
self.tableWidget.horizontalHeader().setStretchLastSection(True)
self.tableWidget.horizontalHeader().setSectionResizeMode(
QtWidgets.QHeaderView.Stretch
)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
You can subclass the table and overwrite closeEditor(): the hint argument tells the view what should happen when the editor has been closed; by default, when pressing Enter the current cell data is submitted, but you can override this behavior like this:
from PyQt5 import QtGui, QtWidgets
class Table(QtWidgets.QTableView):
# leave to False for the default behavior (the next cell is the one at the
# right of the current, or the first of the next row; when set to True it
# will always go to the next row, while keeping the same column
useNextRow = False
def closeEditor(self, editor, hint):
if hint == QtWidgets.QAbstractItemDelegate.SubmitModelCache:
if self.useNextRow:
super().closeEditor(editor, hint)
current = self.currentIndex()
newIndex = current.sibling(current.row() + 1, current.column())
if newIndex.isValid():
self.setCurrentIndex(newIndex)
self.edit(newIndex)
return
else:
hint = QtWidgets.QAbstractItemDelegate.EditNextItem
super().closeEditor(editor, hint)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
test = Table()
test.show()
model = QtGui.QStandardItemModel(10, 5)
test.setModel(model)
sys.exit(app.exec_())
My main objective is to show a constantly evolving value on a Qt-window textEdit. (this window contains only a checkBox and a textEdit).
Sadly, I cannot click on the checkbox and the window is frozen until I shutdown the terminal.
import sys
from threading import Thread
from random import randint
import time
from PyQt4 import QtGui,uic
class MyThread(Thread):
def __init__(self):
Thread.__init__(self)
#function to continually change the targeted value
def run(self):
for i in range(1, 20):
self.a = randint (1, 10)
secondsToSleep = 1
time.sleep(secondsToSleep)
class MyWindow(QtGui.QMainWindow,Thread):
def __init__(self):
Thread.__init__(self)
super(MyWindow,self).__init__()
uic.loadUi('mywindow.ui',self)
self.checkBox.stateChanged.connect(self.checkeven)
self.show()
#i show the value only if the checkbox is checked
def checkeven(self):
while self.checkBox.isChecked():
self.textEdit.setText(str(myThreadOb1.a))
# Run following code when the program starts
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
# Declare objects of MyThread class
myThreadOb1 = MyThread()
myThreadOb2 = MyWindow()
# Start running the threads!
myThreadOb1.start()
myThreadOb2.start()
sys.exit(app.exec_())
At the moment I'm using a thread to set a random value to a, but at the end it is supposed to be a bit more complex as I will have to take the values from an automation.
Do you have any clue why my code is acting like this?
Thank you very much for your help.
The problem is that the while self.checkBox.isChecked() is blocking, preventing the GUI from handling other events.
Also you should not run a PyQt GUI on another thread than the main one.
If you want to send data from one thread to another, a good option is to use the signals.
Doing all these considerations we have the following:
import sys
from threading import Thread
from random import randint
import time
from PyQt4 import QtGui, uic, QtCore
class MyThread(Thread, QtCore.QObject):
aChanged = QtCore.pyqtSignal(int)
def __init__(self):
Thread.__init__(self)
QtCore.QObject.__init__(self)
#function to continually change the targeted value
def run(self):
for i in range(1, 20):
self.aChanged.emit(randint(1, 10))
secondsToSleep = 1
time.sleep(secondsToSleep)
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow,self).__init__()
uic.loadUi('mywindow.ui',self)
self.thread = MyThread()
self.thread.aChanged.connect(self.on_a_changed, QtCore.Qt.QueuedConnection)
self.thread.start()
self.show()
#i show the value only if the checkbox is checked
#QtCore.pyqtSlot(int)
def on_a_changed(self, a):
if self.checkBox.isChecked():
self.textEdit.setText(str(a))
# Run following code when the program starts
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
# Declare objects of MyThread class
w = MyWindow()
sys.exit(app.exec_())
An option more to the style of Qt would be to use QThread since it is a class that handles a thread and is a QObject so it handles the signals easily.
import sys
from random import randint
from PyQt4 import QtGui, uic, QtCore
class MyThread(QtCore.QThread):
aChanged = QtCore.pyqtSignal(int)
def run(self):
for i in range(1, 20):
self.aChanged.emit(randint(1, 10))
secondsToSleep = 1
QtCore.QThread.sleep(secondsToSleep)
class MyWindow(QtGui.QMainWindow):
def __init__(self):
super(MyWindow,self).__init__()
uic.loadUi('mywindow.ui',self)
self.thread = MyThread()
self.thread.aChanged.connect(self.on_a_changed, QtCore.Qt.QueuedConnection)
self.thread.start()
self.show()
#i show the value only if the checkbox is checked
#QtCore.pyqtSlot(int)
def on_a_changed(self, a):
if self.checkBox.isChecked():
self.textEdit.setText(str(a))
# Run following code when the program starts
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
# Declare objects of MyThread class
w = MyWindow()
sys.exit(app.exec_())
I'm building a little pyqt5 app that display a list of 512 values in a QListView. The list is updated through separate thread, with QThread.
It's working nice, except the fact that it is using 65/95 % of the CPU of the (old) Core 2 Duo 2,53 Ghz I'm developing on.
I simplify the code to remove dependancies, because the update is done from a network protocol. Updates are done 40 times per second (each 25 ms).
The simplified script below is refreshing the list 10 times per second and the CPU is still at 65 % when list is updated.
Is there anything to do for avoiding the overload?
Is there some best practices to follow for updating a View?
(the global is not in my last code, it's here to have a simple example)
from random import randrange
from time import sleep
from sys import argv, exit
from PyQt5.QtCore import QThread, QAbstractListModel, Qt, QVariant, pyqtSignal
from PyQt5.QtWidgets import QListView, QApplication, QGroupBox, QVBoxLayout, QPushButton
universe_1 = [0 for i in range(512)]
class SpecialProcess(QThread):
universeChanged = pyqtSignal()
def __init__(self):
super(SpecialProcess, self).__init__()
self.start()
def run(self):
global universe_1
universe_1 = ([randrange(0, 101, 2) for i in range(512)])
self.universeChanged.emit()
sleep(0.1)
self.run()
class Universe(QAbstractListModel):
def __init__(self, parent=None):
super(Universe, self).__init__(parent)
def rowCount(self, index):
return len(universe_1)
def data(self, index, role=Qt.DisplayRole):
index = index.row()
if role == Qt.DisplayRole:
try:
return universe_1[index]
except IndexError:
return QVariant()
return QVariant()
class Viewer(QGroupBox):
def __init__(self):
super(Viewer, self).__init__()
list_view = QListView()
self.list_view = list_view
# create a vertical layout
vbox = QVBoxLayout()
universe = Universe()
vbox.addWidget(list_view)
# Model and View setup
self.model = Universe(self)
self.list_view.setModel(self.model)
# meke a process running in parallel
my_process = SpecialProcess()
my_process.universeChanged.connect(self.model.layoutChanged.emit)
# set the layout on the groupbox
vbox.addStretch(1)
self.setLayout(vbox)
if __name__ == "__main__":
app = QApplication(argv)
group_widget = Viewer()
group_widget.show()
exit(app.exec_())
It seems to be a normal behavior…
I'm trying to populate a QTableWidget with the attributes of a file opened elsewhere in the script. I've successfully set the file attributes read in from the file using the following code:
class FileHeader(object):
fileheader_fields=(
"filetype","fileversion","numframes",
"framerate","resolution","numbeams",
"samplerate","samplesperchannel","receivergain",
"windowstart","winlengthsindex","reverse",
"serialnumber","date","idstring","ID1","ID2",
"ID3","ID4","framestart","frameend","timelapse",
"recordInterval","radioseconds","frameinterval","userassigned")
fileheader_formats=(
'S3','B','i4','i4','i4','i4','f','i4','i4','i4',
'i4','i4','i4','S32','S256','i4','i4','i4','i4',
'i4','i4','i4','i4','i4','i4','S136')
IMAGE_HEADER_BYTES = 256
IMAGES_DATA_BYTES = 49152
def __init__(self,filename=''):
self.filename = filename
if filename:
self.setFile(filename)
else:
# initialize our attributes to None
for field in self.fileheader_fields:
setattr(self,field,None)
def setFile(self, f):
self.infile=open(f, 'rb')
dtype=dict(names=self.fileheader_fields, formats=self.fileheader_formats)
self.fileheader=np.fromfile(self.infile, dtype=dtype, count=1)
self.fileheader_length=self.infile.tell()
for field in self.fileheader_fields:
setattr(self,field,self.fileheader[field])
I've used this code to populate the table but I keep getting a "FileHeader has no attribute fileheader" error.
from fileheader import FileHeader, Frame
from echogram import QEchogram
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import os, sys
class MainWindow(QWidget):
def __init__(self, parent=None):
self.fileheader_fields=FileHeader.fileheader_fields
self.fileheader_values=FileHeader.fileheader[field]
self.fileheader={field: "value of" + field
for field in self.fileheader_fields}
super(MainWindow, self).__init__(parent)
self.fileheader_table=QTableWidget()
layout=QVBoxLayout()
layout.addWidget(self.fileheader_table)
self.setLayout(layout)
self.populate
def populate(self):
self.fileheader_table.setRowCount(len(self.fileheader_fields))
self.fileheader_table.sestColumnCount(2)
self.fileheader_table.setHorizontalHeaderLabels(['name','value'])
for i,field in enumerate(self.fileheader_fields):
name=QTableWidgetItem(field)
value=QTableWidgetItem(self.fileheader[field])
self.fileheader_table.setItem(i,0,name)
self.fileheader_table.setItem(i,1,value)
if __name__=="__main__":
app=QApplication(sys.argv)
filename=str(QFileDialog.getOpenFileName(None,"open file","C:/vprice/DIDSON/DIDSON Data","*.ddf"))
wnd=MainWindow()
wnd.resize(640,480)
wnd.show()
#echoGram=QEchogram()
#echoGram.initFromFile(filename)
#fileName="test.png"
#echoGram.processEchogram()
#dataH=echoGram.data
#print "Horizontal data", dataH
Bear with me-- I just started with all of the Python stuff about a month ago...
See populate method. Also there is some examples in documentation
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtGui
class MainWindow(QtGui.QWidget):
def __init__(self, parent=None):
self.fileheader_fields=(
"filetype","fileversion","numframes",
"framerate","resolution","numbeams",
"samplerate","samplesperchannel","receivergain",
"windowstart","winlengthsindex","reverse",
"serialnumber","date","idstring","ID1","ID2",
"ID3","ID4","framestart","frameend","timelapse",
"recordInterval","radioseconds","frameinterval","userassigned"
)
# just for test
self.fileheader = {field: 'value of ' + field
for field in self.fileheader_fields}
super(MainWindow, self).__init__(parent)
self.table_widget = QtGui.QTableWidget()
layout = QtGui.QVBoxLayout()
layout.addWidget(self.table_widget)
self.setLayout(layout)
self.populate()
def populate(self):
self.table_widget.setRowCount(len(self.fileheader_fields))
self.table_widget.setColumnCount(2)
self.table_widget.setHorizontalHeaderLabels(['name', 'value'])
for i, field in enumerate(self.fileheader_fields):
name = QtGui.QTableWidgetItem(field)
value = QtGui.QTableWidgetItem(self.fileheader[field])
self.table_widget.setItem(i, 0, name)
self.table_widget.setItem(i, 1, value)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
wnd = MainWindow()
wnd.resize(640, 480)
wnd.show()
sys.exit(app.exec_())
UPD
Code for your concrete case:
from fileheader import FileHeader, Frame
from echogram import QEchogram
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import os, sys
class MainWindow(QWidget):
def __init__(self, filename, parent=None):
super(MainWindow, self).__init__(parent)
# here we are loading file
# now self.fileheader contains attributes
self.fileheader = FileHeader(filename)
self.fileheader_table = QTableWidget()
layout = QVBoxLayout()
layout.addWidget(self.fileheader_table)
self.setLayout(layout)
self.populate()
def populate(self):
self.fileheader_table.setRowCount(len(self.fileheader.fileheader_fields))
self.fileheader_table.sestColumnCount(2)
self.fileheader_table.setHorizontalHeaderLabels(['name','value'])
for i,field in enumerate(self.fileheader.fileheader_fields):
name=QTableWidgetItem(field)
value=QTableWidgetItem(getattr(self.fileheader, field))
self.fileheader_table.setItem(i,0,name)
self.fileheader_table.setItem(i,1,value)
if __name__=="__main__":
app=QApplication(sys.argv)
filename=str(QFileDialog.getOpenFileName(None,"open file","C:/vprice/DIDSON/DIDSON Data","*.ddf"))
wnd=MainWindow(filename)
wnd.resize(640,480)
wnd.show()
#echoGram=QEchogram()
#echoGram.initFromFile(filename)
#fileName="test.png"
#echoGram.processEchogram()
#dataH=echoGram.data
#print "Horizontal data", dataH
ok u have to add a QTableWidget to your custom QtGui.QMainWindow
i would create i whith 0 line and the right amount of collumn,like this:
self.tableArea=QtGui.QTableWidget(0,self.fileheader_length,self)
self.setHorizontalHeaderLabels(['nameOne','nameTwo'])#will name your top header
self.verticalHeader().setVisible(False)#will hide your left header if you don't need them
and add your line one by one
self.tableArea.insertRow(row)#it will add a line at the row position
after that u have to create a QTableWidgetItem for each cell
itemTo=QtGui.QTableWidgetItem('text')#replace text by your value
and place your item in the cell you want
self.tableArea.setItem(row, column, itemTo)
I have this sample of code:
import sys
import time
from PyQt4.QtGui import *
from PyQt4.QtCore import *
class Bar(QDialog):
def __init__(self, parent=None):
super(Bar, self).__init__()
self.pbar = QProgressBar(self)
self.pbar.setValue(0)
layout = QHBoxLayout()
layout.addWidget(self.pbar)
self.setLayout(layout)
def main(self):
for value in range(1, 100):
time.sleep(1)
print value
self.pbar.setValue(value)
app = QApplication(sys.argv)
form = Bar()
form.show()
form.main()
app.exec_()
I expect progressbar's value to increased by 1 every second.
Instead, although that all values printed on the screen, the progressbar shows only some of them. Also, the bar appears just when value == 5. I know how to achieve the appropriate result with QBasicTimer(), but why this one does not work? Did i make a stupid mistake?
Try adding a
QApplication.processEvents()
just after print value (this should force the UI to update).