How can i disable mouse press event for special widget? - python

I tried to disable it by using attribute QtCore.Qt.WA_TransparentForMouseEvents, also I tried to disable my frame, but ef mousePressEvent(self, event) is still working
import sys
from b import *
from PyQt5 import QtCore, QtGui, QtWidgets
class main_interface(QtWidgets.QMainWindow):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.ui = Ui_Form()
self.ui.setupUi(self)
self.setWindowFlag(QtCore.Qt.FramelessWindowHint)
self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
self.ui.frame.setAttribute(QtCore.Qt.WA_TransparentForMouseEvents)
def mousePressEvent(self, event):
print('pressed')
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
myapp = main_interface()
myapp.show()
sys.exit(app.exec_())

Related

How to do override closeEvent functions to make it working

I'm using pyside2 and pyqt5 lib to loading my UI file.
from PySide2 import QtWidgets
from PySide2.QtWidgets import QApplication
from PySide2.QtUiTools import QUiLoader
class A(QtWidgets.QWidget):
def __init__(self, parent=None):
super(A, self).__init__(parent)
self.ui = QUiLoader().load('uiFile.ui')
def closeEvent(self, event):
event.ignore()
app = QApplication([])
mainWin = A()
mainWin.ui.show()
app.exec_()
In my thought, it will show '11111' when I clicked the X button.
However, it's not working at all.
here is the ui file
The problem is that "A" is a widget that is not displayed, it is not the window, so override closeEvent does not make sense. Instead you should use an event filter to monitor the window's events.
from PySide2.QtCore import QEvent, QObject
from PySide2.QtWidgets import QApplication
from PySide2.QtUiTools import QUiLoader
class Manager(QObject):
def __init__(self, ui, parent=None):
super(Manager, self).__init__(parent)
self._ui = ui
self.ui.installEventFilter(self)
#property
def ui(self):
return self._ui
def eventFilter(self, obj, event):
if obj is self.ui:
if event.type() == QEvent.Close:
event.ignore()
return True
super().eventFilter(obj, event)
def main():
app = QApplication([])
manager = Manager(QUiLoader().load("uiFile.ui"))
manager.ui.show()
app.exec_()
if __name__ == "__main__":
main()
If you want to override the closeEvent method of the QMainWindow used in the .ui then you must promote it and register it as this other answer shows.

Why does the QMainWindow not show? [duplicate]

I am loading a QMainWindow from an .ui file - it's working fine but window events like resize won't be triggered. I can't really figure out what I am doing wrong.
This is the code:
class TestWindow(QMainWindow):
def __init__(self, parent=None):
super(TestWindow, self).__init__(parent)
loader = QUiLoader()
file = QFile(abspath("ui/mainwindow.ui"))
file.open(QFile.ReadOnly)
self.window = loader.load(file, parent)
file.close()
self.window.show()
def resizeEvent(self, event):
print "resize"
app = QApplication(sys.argv)
test = TestWindow()
sys.exit(app.exec_())
The .ui file can be found here.
It seems that you have a confusion, but for you to understand call the test method of TestWindow:
import os
from PySide2 import QtCore, QtWidgets, QtUiTools
class TestWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(TestWindow, self).__init__(parent)
loader = QtUiTools.QUiLoader()
file = QtCore.QFile(os.path.abspath("ui/mainwindow.ui"))
file.open(QtCore.QFile.ReadOnly)
self.window = loader.load(file, parent)
file.close()
self.window.show()
self.show()
def resizeEvent(self, event):
print("resize")
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
test = TestWindow()
sys.exit(app.exec_())
And if you move the small window observe that the event is triggered.
Why does that happen?: QUiLoader creates a widget based on the .ui that unlike uic.loadUi() or ui.loadUiType() of PyQt5 does not load in the main widget but creates a new widget, maybe that's a disadvantage but that's the limitations.
So depending on what you want to do there are several options:
To load the .ui with QUiLoader() it is not necessary to have TestWindow as a parent since it can be a QObject that monitors the events through an event filter.
import os
from PySide2 import QtCore, QtWidgets, QtUiTools
class Manager(QtCore.QObject):
def __init__(self, parent_widget=None, parent=None):
super(Manager, self).__init__(parent)
loader = QtUiTools.QUiLoader()
file = QtCore.QFile(os.path.abspath("ui/mainwindow.ui"))
file.open(QtCore.QFile.ReadOnly)
self.window = loader.load(file, parent_widget)
file.close()
self.window.installEventFilter(self)
self.window.show()
self.setParent(self.window)
self.window.destroyed.connect(lambda *args: print(">>>>>>"))
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.Close and self.window is obj:
self.window.removeEventFilter(self)
elif event.type() == QtCore.QEvent.Resize and self.window is obj:
print("resize")
return super(Manager, self).eventFilter(obj, event)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
test = Manager()
sys.exit(app.exec_())
Another option is to make the self.widow the centralwidget (the QMainWindow in the .ui will be the centralwidget of the TestWindow, so the resize will be from the TestWindow not from the .ui but as if the size of the .ui is changed, the same will happen with TestWindow):
import os
from PySide2 import QtCore, QtWidgets, QtUiTools
class TestWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(TestWindow, self).__init__(parent)
loader = QtUiTools.QUiLoader()
file = QtCore.QFile(os.path.abspath("ui/mainwindow.ui"))
if file.open(QtCore.QFile.ReadOnly):
self.window = loader.load(file, parent)
file.close()
self.setCentralWidget(self.window)
self.show()
def resizeEvent(self, event):
print("resize")
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
test = TestWindow()
sys.exit(app.exec_())
The previous methods only serve to notify you of the event but if you want to overwrite it is better that you use pyuic converting the .ui to .py

Python: Drag from pyqt5 to folder/desktop

I have this script that allows dragging a file to a pyqt5 gui and receiving its path:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from idna import unicode
class MainWidget(QMainWindow):
def __init__(self, parent=None):
super(MainWidget, self).__init__(parent)
self.setWindowTitle("Drag to here")
self.resize(720, 480)
self.setAcceptDrops(True)
def dragEnterEvent(self, event):
if event.mimeData().hasUrls():
event.accept()
else:
event.ignore()
def dropEvent(self, event):
files = [unicode(u.toLocalFile()) for u in event.mimeData().urls()]
for f in files:
print(f)
if __name__ == '__main__':
app = QApplication(sys.argv)
demo = MainWidget()
demo.show()
sys.exit(app.exec_())
The only problem is that I would like to drag a file from the pyqt5 gui to the desktop/folder. Is there any way to do this?

QToolBar does not show text next to icon

I have the following code:
from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon
class ConfigureBar(QtWidgets.QToolBar):
def __init__(self, parent=None):
super().__init__(parent)
self.addAction(QtWidgets.QIcon("some_icon.png"), "Hi")
self.addSeparator()
self.addAction(QIcon("some_icon.png"), "Hello")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
window = ConfigureBar()
window.show()
app.exec()
But for some reason, the displayed widget does not have a text (Hi, Hello), only the icon. I tried to find some answer but I can't seem to get the keyword right. Help?
The toolButtonStyle property indicates the style of how the QToolButtons are shown, and by default it is Qt::ToolButtonIconOnly, so only show the icon, if you want to show the text you have to use Qt::ToolButtonTextBesideIcon or Qt::ToolButtonTextUnderIcon:
from PyQt5 import QtCore, QtGui, QtWidgets
class ConfigureBar(QtWidgets.QToolBar):
def __init__(self, parent=None):
super().__init__(parent)
self.setToolButtonStyle(QtCore.Qt.ToolButtonTextBesideIcon)
# or
# self.setToolButtonStyle(QtCore.Qt.ToolButtonTextUnderIcon)
self.addAction(QtGui.QIcon("some_icon.png"), "Hi")
self.addSeparator()
self.addAction(QtGui.QIcon("some_icon.png"), "Hello")
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
toolbar = ConfigureBar()
w = QtWidgets.QMainWindow()
w.addToolBar(toolbar)
w.show()
sys.exit(app.exec_())

Why does not pyqt recognize background colour of Qt Designer css Style Sheet?

I set the background properties in Qt Designer with the Style Sheet, e.g. green:
Apparently it works.
I translate the ui-file to pyqt with pyuic4 and get:
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName(_fromUtf8("Dialog"))
Dialog.resize(400, 300)
Dialog.setAutoFillBackground(False)
Dialog.setStyleSheet(_fromUtf8("QDialog{background-color: green;}"))
I write the code in python to show the green window, but it doesn’t work.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from background_green import *
class Window(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
myapp = Window()
myapp.show()
sys.exit(app.exec_())
Although I follow the same process with other Widgets trouble-free, I cannot change the colour of my main window.
The problem is generated because when you use QDesigner you implemented a QDialog, so your widget should be of that type.
Change QWidget to QDialog.
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.resize(400, 300)
Dialog.setAutoFillBackground(False)
Dialog.setStyleSheet("QDialog{background-color: green;}")
class Window(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent=parent)
self.ui = Ui_Dialog()
self.ui.setupUi(self)
if __name__ == '__main__':
app = QApplication(sys.argv)
myapp = Window()
myapp.show()
sys.exit(app.exec_())
Screenshot:

Categories