Pyside2 with Qt Designer - Resource(icons) ignored when using QUiLoader() - python

In Qt Designer, I defined a couple of icons in the resource browser and I attached them to buttons and actions. The Designer preview shows the icons. Icons are stored in icons.qrc file.
But when I load the UI file :
class MyQtApp():
def __init__(self):
super().__init__()
self.ui = QUiLoader().load("ui/main.ui")
self.ui.show()
if __name__ == '__main__':
app = QtWidgets.QApplication([])
my_app = MyQtApp()
app.exec_()
the icons are lost. They don't appear on the buttons.
I don't mind compiling the icons.qrc using :
pyside2-rcc.exe icons.qrc -o icons_rc.py
but how can I link the icons_rc.py to my code if I use QUiLoader().load() ?
PS: Of course, when I use both the resource and the ui compiler (pyside2-uic.exe and pyside2-rcc.exe), I don't have this issue but I prefer using QUiLoader().load() if possible.

The only thing you have to do is to import the icons_rc.py in your main file.
Just add the statement
import icons_rc.py
In the beginning of you file and that's it. This works for me.

Related

pyqt5 default native menu text on MacOS

Is there a way to change default menu title for native menu on MacOS for a simple pyqt5 app?
I'm trying to change "Python" menu entry on above image. Is there a way to rename it? Can I hide it somehow?
This code prints only "File":
menubar = self.menuBar()
for item in menubar.actions():
print(item.text())
menubar.setNativeMenuBar(False) doesn't help too (just move "File" into the MainWindow).
Update Sample app code:
from PyQt5 import QtCore, QtWidgets, uic
import sys
class Ui(QtWidgets.QMainWindow):
def __init__(self):
super(Ui, self).__init__()
# QtCore.QCoreApplication.setApplicationName('QtFoo') # doesn't work
uic.loadUi('App.ui', self)
self.show()
# app = QtWidgets.QApplication(sys.argv)
app = QtWidgets.QApplication(["MyCoolApp"])
# app.setApplicationName("QtFoo") # doesn't work
# app.setApplicationDisplayName("Display Name")
window = Ui()
app.exec()
If you are not willing to package your app, you could create a temporary symbolic link to python as described here. This link can be used to execute python apps while displaying a custom name in the title bar:
go to the location of your app (e.g. my_app.py) file in the terminal
create symbolic link (replace location to your python interpreter, replace "BeautifulNaming" with desired name)
ln -s /Users/Christian/miniconda3/bin/python BeautifulNaming
call link to python with your script
./BeautifulNaming my_app.py
The "python" in the system menu bar appears because you run the script from the python, as soon as you package the application the title will disappear. For example the following code
# FileName PyQt5MenuProblem.py
import sys
from PyQt5.QtWidgets import QApplication,QMainWindow
class AppTest(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.setWindowTitle("My application")
self._createMenuBar()
def _createMenuBar(self):
self.menuBar = self.menuBar()
self.menuBar.setNativeMenuBar(False)
fileMenu = self.menuBar.addMenu("&File")
editMenu = self.menuBar.addMenu("&Edit")
if __name__== "__main__":
app = QApplication(sys.argv)
plotWindow = AppTest()
plotWindow.showMaximized()
sys.exit(app.exec_())
after packaging with
pyinstaller --onefile PyQt5MenuProblem.py
looks like
Key words: MacOS, User Interface, LSUIElement, pyinstaller, pyqt5

PyQt WebEngineView interferes with MainMenu

I'm trying to create an application that contains a web browser within it, but when I add the web browser my menu bar visually disappears but functionally remains in place. The following are two images, one showing the "self.centralWidget(self.web_widget)" commented out, and the other allows that line to run. If you run the example code, you will also see that while visually the entire web page appears as if the menu bar wasn't present, you have to click slightly below each entry field and button in order to activate it, behaving as if the menu bar was in fact present.
Web Widget Commented Out
Web Widget Active
Example Code
import os
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtWebEngineWidgets import *
class WebPage(QWebEngineView):
def __init__(self, parent=None):
QWebEngineView.__init__(self)
self.current_url = ''
self.load(QUrl("https://facebook.com"))
self.loadFinished.connect(self._on_load_finished)
def _on_load_finished(self):
print("Url Loaded")
class MainWindow(QMainWindow):
def __init__(self, parent=None):
# Initialize the Main Window
super(MainWindow, self).__init__(parent)
self.create_menu()
self.add_web_widet()
self.show()
def create_menu(self):
''' Creates the Main Menu '''
self.main_menu = self.menuBar()
self.main_menu_actions = {}
self.file_menu = self.main_menu.addMenu("Example File Menu")
self.file_menu.addAction(QAction("Testing Testing", self))
def add_web_widet(self):
self.web_widget = WebPage(self)
self.setCentralWidget(self.web_widget)
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.showMaximized()
sys.exit(app.exec_()) # only need one app, one running event loop
Development Environment
Windows 10, PyQt5, pyqt5-5.9
EDIT
The problem doesn't seem to be directly related to the menu bar. Even removing the menu bar the issue still occurs. That said, changing from showMaximized() to showFullScreen() does seem to solve the problem.
I no longer believe this is an issue with PyQt5 specifically but rather a problem with the graphics driver. Specifically, if you look at Atlassian's HipChat application it has a similar problem which is documented here:
https://jira.atlassian.com/browse/HCPUB-3177
Some individuals were able to solve the problem by running the application from the command prompt with the addendum "--disable-gpu" but that didn't work for my python application. On the other hand, rolling back the Intel(R) HD Graphics Driver did solve my problem. Version 21.20.16.4627 is the one that seems to be causing problems.

PyQt: ListWidget.insertItem not shown

I have a fairly simply PyQt question. (Python 3.4, PyQt 4.11.3, Qt 4.8.5) I built a very simple dialog using Qt Designer (Ui_Dialog). This object has a QPushButton, a QLineEdit, and a QListWidget. I wrote another object that inherits from Ui_Dialog, and sets up a returnPressed signal from QLineEdit that should add some text to the QListWidget. Unfortunately, this does not work.
Here's my code:
import sys
from PyQt4 import QtGui
from dialog import Ui_Dialog
class ImDialog(QtGui.QDialog, Ui_Dialog):
def __init__(self):
super(ImDialog, self).__init__()
self.setupUi(self)
self.lineEdit.returnPressed.connect(self.additem)
self.pushButton.clicked.connect(self.listWidget.clear)
def additem(self):
text = self.lineEdit.text()
print(text)
self.listWidget.insertItem(0, text)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
ui = ImDialog()
ui.show()
sys.exit(app.exec_())
The text in the line editor prints fine to the terminal, but it is not added to the listWidget.
Interestingly, if I comment out the sys.exit line and run this in an IPython terminal, I can add as much text as I like to the listWidget without a problem.
[In 1]: %run that_program.py
[In 2]: ui.listWidget.insertItem(0, "Test") # This works fine
If anyone has any suggestions to get this to work (outside IPython), I would appreciate the help. Thanks
There is only one button in your dialog, and so it will become the auto-default. This means that whenever you press enter in the dialog, the button will receive a press event, even if it doesn't currently have the keyboard focus.
So the item does get added to the list-widget - it's just that it then immediately gets cleared by the auto-default button.
To fix this, reset the auto-default like so:
self.pushButton.setAutoDefault(False)
(NB: you can also change this property in Qt Designer).

PyQt code doesnt work when it's inside the class

Recently I have created an simple PyQt based app. All it have to do is to make a system tray icon. The problem is that I can see the application is running, however no tray icon is being made. When I move the tray icon code outside the class its working fine. So why is the class a problem here?
My code:
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtGui, QtCore
app = QtGui.QApplication(sys.argv)
class myApp(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.createTrayIcon()
def createTrayIcon(self):
self.trayIconMenu = QtGui.QMenu()
self.trayIconMenu.addAction("Close", lambda : exit(1))
self.trayIcon = QtGui.QSystemTrayIcon()
self.trayIcon.setIcon(QtGui.QIcon(r"icon.png"))
self.trayIcon.setToolTip("Tooltip")
self.trayIcon.setContextMenu(self.trayIconMenu)
self.trayIcon.show()
self.trayIcon.showMessage("Title", u"Content")
myApp()
sys.exit(app.exec_())
As you don't store the object created in the call to myApp() in a variable, it is garbage collected by Python at some point. Your tray icon may thus be visible for a very brief moment, but will be cleaned up and disappear at some point (which is what you are seeing).
As long as you do myapp = myApp() instead, you will be fine.
Have your forget this line at main ?
.
.
.
objectMyApp = myApp()
objectMyApp.show() # Or don't show please comment this in.
sys.exit(app.exec_())
Problem is your create object but not put in variable. It better if your put in variable.

QToolbar on mac could not be unified even after setting setUnifiedTitleAndToolBarOnMac flag

This is a Mac OS QT issue,
I have created a QMainWindow and added a toolbar to it.
after setting "setUnifiedTitleAndToolBarOnMac" flag to TRUE my toolbar got unified to my mainwindow.
But when i customize the window buttons such as only close button option,
my window Toolbar got detached from title bar as if it looks like a winodow OS toolbar
not like a Mac native one.
Attached my test code below which is in pyqt.
Hope someone know how to achieve it.
Thanks
from PyQt4 import QtGui
from PyQt4 import QtCore
class Ui_windo(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QMainWindow.__init__(self, parent)
self.setObjectName("windo")
self.resize(400, 300)
self.setWindowTitle(QtGui.QApplication.translate("window", "window", None, QtGui.QApplication.UnicodeUTF8))
self.b1 = QtGui.QToolButton()
self.b1.setToolButtonStyle(QtCore.Qt.ToolButtonTextOnly)
self.b1.setText('Test')
self.b1.setObjectName("b1")
self.b1.setCheckable(True)
_toolBar = self.addToolBar('test')
_toolBar.setMovable(False)
_toolBar.addWidget(self.b1)
self.setUnifiedTitleAndToolBarOnMac(True);
_windowButtons = QtCore.Qt.Window| QtCore.Qt.WindowCloseButtonHint | QtCore.Qt.CustomizeWindowHint
self.setWindowFlags(_windowButtons)
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
ui = Ui_windo()
ui.show()
sys.exit(app.exec_())
Seems that the Qt.CustomizeWindowHint flag removes the styling from the toolbar. Not sure if this is expected behavior or a bug. There have been a number of style-related bug reports pertaining to setUnifiedTitleAndToolBarOnMac. You should post it to confirm whether its expected or not.
https://bugreports.qt-project.org/secure/IssueNavigator.jspa
If you set your window flag to use Qt.Tool, that will at least get you close by removing the minimize button.

Categories

Resources