PyQt GUI Window NOT showing on VirtualBox Linux Mint - qt

I've installed PyQt4 on my Linux Cinnamon Mint on my VirtualBox Machine (using : sudo apt-get install python-qt4) and tried running this code :
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("PyQT Tuts!")
window.show()
It compiles without errors, but I do not see any Window for the show method.
I'm new in this so simple instructions will be appreciated.
Thanks for the read.

In Linux you have to include sys.exit(app.exec_()) below your window.show()
Example:
import sys
from PyQt4 import QtGui
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("whatever")
window.show()
sys.exit(app.exec_())
That's the only problem i can see.

try this
import sys
from PyQt4 import QtGui
def main():
app = QtGui.QApplication(sys.argv)
window = QtGui.QWidget()
window.setGeometry(0, 0, 500, 300)
window.setWindowTitle("PyQT Tuts!")
window.show()
if __name__ == '__main__':
main()

Related

how to set the QDockWidget Group color?

The picture bellow is a pyside6 application's capture, I move all QDockWidget into a single group, but the group's header's background has two different color. How to change them into a single color(qss or code)? Thanks very much!
Environment:
macos 11.6.5
python 3.9.12
pyside6 6.3.1
Reproduction Code:
# coding: utf-8
import sys
import platform
from PySide6.QtWidgets import QApplication, QMainWindow, QDockWidget, QTextEdit
from PySide6.QtCore import Qt, QSysInfo
def main():
app: QApplication = QApplication(sys.argv)
window = QMainWindow()
dock1 = QDockWidget("dock1")
dock2 = QDockWidget("dock2")
for dock in [dock1, dock2]:
dock.setFeatures(dock.DockWidgetFloatable | dock.DockWidgetMovable)
window.addDockWidget(Qt.LeftDockWidgetArea, dock1)
window.addDockWidget(Qt.RightDockWidgetArea, dock2)
os_info = QTextEdit()
os_info.setText(platform.version())
dock1.setWidget(os_info)
qt_info = QTextEdit()
info = QSysInfo()
qt_info.setText(f"{info.kernelVersion()}, {info.prettyProductName()}, {info.productVersion()}")
dock2.setWidget(qt_info)
window.show()
app.exec()
if __name__ == '__main__':
main()
I have solve it with the signal method myself:
# coding: utf-8
import sys
import platform
from PySide6.QtWidgets import QApplication, QMainWindow, QDockWidget, QTextEdit, QTabBar
from PySide6.QtCore import Qt, QSysInfo
def main():
app: QApplication = QApplication(sys.argv)
window = QMainWindow()
dock1 = QDockWidget("dock1")
dock2 = QDockWidget("dock2")
for dock in [dock1, dock2]:
dock.setFeatures(dock.DockWidgetFloatable | dock.DockWidgetMovable)
window.addDockWidget(Qt.LeftDockWidgetArea, dock1)
window.addDockWidget(Qt.RightDockWidgetArea, dock2)
os_info = QTextEdit()
os_info.setText(platform.version())
dock1.setWidget(os_info)
qt_info = QTextEdit()
info = QSysInfo()
qt_info.setText(f"{info.kernelVersion()}, {info.prettyProductName()}, {info.productVersion()}")
dock2.setWidget(qt_info)
style = """
QTabBar {
background-color: #ff0000;
}
"""
app.setStyleSheet(style)
# force update theme on dock change
for w in window.children():
if isinstance(w, QDockWidget):
w.dockLocationChanged.connect(lambda: app.setStyleSheet(style))
window.show()
app.exec()
if __name__ == '__main__':
main()

PyQt 5.6.2 / 5.9.1 on Windows 10 - WindowStaysOnTopHint doesn't work

PyQt 5.6.2, 5.9.1, Anaconda, vscode, Windows 10 x64.
Setting QtCore.Qt.WindowStaysOnTopHint in super().__init__() or via self.setWindowFlags() does nothing. With Qt 4.8.7 (PyQt 4.11.4) the flag works as expected.
I can fix this using Windows API (HWND_TOPMOST flag):
import sys
try:
from PyQt5 import QtCore, QtWidgets
except ImportError:
from PyQt4 import QtCore, QtGui as QtWidgets
class Form1(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.pushButton1 = QtWidgets.QPushButton("Push", self)
self.pushButton1.clicked.connect(self.clicked)
self.ontop = False
def clicked(self):
self.ontop = not self.ontop
self.windowStaysOnTopHint(self.ontop)
def windowStaysOnTopHint(self, b=True):
try:
import win32gui, win32con
flag = win32con.HWND_TOPMOST if b else win32con.HWND_NOTOPMOST
win32gui.SetWindowPos(self.winId(), flag, 0, 0, 0, 0, win32con.SWP_NOSIZE | win32con.SWP_NOMOVE)
except ModuleNotFoundError:
pass
if b:
flag = self.windowFlags() | QtCore.Qt.WindowStaysOnTopHint
else:
flag = self.windowFlags() & ~QtCore.Qt.WindowStaysOnTopHint
self.setGeometry(self.geometry()) # `setWindowFlags` resets size if setGeometry is never called
self.setWindowFlags(flag)
self.show()
print(QtCore.QT_VERSION_STR)
app = QtWidgets.QApplication(sys.argv)
form1 = Form1()
form1.show()
app.exec_()
I can use win32gui.SetWindowPos and remove setWindowFlags block at all. But I really want to know what's the reason of this behaviour.

Pyside: QNetworkAccessManager sends no Request

I tried to establish a network connection with PySide (Ubuntu 15.04, Python3.4, PySide 1.2.4). I used the example code from the documentation.
The QNetworkAccessManager does not send the request and I recieve no answer. I checked the Network state with QNetworkSession(QNetworkConfigurationManager().defaultConfiguration()).State() but it says the State is invalid. This seems to make no sense since I am on a desktop pc with a network connection via ethernet cable.
My complete example for the test is the following code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PySide.QtGui import QApplication
from PySide.QtCore import QUrl
from PySide.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkSession, QNetworkConfigurationManager
def replyFinished(reply):
print(reply)
if __name__ == "__main__":
app = QApplication(sys.argv)
manager = QNetworkAccessManager()
manager.finished.connect(replyFinished)
print(QNetworkSession(QNetworkConfigurationManager().defaultConfiguration()).State())
print("Sending request")
print(manager.get(QNetworkRequest(QUrl("http://www.heise.de/ct/"))))
This prints
PySide.QtNetwork.QNetworkSession.State.Invalid
Sending request
<PySide.QtNetwork.QNetworkReply object at 0x7f4b59c9af08>
but it should display the PySide.QtNetwork.QNetworkReply object twice.
My example was too small. I realized this because of the comment of Pavel Strakhov. I extended it to display a window with a button. By clicking the button it connects successfully. QNetworkSession(QNetworkConfigurationManager().defaultConfiguration()).State() is still invalid but it works.
This is the working code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
from PySide.QtGui import QApplication, QWidget, QBoxLayout, QPushButton
from PySide.QtCore import QUrl
from PySide.QtNetwork import QNetworkAccessManager, QNetworkRequest, QNetworkSession, QNetworkConfigurationManager
class Window(QWidget):
def __init__(self):
super().__init__()
self.manager = QNetworkAccessManager()
self.manager.finished.connect(self.reply_finished)
layout = QBoxLayout(QBoxLayout.TopToBottom)
button = QPushButton("connect")
button.clicked.connect(self.network_connect)
layout.addWidget(button)
self.setLayout(layout)
self.setWindowTitle("Network test")
self.setGeometry(100, 100, 200, 150)
self.show()
def network_connect(self):
print(QNetworkSession(QNetworkConfigurationManager().defaultConfiguration()).State())
request = QNetworkRequest(QUrl("http://example.org"))
print("Sending request")
self.manager.get(request)
def reply_finished(self, reply):
print(reply)
print(reply.readData(500))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Window()
app.exec_()

PyQT5 menu not visible

When executing this little PyQT5 script, I can't see the menu; it just displays an empty window (no errors or warnings) on ubuntu 14.04.
from PyQt5 import QtWidgets
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow,self).__init__()
self.createUI()
def doAction(self):
print('action')
def createUI(self):
self.setWindowTitle('Test')
menu = self.menuBar().addMenu('File')
action = menu.addAction('Action')
action.triggered.connect(self.doAction)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
window = MainWindow()
window.show()
window.setGeometry(400, 200, 200, 200)
sys.exit(app.exec_())
Any ideas?
I had the same problem
Try to set native menu bar flag as false like this:
menu.setNativeMenuBar(False)

Animating a window on startup

I am trying to animate the window in startup but it doesn't seem to work, I have written the code below.
from PyQt4 import QtCore,QtGui
import sys
class AnimatedWindow(QtGui.QMainWindow):
"""docstring for AnimatedWindow"""
def __init__(self):
super(AnimatedWindow, self).__init__()
animation = QtCore.QPropertyAnimation(self, "geometry")
animation.setDuration(10000);
animation.setStartValue(QtCore.QRect(0, 0, 100, 30));
animation.setEndValue(QtCore.QRect(250, 250, 100, 30));
animation.start();
if __name__ == "__main__":
application = QtGui.QApplication(sys.argv)
main = AnimatedWindow()
main.show()
sys.exit(application.exec_())
The problem with this code is, when you create an object of QPropertyAnimation it is destroyed by the python garbage collector after animation.start() statement as animation variable is a local variable, hence the animation does not take place. To overcome this problem you need to make the animation as member variable (self.animation)
Here is the updated code which works fine:
from PyQt4 import QtCore,QtGui
import sys
class AnimatedWindow(QtGui.QMainWindow):
"""docstring for AnimatedWindow"""
def __init__(self):
super(AnimatedWindow, self).__init__()
self.animation = QtCore.QPropertyAnimation(self, "geometry")
self.animation.setDuration(1000);
self.animation.setStartValue(QtCore.QRect(50, 50, 100, 30));
self.animation.setEndValue(QtCore.QRect(250, 250, 500, 530));
self.animation.start();
if __name__ == "__main__":
application = QtGui.QApplication(sys.argv)
main = AnimatedWindow()
main.show()
sys.exit(application.exec_())

Resources