PyQT5 Moving focus between list boxes using arrow keys - python

I have a QT widget that has two list boxes that are wrapped in QFrames along with a label and placed side-by-side on QWidget. I can move between them using the tab key but I would like to move between them using the left and right arrow keys. What is the best way to do this?
My QWidget and ListWidgets look like the following
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.init_ui()
def init_ui(self):
hbox = QHBoxLayout()
hbox.addWidget(Left())
hbox.addWidget(Right())
self.setLayout(hbox)
self.show()
class Left(QFrame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
header = QLabel()
header.setText('Left')
l = QListWidget()
items = ['Item %s' % (i + 1) for i in range(10)]
l.addItems(items)
vbox = QVBoxLayout()
vbox.addWidget(header)
vbox.addWidget(l)
self.setLayout(vbox)
class Right(QFrame):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
header = QLabel()
header.setText('Right')
l = QListWidget()
items = ['Item %s' % (i + 1) for i in range(10)]
l.addItems(items)
vbox = QVBoxLayout()
vbox.addWidget(header)
vbox.addWidget(l)
self.setLayout(vbox)
def main():
app = QApplication(sys.argv)
main_window = MainWindow()
sys.exit(app.exec_())
if __name__ == '__main__':
main()

You can install an event-filter on the list-widgets and then use the focusNextPrevChild method of their parent widget to move the focus:
class MainWindow(QWidget):
...
def init_ui(self):
hbox = QHBoxLayout()
self.left = Left()
self.left.installEventFilter(self)
hbox.addWidget(self.left)
self.right = Right()
self.right.installEventFilter(self)
hbox.addWidget(self.right)
self.setLayout(hbox)
self.show()
def eventFilter(self, source, event):
if (event.type() == QEvent.KeyPress and
(event.key() == Qt.Key_Left or event.key() == Qt.Key_Right) and
event.modifiers() == Qt.NoModifier and
(source is self.left or source is self.right)):
self.focusNextPrevChild(event.key() == Qt.Key_Right)
return True
return super(MainWindow, self).eventFilter(source, event)

Related

Move focus to another widget when the return or right arrow key is pressed?

I have a Qt widget that looks like this:
class launchiiwidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
layout = QtWidgets.QVBoxLayout(self)
self.textbox = QtWidgets.QTextEdit(self)
self.textbox.setLineWrapMode(QtWidgets.QTextEdit.NoWrap)
self.textbox.setAlignment(QtCore.Qt.AlignCenter)
self.textbox.setFixedSize(QtCore.QSize(600, 100))
self.textbox.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.textbox.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
layout.addWidget(self.textbox)
font = self.textbox.font()
font.setPointSize(80)
self.textbox.setFont(font)
self.listwidget = QtWidgets.QListWidget(self)
self.listwidget.addItem("Red")
self.listwidget.addItem("Blue")
layout.addWidget(self.listwidget)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = launchiiwidget()
widget.setWindowFlags(QtCore.Qt.FramelessWindowHint)
widget.resize(600, 200)
widget.show()
sys.exit(app.exec())
How can I make it so when the "return" or "right arrow key" is pressed, focus moves from wherever it is currently to the first item in listwidget? This should also work while being focused inside of textbox, without triggering a newline.
Note: items get dynamically added to listwidget.
A possible solution could be to use QShorcut but because the OP requires "without triggering a newline". So in this case the solution is to implement an eventfilter to the QWindow:
import sys
from PyQt6 import QtCore, QtGui, QtWidgets
class KeyHelper(QtCore.QObject):
pressed = QtCore.pyqtSignal()
def __init__(self, window):
super().__init__(window)
self._window = window
self.window.installEventFilter(self)
#property
def window(self):
return self._window
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.Type.KeyPress:
if event.key() in (
QtCore.Qt.Key.Key_Return,
QtCore.Qt.Key.Key_Enter,
QtCore.Qt.Key.Key_Right,
):
self.pressed.emit()
return True
return super().eventFilter(obj, event)
class Launchiiwidget(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.textbox = QtWidgets.QTextEdit()
self.textbox.setLineWrapMode(QtWidgets.QTextEdit.LineWrapMode.NoWrap)
self.textbox.setAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.textbox.setFixedSize(QtCore.QSize(600, 100))
self.textbox.setVerticalScrollBarPolicy(
QtCore.Qt.ScrollBarPolicy.ScrollBarAlwaysOff
)
self.textbox.setHorizontalScrollBarPolicy(
QtCore.Qt.ScrollBarPolicy.ScrollBarAlwaysOff
)
font = self.textbox.font()
font.setPointSize(80)
self.textbox.setFont(font)
self.listwidget = QtWidgets.QListWidget()
self.listwidget.addItem("Red")
self.listwidget.addItem("Blue")
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.textbox)
layout.addWidget(self.listwidget)
def update_focus(self):
self.listwidget.setFocus()
index = self.listwidget.model().index(0, 0)
if index.isValid():
self.listwidget.setCurrentIndex(index)
if __name__ == "__main__":
app = QtWidgets.QApplication([])
widget = Launchiiwidget()
widget.setWindowFlags(QtCore.Qt.WindowType.FramelessWindowHint)
widget.resize(600, 200)
widget.show()
key_helper = KeyHelper(widget.windowHandle())
key_helper.pressed.connect(widget.update_focus)
sys.exit(app.exec())

How to update QWidget's content?

There is a QWidget, which is called with the button "Start" in the main widget.
It shows some text, which should be updated every minute during the work of the QWidget.
How can I make this infinity updating within this code?
class ExampleWidget(QWidget):
def __init__(self, parent=None):
super().__init__()
self.setWindowTitle('Example Widget ScrollArea')
self.initUi()
def initUi(self):
area = QScrollArea(self)
area.setWidgetResizable(True)
self.scrollAreaWidgetContents = QLabel(some_text, self)
area.setWidget(self.scrollAreaWidgetContents)
button = QPushButton("Close")
button.clicked.connect(self.goMainWindow)
layoutV = QVBoxLayout()
layoutV.addWidget(area)
layoutV.addWidget(button)
self.setLayout(layoutV)
def goMainWindow(self):
self.hide()
def sizeHint(self):
return QSize(400, 200)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
start_main_button = QPushButton('Start', self)
start_main_button.move(40, 40)
start_main_button.clicked.connect(self.start)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Test')
def start(self):
global some_text
some_text = 'some text'
self.result_widget = ExampleWidget()
self.result_widget.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())
One of my tries:
def start(self):
global some_text
some_text = 'some text'
self.result_widget = ExampleWidget()
self.result_widget.show()
i = 0
while True:
i+=1
some_text+=str(i)
self.result_widget = ExampleWidget()
self.result_widget.show()
Forget the global variables because they are considered bad practice, on the other hand in a GUI you should avoid having loops that consume a lot of time, and in your case a while True blocks the GUI. In Qt if you want to do periodic tasks you must use a QTimer:
from PyQt5 import QtCore, QtWidgets
class ExampleWidget(QtWidgets.QWidget):
def __init__(self, parent=None):
super().__init__()
self.setWindowTitle('Example Widget ScrollArea')
self.initUi()
def initUi(self):
area = QtWidgets.QScrollArea()
area.setWidgetResizable(True)
self.scrollAreaWidgetContents = QtWidgets.QLabel("some_text")
area.setWidget(self.scrollAreaWidgetContents)
button = QtWidgets.QPushButton("Close")
button.clicked.connect(self.hide)
layoutV = QtWidgets.QVBoxLayout(self)
layoutV.addWidget(area)
layoutV.addWidget(button)
def update_text(self, text):
# update the text
self.scrollAreaWidgetContents.setText(text)
def sizeHint(self):
return QtCore.QSize(400, 200)
class MainWindow(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
start_main_button = QtWidgets.QPushButton('Start', self)
start_main_button.move(40, 40)
start_main_button.clicked.connect(self.start)
self.setGeometry(300, 300, 290, 150)
self.setWindowTitle('Test')
self.result_widget = ExampleWidget()
self.timer = QtCore.QTimer(self, interval=60*1000)
self.timer.timeout.connect(self.on_timeout)
self.counter = 0
self.initial_text = "some_text"
def on_timeout(self):
# this method will be called every 60 * 1000 ms
self.initial_text += str(self.counter)
self.result_widget.update_text(self.initial_text)
self.counter += 1
#QtCore.pyqtSlot()
def start(self):
self.result_widget.show()
# start timer
self.timer.start()
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())

How do I add table to central widget in PyQt5 keeping my menu bar?

I am having trouble creating a PyQt5 GUI where I want a table of information as the central widget and a menu bar (which I will eventually put sorting options into). My code allows me to have either the table OR the menu but not both, and can't set a central widget because the class is not defined. Hopefully it's just something small I'm missing. Would appreciate any help, thanks.
Here is my code:
class Main(QMainWindow):
def __init__(self,parent = None):
super().__init__()
#self.grid_widget = App(grid)
self.initUI()
def initUI(self):
exitAct = QAction(QIcon('exit.png'), '&Exit', self)
exitAct.setShortcut('Ctrl+Q')
exitAct.setStatusTip('Exit application')
exitAct.triggered.connect(qApp.quit)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAct)
#self.setCentralWidget(self.grid_widget)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
sys.exit(app.exec_())
class App(QWidget):
def __init__(self, parent = None):
super(App, self).__init__(parent)
self.title = 'PyQt5 table'
self.left = 90
self.top = 90
self.width = 800
self.height = 600
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.createTable()
# Add layout, add table to grid layout
grid = QGridLayout()
grid.addWidget(self.tableWidget)
self.setLayout(grid)
# Show widget
self.show()
def createTable(self):
# Create table
self.tableWidget = QTableWidget()
self.tableWidget.setRowCount(len(q1.index))
self.tableWidget.setColumnCount(len(q1.columns))
self.tableWidget.setHorizontalHeaderLabels(heads) #set column names to headers in df
for i in range(len(q1.index)):
for j in range(len(q1.columns)):
self.tableWidget.setItem(i, j, QTableWidgetItem(str(q1.iat[i, j])))
self.tableWidget.resizeColumnsToContents()
self.tableWidget.resizeRowsToContents()
self.tableWidget.move(0,0)
# table selection change
self.tableWidget.doubleClicked.connect(self.on_click)
#pyqtSlot()
def on_click(self):
print("\n")
for currentQTableWidgetItem in self.tableWidget.selectedItems():
print(currentQTableWidgetItem.row(), currentQTableWidgetItem.column(), currentQTableWidgetItem.text())
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
Which produces this:
and ignores my whole tablewidget altogether.
I have commented out the 3rd line in the QMainWindow because it produces an error saying that App is not defined.
How do I set the central widget so that it allows the table to appear as well? Thanks.
the application should only have a single main, to have several only the first one will be executed, and therefore everything that you are after will not be defined, and consequently App is not defined as you indicate.
So the solution is to eliminate the intermediate main and copy it to the final main.
class Main(QMainWindow):
def __init__(self,parent = None):
super().__init__()
self.grid_widget = App(self)
self.initUI()
...
class App(QWidget):
...
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Main()
sys.exit(app.exec_())

PySide switching widgets with events?

I can't figure it. I want to be able to swap out QWidgets according to events like button clicks but I am missing something and I haven't been able to search out any example along the lines of the code below. What I want to do is click one of the top buttons and get the widget below to switch between either the QCalendar or QtextEdit. Where am I going wrong?
Thanks!
#!/usr/bin/python
import sys
from PySide import QtGui, QtCore
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
widget = QtGui.QCalendarWidget()
button1 = QtGui.QPushButton('Calendar', self)
button1.setCheckable(True)
button1.clicked[bool].connect(self.setWidget)
button2 = QtGui.QPushButton('TextEdit', self)
button2.setCheckable(True)
button2.clicked[bool].connect(self.setWidget)
splitter1 = QtGui.QSplitter(QtCore.Qt.Vertical)
splitter1.addWidget(button1)
splitter1.addWidget(button2)
splitter1.addWidget(widget)
hbox.addWidget(splitter1)
self.setLayout(hbox)
self.setGeometry(0, 0, 600, 600)
self.setWindowTitle('Switching QWidgets')
self.show()
def setWidget(self, pressed):
source = self.sender()
val1 = QtGui.QCalendarWidget()
val2 = QtGui.QTextEdit()
if source.text() == "Calendar":
widget = val1
QtGui.QWidget.update(Example.hbox)
elif source.text() == "TextEdit":
widget = val2
QtGui.QWidget.update(Example.hbox)
else:
widget = val1
QtGui.QWidget.update(Example.hbox)
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
You have to use QStackedWidget, where you update the indexes.
class Example(QtGui.QWidget):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.initUI()
def initUI(self):
hbox = QtGui.QHBoxLayout(self)
self.stacked = QtGui.QStackedWidget(self)
self.stacked.addWidget(QtGui.QCalendarWidget())
self.stacked.addWidget(QtGui.QTextEdit())
splitter1 = QtGui.QSplitter(QtCore.Qt.Vertical)
for text in ["Calendar", "TextEdit"]:
btn = QtGui.QPushButton(text, self)
btn.clicked.connect(self.setWidget)
splitter1.addWidget(btn)
splitter1.addWidget(self.stacked)
hbox.addWidget(splitter1)
self.setLayout(hbox)
self.setGeometry(0, 0, 600, 600)
self.setWindowTitle('Switching QWidgets')
self.show()
def setWidget(self):
source = self.sender()
if source.text() == "Calendar":
self.stacked.setCurrentIndex(0)
elif source.text() == "TextEdit":
self.stacked.setCurrentIndex(1)
If you change your code to keep a reference to the calendar and textedit widgets, then add them both to the splitter in init... You can just use the show and hide functions in your setWidget().
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
self.hbox = QtGui.QHBoxLayout(self)
self.widget = QtGui.QCalendarWidget()
self.widget2 = QtGui.QTextEdit()
button1 = QtGui.QPushButton('Calendar', self)
button1.setCheckable(True)
button1.clicked[bool].connect(self.setWidget)
button2 = QtGui.QPushButton('TextEdit', self)
button2.setCheckable(True)
button2.clicked[bool].connect(self.setWidget)
splitter1 = QtGui.QSplitter(QtCore.Qt.Vertical)
splitter1.addWidget(button1)
splitter1.addWidget(button2)
splitter1.addWidget(self.widget)
splitter1.addWidget(self.widget2)
self.widget2.hide()
self.hbox.addWidget(splitter1)
self.setLayout(self.hbox)
self.setGeometry(59, 59, 600, 600)
self.setWindowTitle('Switching QWidgets')
self.show()
def setWidget(self, pressed):
source = self.sender()
if source.text() == "Calendar":
self.widget.show()
self.widget2.hide()
elif source.text() == "TextEdit":
self.widget.hide()
self.widget2.show()

PyQt application close with error

I have some trouble with creating window application using PyQt. For example: we have the following class for GUI:
class SampleMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
...
def hideEvent(self, event):
...
def showEvent(self, event):
...
def activate(self):
...
def closeEvent(self, event):
...
What if we want to use it just for showing results and acept user inputs, and for all control logic we have another class, which use the instance of SampleMainWindow as a field:
class Programm:
def __init__(self):
self.window = SampleMainWindow()
....
app = QtGui.QApplication(sys.argv)
prog = Programm()
prog.window.show()
app.exec_()
So, if we create the instance of Programm, the application will run well, but on close we'll get an error that python.exe process could not be completed normally. But if the instance of SampleMainWindow is not a field of class:
class Programm:
def __init__(self):
win = SampleMainWindow()
....
application close well. What's the reason?
Here are the full version of Gui class:
class SampleMainWindow(QtGui.QMainWindow):
def initTray(self):
self.icon = QtGui.QSystemTrayIcon()
self.icon.setIcon(QtGui.QIcon('Tray.png'))
self.icon.show()
self.icon.activated.connect(self.activate)
def __init__(self, parent=None):
QtGui.QMainWindow.__init__(self)
moduleglobalconstants.APP_RUNNING = True
self.initTray()
self.newSession = True
self.setGeometry(300, 300, 600, 400)
self.setWindowTitle('Eye: Recently Added Lines')
self.statusBar().showMessage('Ready')
exitAction = QtGui.QAction(QtGui.QIcon('Exit.png')
,'Exit'
,self)
exitAction.setShortcut('Ctrl+Q')
exitAction.setStatusTip('Exit application')
self.connect(exitAction
,QtCore.SIGNAL('triggered()')
,QtCore.SLOT('close()'))
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(exitAction)
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(exitAction)
self.textEdit = QtGui.QTextEdit()
self.scrollPosition = self.textEdit.textCursor()
self.textEdit.setReadOnly(True)
self.setCentralWidget(self.textEdit)
def hideEvent(self, event):
self.hidden = True
self.setWindowFlags(QtCore.Qt.ToolTip)
self.setVisible(False)
def showEvent(self, event):
self.hidden = False
self.setVisible(True)
def activate(self):
self.setWindowFlags(QtCore.Qt.Window)
self.show()
self.setWindowState(self.windowState() & ~QtCore.Qt.WindowMinimized | QtCore.Qt.WindowActive)
self.activateWindow()
def questionDialog(self, caption = 'Message',text = "Are you sure?"):
self.activate()
msgBox = QtGui.QMessageBox(self)
msgBox.setWindowTitle("Eye")
msgBox.setText(caption);
msgBox.setInformativeText(text);
msgBox.setStandardButtons(QtGui.QMessageBox.Yes| QtGui.QMessageBox.No);
msgBox.setDefaultButton(QtGui.QMessageBox.Yes);
return msgBox.exec_()
def criticalDialog(self,app, caption = 'Eye: Critical Error',text = "Impossible to continue working!"):
self.activate()
msgBox = QtGui.QMessageBox.critical(self,caption,text)
moduleglobalconstants.APP_RUNNING = False
def closeEvent(self, event):
reply = self.questionDialog()
if reply == QtGui.QMessageBox.Yes:
moduleglobalconstants.APP_RUNNING = False
event.accept()
else:
event.ignore()
def showNewLines(self, singleLine = None, lines = None, mode='insert'):
if (mode == 'rewrite'):
self.textEdit.clear()
if lines:
for line in lines:
self.textEdit.insertPlainText(line.strip() + "\n")
elif singleLine:
self.textEdit.insertPlainText(singleLine.strip()+"\n")
This has to do with python's garbage collecting - as (almost) soon an object isn't referenced anymore it destroys it, so I guess the process doesn't terminate as long as the window wasn't garbage-collected.
You can try to add at the end del Programm.window or something like that (there's probably a better way of doing this, but I never programmed QT myself so I can't help you here)
see this:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys
class MyWindow(QWidget):
def __init__(self, parent=None):
QWidget.__init__(self, parent)
self.label = QLabel("Hi, I'm a window", self)
self.button = QPushButton("&Quit", self)
self.connect(self.button, SIGNAL("clicked()"), QCoreApplication.instance(), SLOT("quit()"))
lay = QVBoxLayout()
lay.addWidget(self.label)
lay.addWidget(self.button)
self.setLayout(lay)
def closeEvent(self, event):
print "Closing the app"
self.deleteLater()
if __name__ == '__main__':
app = QApplication(sys.argv)
mw = MyWindow()
mw.show()
sys.exit(app.exec_())
the solution is to implement closeEvent method

Categories