VTK with Qt5 - Timer stops running when window is interacted with - python

I've been trying to do animation with VTK, so I've been using TimerEvent. When I tried to move over to the Qt binding, it broke. The problem is that as soon as I interact with the view (say scrolling to zoom, or clicking to rotate) the timer stops. Here's a simple minimal example:
import vtk
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt5 import Qt
message = "tick"
def onTimerEvent(object, event):
global message
print(message)
if message == "tick":
message = "tock"
else:
message = "tick"
app = Qt.QApplication([])
mainWindow = Qt.QMainWindow()
renderer = vtk.vtkRenderer()
vtkWidget = QVTKRenderWindowInteractor(mainWindow)
vtkWidget.GetRenderWindow().AddRenderer(renderer)
mainWindow.setCentralWidget(vtkWidget)
vtkWidget.GetRenderWindow().GetInteractor().Initialize()
timerId = vtkWidget.CreateRepeatingTimer(100)
vtkWidget.AddObserver("TimerEvent", onTimerEvent)
mainWindow.show()
app.exec_()
This script should display the words "tick" and "tock" over and over again, but stop as soon as you click inside the window.
One odd behavior is that pressing "T" to switch to trackball interaction style seems to have some effect. If I press T and then click inside the window, the timer only stops running while I'm clicking: when I let go it starts up again. If I then press J to go back to "joystick mode", the problem returns: clicking stops the timer forever.
Python 3.6, VTK 8, Qt 5.

The problem is reproducible in Linux 16.04, VTK8.1.1 and Qt5.5.1.
As you are using Qt, a workaround for your problem is to use QTimer(). It is a solution if you want to work with a timing.
This is your minimal example changing the TimerEvent for QTimer():
import vtk
from vtk.qt.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt5 import Qt
from PyQt5.QtCore import QTimer
message = "tick"
def onTimerEvent():
global message
print(message)
if message == "tick":
message = "tock"
else:
message = "tick"
app = Qt.QApplication([])
mainWindow = Qt.QMainWindow()
renderer = vtk.vtkRenderer()
vtkWidget = QVTKRenderWindowInteractor(mainWindow)
vtkWidget.GetRenderWindow().AddRenderer(renderer)
mainWindow.setCentralWidget(vtkWidget)
vtkWidget.GetRenderWindow().GetInteractor().Initialize()
#timerId = vtkWidget.CreateRepeatingTimer(100)
#vtkWidget.AddObserver("TimerEvent", onTimerEvent)
timer = QTimer()
timer.timeout.connect(onTimerEvent)
timer.start(100)
mainWindow.show()
app.exec_()

Related

Pyqt5 | How to properly combine multiple windows in one app

I have an application that consists of multiple uis(windows) and a main script, that should tie them all together into one program. Here is the video showing it: https://youtu.be/B4-PKmbyvjY.
The first problem is that when the active window changes, the application icon on the taskbar flashes. And it's very annoying. The second problem is that my computer is quite strong, and the delay between switching windows is not particularly noticeable. But when my friend started this program on his laptop, the delay between hiding one window and opening another was huge. For a whole second there was a desktop instead of the program. And so it is with every window switch.
Here's a main.py script:
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt, pyqtSlot
from downloadPage import DownloadPage
from mainPage import MainPage
from managerPage import ManagerPage
from registrationPage import RegistrationPage
from loginPage import LoginPage
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling, True)
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps, True)
def switch(_from, _to):
# Open new window on the position of the last opened window
# Otherwise it just appears in the center
lastPos = _from.pos()
modifiedPos = (lastPos.x() + _from.width() // 2,
lastPos.y() + _from.height() // 2)
newPos = (modifiedPos[0] - _to.width() // 2,
modifiedPos[1] - _from.height() // 2)
_to.move(*newPos)
_to.show()
_from.hide()
if __name__ == '__main__':
app = QApplication(sys.argv)
# Pages (uis)
registerWin = RegistrationPage()
mainWin = MainPage()
downloadWin = DownloadPage()
managerWin = ManagerPage()
loginWin = LoginPage()
mainWin.show()
# Switch windows on button clicks
loginWin.registerButton.clicked.connect(
lambda: switch(loginWin, registerWin))
registerWin.loginButton.clicked.connect(
lambda: switch(registerWin, loginWin))
mainWin.downloadButton.clicked.connect(
lambda: switch(mainWin, downloadWin))
mainWin.browseButton.clicked.connect(
lambda: switch(mainWin, managerWin))
downloadWin.returnButton.clicked.connect(
lambda: switch(downloadWin, mainWin))
# After register/login was successful
#pyqtSlot(str)
def successfulLogin(login):
mainWin.upperText.setText(
f'Welcome, {login}! What brings you here today?')
switch(registerWin, mainWin)
registerWin.successfulRegister.connect(successfulLogin)
loginWin.successfulLogin.connect(successfulLogin)
sys.exit(app.exec_())
I've heard about QTabWidget, but i'm not quite sure if it's the solution in my case. There should be buttons to switch between tabs and my program just can't allow that.
Thanks in advance for any help

How to figure out whether button is continuosly pressed or not in PyQt5?

For my application, I need my lift system to go up as long as the button is pressed and it should stop when I don't press the button.
clicked() function is not functional for this purpose. However pressed() and released() functions also didn't work.
I snipped related section of my code below. My aim is to print "Pressed" text as long as button is pressed
def __init__(self):
manual_button = QPushButton('Lift Button')
manual_button.pressed.connect(press_function)
self.manual_grid.addWidget(manual_button, 0, 1)
def press_function(self):
print('pressed')
Thanks
Just use the pressed and released signals to start/stop a QTimer. Something like...
#!/usr/local/bin/python3
import os
import sys
from PyQt5.QtCore import(QTimer)
from PyQt5.QtWidgets import(QApplication, QPushButton)
def button_pressed(timer):
timer.start(100)
def button_released(timer):
timer.stop()
if __name__ == '__main__':
app = QApplication(sys.argv)
pb = QPushButton("Press")
timer = QTimer()
pb.pressed.connect(lambda checked = False: button_pressed(timer))
pb.released.connect(lambda checked = False: button_released(timer))
timer.timeout.connect(lambda: print('Button Pressed'))
pb.show()
app.exec_()

How to attach and detach an external app with PyQT5 or dock an external application?

I'm developing an GUI for multi-robot system using ROS, but i'm freezing in the last thing i want in my interface: embedding the RVIZ, GMAPPING or another screen in my application. I already put an terminal in the interface, but i can't get around of how to add an external application window to my app. I know that PyQt5 have the createWindowContainer, with uses the window ID to dock an external application, but i didn't find any example to help me with that.
If possible, i would like to drag and drop an external window inside of a tabbed frame in my application. But, if this is not possible or is too hard, i'm good with only opening the window inside a tabbed frame after the click of a button.
I already tried to open the window similar to the terminal approach (see the code bellow), but the RVIZ window opens outside of my app.
Already tried to translate the attaching/detaching code code to linux using the wmctrl command, but didn't work wither. See my code here.
Also already tried the rviz Python Tutorial but i'm receveing the error:
Traceback (most recent call last):
File "rvizTutorial.py", line 23, in
import rviz
File "/opt/ros/indigo/lib/python2.7/dist-packages/rviz/init.py", line 19, in
import librviz_shiboken
ImportError: No module named librviz_shiboken
# Frame where i want to open the external Window embedded
self.Simulation = QtWidgets.QTabWidget(self.Base)
self.Simulation.setGeometry(QtCore.QRect(121, 95, 940, 367))
self.Simulation.setTabPosition(QtWidgets.QTabWidget.North)
self.Simulation.setObjectName("Simulation")
self.SimulationFrame = QtWidgets.QWidget()
self.SimulationFrame.setObjectName("SimulationFrame")
self.Simulation.addTab(rviz(), "rViz")
# Simulation Approach like Terminal
class rviz(QtWidgets.QWidget):
def __init__(self, parent=None):
super(rviz, self).__init__(parent)
self.process = QtCore.QProcess(self)
self.rvizProcess = QtWidgets.QWidget(self)
layout = QtWidgets.QVBoxLayout(self)
layout.addWidget(self.rvizProcess)
# Works also with urxvt:
self.process.start('rViz', [str(int(self.winId()))])
self.setGeometry(121, 95, 940, 367)
I've not tested this specifically, as I've an old version of Qt5 I can't upgrade right now, while from Qt5 5.10 startDetached also returns the pid along with the bool result from the started process.
In my tests I manually set the procId (through a static QInputBox.getInt()) before starting the while cycle that waits for the window to be created.
Obviously there are other ways to do this (and to get the xid of the window).
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import gi
gi.require_version('Wnck', '3.0')
from gi.repository import Wnck, Gdk
class Container(QtWidgets.QTabWidget):
def __init__(self):
QtWidgets.QTabWidget.__init__(self)
self.embed('xterm')
def embed(self, command, *args):
proc = QtCore.QProcess()
proc.setProgram(command)
proc.setArguments(args)
started, procId = proc.startDetached()
if not started:
QtWidgets.QMessageBox.critical(self, 'Command "{}" not started!')
return
attempts = 0
while attempts < 10:
screen = Wnck.Screen.get_default()
screen.force_update()
# this is required to ensure that newly mapped window get listed.
while Gdk.events_pending():
Gdk.event_get()
for w in screen.get_windows():
if w.get_pid() == procId:
window = QtGui.QWindow.fromWinId(w.get_xid())
container = QtWidgets.QWidget.createWindowContainer(window, self)
self.addTab(container, command)
return
attempts += 1
QtWidgets.QMessageBox.critical(self, 'Window not found', 'Process started but window not found')
app = QtWidgets.QApplication(sys.argv)
w = Container()
w.show()
sys.exit(app.exec_())
I couldn't get the code in the accepted answer to work on Ubuntu 18.04.3 LTS; even when I got rid of the exceptions preventing the code to run, I'd still get a separate PyQt5 window, and separate xterm window.
Finally after some tries, I got the xterm window to open inside the tab; here is my code working in Ubuntu 18.04.3 LTS (with all the misses commented):
#!/usr/bin/env python3
# (same code seems to run both with python3 and python2 with PyQt5 in Ubuntu 18.04.3 LTS)
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import gi
gi.require_version('Wnck', '3.0')
from gi.repository import Wnck, Gdk
import time
class Container(QtWidgets.QTabWidget):
def __init__(self):
QtWidgets.QTabWidget.__init__(self)
self.embed('xterm')
def embed(self, command, *args):
proc = QtCore.QProcess()
proc.setProgram(command)
proc.setArguments(args)
#started, procId = proc.startDetached()
#pid = None
#started = proc.startDetached(pid)
# https://stackoverflow.com/q/31519215 : "overload" startDetached : give three arguments, get a tuple(boolean,PID)
# NB: we will get a failure `xterm: No absolute path found for shell: .` even if we give it an empty string as second argument; must be a proper abs path to a shell
started, procId = proc.startDetached(command, ["/bin/bash"], ".")
if not started:
QtWidgets.QMessageBox.critical(self, 'Command "{}" not started!'.format(command), "Eh")
return
attempts = 0
while attempts < 10:
screen = Wnck.Screen.get_default()
screen.force_update()
# do a bit of sleep, else window is not really found
time.sleep(0.1)
# this is required to ensure that newly mapped window get listed.
while Gdk.events_pending():
Gdk.event_get()
for w in screen.get_windows():
print(attempts, w.get_pid(), procId, w.get_pid() == procId)
if w.get_pid() == procId:
self.window = QtGui.QWindow.fromWinId(w.get_xid())
#container = QtWidgets.QWidget.createWindowContainer(window, self)
proc.setParent(self)
#self.scrollarea = QtWidgets.QScrollArea()
#self.container = QtWidgets.QWidget.createWindowContainer(self.window)
# via https://vimsky.com/zh-tw/examples/detail/python-method-PyQt5.QtCore.QProcess.html
#pid = proc.pid()
#win32w = QtGui.QWindow.fromWinId(pid) # nope, broken window
win32w = QtGui.QWindow.fromWinId(w.get_xid()) # this finally works
win32w.setFlags(QtCore.Qt.FramelessWindowHint)
widg = QtWidgets.QWidget.createWindowContainer(win32w)
#self.container.layout = QtWidgets.QVBoxLayout(self)
#self.addTab(self.container, command)
self.addTab(widg, command)
#self.scrollarea.setWidget(self.container)
#self.container.setParent(self.scrollarea)
#self.scrollarea.setWidgetResizable(True)
#self.scrollarea.setFixedHeight(400)
#self.addTab(self.scrollarea, command)
self.resize(500, 400) # set initial size of window
return
attempts += 1
QtWidgets.QMessageBox.critical(self, 'Window not found', 'Process started but window not found')
app = QtWidgets.QApplication(sys.argv)
w = Container()
w.show()
sys.exit(app.exec_())

QMessageBox add custom button and keep open

I want to add a custom button to QMessagebox that opens up a matplotlib window, along with an Ok button for user to click when they want to close it
I currently have it somewhat working, but I want the two buttons to do separate things and not open the window.
I know I can just create a dialog window with the desired results, but I wanted to know how to with a QMessageBox.
import sys
from PyQt5 import QtCore, QtWidgets
def main():
app = QtWidgets.QApplication(sys.argv)
msgbox = QtWidgets.QMessageBox()
msgbox.setWindowTitle("Information")
msgbox.setText('Test')
msgbox.addButton(QtWidgets.QMessageBox.Ok)
msgbox.addButton('View Graphs', QtWidgets.QMessageBox.YesRole)
bttn = msgbox.exec_()
if bttn:
print("Ok")
else:
print("View Graphs")
sys.exit(app.exec_())
if __name__ == "__main__":
main()
Desired result:
Ok button - closes QMessageBox
View Graph button - opens matplotlib window and keeps QMessageBox open until user clicks Ok
A bit hacky IMO, but after you add the View Graphs button you could disconnect its clicked signal and reconnect it to your slot of choice, e.g.
import sys
from PyQt5 import QtCore, QtWidgets
def show_graph():
print('Show Graph')
def main():
app = QtWidgets.QApplication(sys.argv)
msgbox = QtWidgets.QMessageBox()
msgbox.setWindowTitle("Information")
msgbox.setText('Test')
msgbox.addButton(QtWidgets.QMessageBox.Ok)
yes_button = msgbox.addButton('View Graphs', QtWidgets.QMessageBox.YesRole)
yes_button.clicked.disconnect()
yes_button.clicked.connect(show_graph)
bttn = msgbox.exec_()
if bttn:
print("Ok")
sys.exit(app.exec_())
if __name__ == "__main__":
main()
A QMessageBox, as all QDialogs, blocks everything until exec_() is returned, but it also automatically connects all buttons to either accepted/rejected signals, returning the exec_() in any case.
A possible solution for your code is:
app = QtWidgets.QApplication(sys.argv)
msgbox = QtWidgets.QMessageBox()
# the following is if you need to interact with the other window
msgbox.setWindowModality(QtCore.Qt.NonModal)
msgbox.addButton(msgbox.Ok)
viewGraphButton = msgbox.addButton('View Graphs', msgbox.ActionRole)
# disconnect the clicked signal from the slots QMessageBox automatically sets
viewGraphButton.clicked.disconnect()
# this is just an example, replace with your matplotlib widget/window
graphWidget = QtWidgets.QWidget()
viewGraphButton.clicked.connect(graphWidget.show)
msgbox.button(msgbox.Ok).clicked.connect(graphWidget.close)
# do not use msgbox.exec_() or it will reset the window modality
msgbox.show()
sys.exit(app.exec_())
That said, be careful in using QDialog.exec_() outside (as in "before") the sys.exit(app.exec_()) call, as it might result in unexpected behavior if you don't know what you are doing.
Okay well first you do not use anything in QtCore so no need to import that. This should help you understand what you need to do. I tweaked it a smidge and I had to add the 2 sys.exits or when you pressed View Graphs the program just hung due to how you have this currently set up. If you do not choose to adjust this code flow then pull the sys.exit out of the if/else and put it right after the if/else -- no sense having unnecessary redundant code
from sys import exit as sysExit
from PyQt5.QtWidgets import QApplication, QMessageBox
def Main():
msgbox = QMessageBox()
msgbox.setWindowTitle("Information")
msgbox.setText('Test')
msgbox.addButton(QMessageBox.Ok)
msgbox.addButton('View Graphs', QMessageBox.YesRole)
bttn = msgbox.exec_()
if bttn == QMessageBox.Ok:
print("Ok")
sysExit()
else:
print("View Graphs")
sysExit()
if __name__ == "__main__":
MainThred = QApplication([])
MainApp = Main()
sysExit(MainThred.exec_())
Aka your non-redundant if/else would look as follows
if bttn == QMessageBox.Ok:
print("Ok")
else:
print("View Graphs")
sysExit()

PyQt4: Only the last signal is being processed

I ran into a strange problem when working on my project. I have a GUI and a QTextEdit that serves as a status browser. When a button is clicked, I want the QTextEdit to display a 10 second countdown while another function is happening in a separate thread. Even though I emit a signal every second, the QTextEdit hangs for 9 seconds, then displays the last countdown number.
I thought this might have something to do with stuff happening in a separate thread, so I created a separate example to test this out. In my simple example, there are two things: a QTextEdit and a Button. When the button is clicked, the status browser should display '5' for two seconds, then '4'.
Here is the code:
import sys
from PyQt4 import QtGui, uic
from PyQt4.QtCore import QObject, pyqtSignal
import time
class MainUI(QObject):
status_signal = pyqtSignal(str)
def __init__(self, window):
super(QObject, self).__init__()
self.ui = uic.loadUi(r'L:\internal\684.07\Mass_Project\Software\PythonProjects\measure\testing\status_test.ui', window)
self.ui.statusBrowser.setReadOnly(True)
self.ui.statusBrowser.setFontPointSize(20)
self.status_signal.connect(self.status_slot)
self.ui.button.clicked.connect(self.counter)
window.show()
def status_slot(self, message):
self.ui.statusBrowser.clear()
self.ui.statusBrowser.append(message)
def counter(self):
print 'clicked'
i = 5
self.status_signal.emit(str(i))
time.sleep(2)
self.status_signal.emit(str(i-1))
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
app.setStyle("cleanlooks")
main_window = QtGui.QDialog()
main_ui = MainUI(main_window)
sys.exit(app.exec_())
In this example, the same thing happens. The status browser hangs for 2 seconds, then only displays '4'. When I alter the status_slot function so that it doesn't clear the status browser before appending to it, the status browser waits for 2 seconds, then emits both signals at once, displaying '5 \n 4'. Does anyone know why this is happening and what I can do to constantly update the display? Thanks in advance!
time.sleep() blocks the Qt main loop so it can't process window redraw events. Use a QTimer to periodically call a method which emits your signal so that control is returned to the Qt event loop regularly.

Categories