Hide the app from screen but not from taskbar - python

I would like to hide the app from screen but not from taskbar, I tried this:
app = QtWidgets.QApplication([])
w = QtWidgets.QWidget()
w.show()
w.resize(0, 0)
but it doesn't work, any idea?

app = QtWidgets.QApplication([])
w = QtWidgets.QWidget()
w.showMinimized()

I use QMainWindow instead of QWidget, then I override the focusInEvent and focusOutEvent events.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtCore import Qt
from sys import argv, exit
class Window(QMainWindow):
def __init__(self):
super(Window, self).__init__()
self.setFocusPolicy(Qt.StrongFocus)
def focusInEvent(self, event):
print('focusInEvent')
self.setWindowTitle('focusInEvent')
self.showMinimized()
def focusOutEvent(self, event):
print('focusOutEvent')
self.setWindowTitle('focusOutEvent')
# self.showMinimized()
if __name__ == '__main__':
app = QApplication([])
w = Window()
w.showMinimized()
exit(app.exec_())

Related

Multiple key register event PyQT5

I have a program programming with Pyqt5 in which I would like to register some keys simoultaneously;for example, up+right to go to the upper diagonal.
The problem is that with the pressEvent only accept the first key.
Also I use QPygletWidget, but I can not register the push_handlers event from pyglet to PyQt5.
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QApplication
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
def keyPressEvent(self, e, autorep=False):
# print(e.key)
# self.widget.key_pressed = e.key()
print(e.key())
# self.widget.key_pressed = None
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_()) ```
Unfortunately QKeySequence doesn't work for sequences that don't contain CTRL.
You can use keyPressEvent and keyReleaseEvent to keep track what keys is currently pressed and not released yet.
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import Qt
class MyWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self._keys = {Qt.Key_Up: False, Qt.Key_Right: False}
def keyPressEvent(self, event):
if event.key() in [Qt.Key_Up, Qt.Key_Right]:
self._keys[event.key()] = True
if self._keys[Qt.Key_Up] and self._keys[Qt.Key_Right]:
self.onUpRight()
def keyReleaseEvent(self, event):
if event.key() in [Qt.Key_Up, Qt.Key_Right]:
self._keys[event.key()] = False
def onUpRight(self):
print("onUpRight")
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec_())
Be aware that if hold a key longer than some threshold (a second I guess) it sends sequences of keyPressEvent keyReleaseEvent as if you repeatedly pressed a button, so onUpRight will be called multiple times.

PyQt5 add eventFilter outside of pyuic generated code

I'm a new Qt user here.
I've got a project where I'm to use a pyuic generated .py file but I don't have access to it.
I'm also supposed to be installing event filters on some of the objects. Is it possible to use object.installEventFilter() outside the generated .py file?
main_window.py
class Ui_MainWindow(QtWidgets.QMainWindow):
self.titleLabel = QtWidgets.QLabel(MainWindow)
Frontend.py
from PyQt5 import QtCore, QtGui, QtWidgets
from main_window import Ui_MainWindow
class Session (object):
def __init__(self):
self.mainUI = None
def eventFilter(self, source, event):
eventReturn = False
if(event.type() == QtCore.QEvent.MouseButtonDblClick and
source is self.lblTitle):
eventReturn = self._labelTitle(source, event)
return eventReturn
def _labelTitle(self, widget, event):
retVal = True
print("works, Title")
def GUIcontroller():
import sys
app = QtWidgets.QApplication(sys.argv)
thisSession = Session()
MainWindow = QtWidgets.QMainWindow()
thisSession.mainUI = Ui_MainWindow()
thisSession.mainUI.setupUi(MainWindow)
thisSession.mainUI.titleLabel.installEventFilter(???)
MainWindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
GUIcontroller()
The event filters only work in the QObjects, and in your code you use object that will not work, considering the above a possible solution is:
from PyQt5 import QtCore, QtGui, QtWidgets
from main_window import Ui_MainWindow
class Session(QtCore.QObject):
def __init__(self, ui):
super().__init__(ui)
self._ui = ui
self.ui.installEventFilter(self)
#property
def ui(self):
return self._ui
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.MouseButtonDblClick and source is self.ui:
print("double clicked")
return super().eventFilter(source, event)
def GUIcontroller():
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
mainUI = Ui_MainWindow()
mainUI.setupUi(MainWindow)
thisSession = Session(mainUI.titleLabel)
MainWindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
GUIcontroller()

How to change UI in same window using PyQt5?

I'm just getting started with PyQt5. I have been trying to accomplish a seemingly very simple task but haven't been able to get enough info about it. After a fair bit of googling I have been able to get one window to close and another to launch with the other UI loaded but that's not what I want to do here.
I want to switch the UI in the same window. I am loading the UI files as global variables in my python file where I have 2 classes for each UI. When I click a particular button in one UI, I want to switch to the other UI in the same window. Below is a sample of the code:
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
from PyQt5.uic import loadUiType
import os
about_company_ui, _ = loadUiType(os.path.join('frontend', 'ui', 'about_company.ui'))
intern_placement_ui, _ = loadUiType(os.path.join('frontend', 'ui', 'intern_placement.ui'))
class InternPlacement(QMainWindow, intern_placement_ui):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
self.intern_pushButton.clicked.connect(self.change)
def change(self):
self.about_company = AboutCompany()
self.about_company.show()
self.close()
class AboutCompany(QMainWindow, about_company_ui):
def __init__(self):
QMainWindow.__init__(self)
self.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = InternPlacement()
window.show()
app.exec_()
You have to use a QStackedWidget
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic
ui_folder = os.path.join("frontend", "ui")
about_company_ui, _ = uic.loadUiType(os.path.join(ui_folder, "about_company.ui"))
intern_placement_ui, _ = uic.loadUiType(os.path.join(ui_folder, "intern_placement.ui"))
class InternPlacement(QtWidgets.QMainWindow, intern_placement_ui):
def __init__(self, parent=None):
super(InternPlacement, self).__init__(parent)
self.setupUi(self)
class AboutCompany(QtWidgets.QMainWindow, about_company_ui):
def __init__(self, parent=None):
super(AboutCompany, self).__init__(parent)
self.setupUi(self)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
intern_window = InternPlacement()
about_window = AboutCompany()
w = QtWidgets.QStackedWidget()
w.addWidget(intern_window)
w.addWidget(about_window)
intern_window.intern_pushButton.clicked.connect(lambda: w.setCurrentIndex(1))
w.resize(640, 480)
w.show()
sys.exit(app.exec_())

How to display Hello World dialog from TaskBar Menu Application Python/Pyside2 for MacOS

Creating first a dummy "hello world" dialog/window, how to display it from taskbar/menu on MacOS. Thanks.
If I understood your question,
you wanted to open a QDialog from the menu bar of a QMainWindow, right?
For that this is a simple approach:
import sys
from PySide2.QtCore import Slot
from PySide2.QtWidgets import (QApplication, QMainWindow, QAction,
QDialog, QLabel, QHBoxLayout)
class Dialog(QDialog):
def __init__(self):
QDialog.__init__(self)
layout = QHBoxLayout()
layout.addWidget(QLabel("Hello World"))
self.setLayout(layout)
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.menu = self.menuBar()
self.test_menu = self.menu.addMenu("Test")
self.hello_action = QAction("hello", self)
self.hello_action.triggered.connect(self.hello_dialog)
self.test_menu.addAction(self.hello_action)
#Slot()
def hello_dialog(self, checked):
dialog = Dialog()
dialog.exec_()
if __name__ == "__main__":
app = QApplication()
window = MainWindow()
window.show()
sys.exit(app.exec_())

Put an Image on a QPushButton

I'm a beginner in PyQt and I have an image known as add.gif. I need to put this image in a QPushButton but I don't know how.
Example:
from PyQt4 import QtGui, QtCore
class Window(QtGui.QWidget):
def __init__(self):
QtGui.QWidget.__init__(self)
self.button = QtGui.QPushButton('', self)
self.button.clicked.connect(self.handleButton)
self.button.setIcon(QtGui.QIcon('myImage.jpg'))
self.button.setIconSize(QtCore.QSize(24,24))
layout = QtGui.QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
pass
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Here is the same example from #NorthCat but for PyQt5:
from PyQt5 import QtGui, QtCore
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout
class Window(QWidget):
def __init__(self):
QWidget.__init__(self)
self.button = QPushButton('', self)
self.button.clicked.connect(self.handleButton)
self.button.setIcon(QtGui.QIcon('myImage.jpg'))
self.button.setIconSize(QtCore.QSize(200,200))
layout = QVBoxLayout(self)
layout.addWidget(self.button)
def handleButton(self):
pass
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
Assuming pyqt supports gif pictures, this should work
icon = QtGui.QPixmap('add.gif')
button = QtGui.QPushButton()
button.setIcon(icon)
QPushButton
Push buttons display a textual label, and optionally a small icon.
These can be set using the constructors and changed later using
setText() and setIcon(). If the button is disabled, the appearance of
the text and icon will be manipulated with respect to the GUI style to
make the button look "disabled".

Categories

Resources