PyQt5 MainWindow get postion relative to screen - python-3.x

Simple QMainWindow implementation and too much time googling I need to know what the XY coordinates for the main window are relative to the screen. This implementation always shows 0,0:
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication, QMainWindow
from PyQt5.uic import loadUi
class TestReportsApp(QMainWindow):
def __init__(self, parent=None):
super(TestReportsApp, self).__init__(parent)
loadUi('test.ui', self)
#pyqtSlot()
def showEvent(self, e):
print(self.geometry())
app = QApplication(sys.argv)
mainWin = TestReportsApp()
mainWin.show()
sys.exit(app.exec_())
How do I get the coordinates of the top-left XY position of the main window relative to the screen?

Related

PyQt5.QIcon Is not showing any icon

I am trying to show the icon in the window bar and this is not simply working. I tried several solutions but still it doesn't work. Any solution for this code?
import os
from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import *
from PyQt5.QtWebEngineWidgets import *
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.browser = QWebEngineView()
self.browser.setUrl(QUrl('http://gmail.com'))
self.setCentralWidget(self.browser)
self.showMaximized()
self.setWindowTitle('G')
self.setWindowIcon(QIcon(os.path.join('cil.png')))
self.show()
app = QApplication(sys.argv)
window = MainWindow()
app.exec_()
Try putting the icon like this
icon = QIcon()
icon.addPixmap(QtGui.QPixmap()) #add full path or respective path to the icon image inside QtGui.QPixmap()
self.setWindowIcon(icon)

How do I align centre image horizontally in PyQt5?

I'm working on my college project, I want to align an Image to centre Horizontally, I tried many thing but not find solution. Here is my code:
from PyQt5.QtGui import QPalette, QLinearGradient, QColor, QBrush, QPixmap
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
import sys
from PyQt5 import QtGui
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.acceptDrops()
self.setWindowTitle("Mask Detection")
self.setWindowIcon(QtGui.QIcon('img.png'))
self.setGeometry(0, 0, 400, 300)
self.label = QLabel(self)
self.label.setAlignment(Qt.AlignCenter)
self.pixmap = QPixmap('PBL.png')
self.label.setPixmap(self.pixmap)
self.label.resize(self.pixmap.width(),
self.pixmap.height())
self.show()
App = QApplication(sys.argv)
window = Window()
p = QPalette()
gradient = QLinearGradient(0, 0, 0, 400)
gradient.setColorAt(0.0, QColor(56, 93, 166))
gradient.setColorAt(1.0, QColor(10, 123, 146))
p.setBrush(QPalette.Window, QBrush(gradient))
window.setPalette(p)
sys.exit(App.exec())
The alignment is with respect to the geometry of the element itself, and since the geometry of the QLabel has the same size as the QPixmap then it will not make a difference. The solution is to make the geometry of the QLabel be the same as the window and that can be done by setting it in the centralWidget:
self.setCentralWidget(self.label)

Why QMainWindow is in top left corner while QDialog is in center position?

I don't understand why by default a QDialog is placed in center position and a QMainWindow in the top left corner?
Example code:
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QDialog
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("MainWindow")
class Dialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent)
self.setWindowTitle("Dialog")
if __name__ == "__main__":
app = QApplication(sys.argv)
main_window = MainWindow()
main_window.show()
dialog = Dialog()
dialog.show()
sys.exit(app.exec_())
I would prefer a QMainWindow in center position by default.

PyQt5 - Add image in background of MainWindow layout

New to PyQt5... Here is a very basic question.
I would like to add an image inside the layout of a widget. This widget is the Main Window / root widget of my application. I use the following code, but I get an error message.
import sys
from PyQt5.QtGui import QImage
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(300,300,300,220)
self.setWindowTitle("Hello !")
oImage = QImage("backgound.png")
oLayout = QVBoxLayout()
oLayout.addWidget(oImage)
self.setLayout(oLayout)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
oMainwindow = MainWindow()
sys.exit(app.exec_())
TypeError: QBoxLayout.addWidget(QWidget, int stretch=0, Qt.Alignment alignment=0): argument 1 has unexpected type 'QImage'
Apparently a QLayoutWidget does not accept a QImage as an input. Is there a workaround to have an image appear as a brackground in a QWidget ?
The QVBoxLayout class lines up widgets vertically.
documentation QVBoxLayout
QImage is no widget.
on many widgets e.g. QmainWindow, QLabel you can use
widget.setStyleSheet(„ background-image: url(backgound.png);“)
on my machine this doesn't work with QWidget. In this case you can use the following rewrite of your code:
import sys
from PyQt5.QtCore import QSize
from PyQt5.QtGui import QImage, QPalette, QBrush
from PyQt5.QtWidgets import *
class MainWindow(QWidget):
def __init__(self):
QWidget.__init__(self)
self.setGeometry(100,100,300,200)
oImage = QImage("test.png")
sImage = oImage.scaled(QSize(300,200)) # resize Image to widgets size
palette = QPalette()
palette.setBrush(QPalette.Window, QBrush(sImage))
self.setPalette(palette)
self.label = QLabel('Test', self) # test, if it's really backgroundimage
self.label.setGeometry(50,50,200,50)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
oMainwindow = MainWindow()
sys.exit(app.exec_())

PyQT how to make a QEvent.Enter on QPushbutton?

My main goal really is,i have a Qpushbutton and a frame, what im trying to do is. when i hover on a Qpushbutton the frame will show up. using visible false. can somebody help me please on how to make an events?
Here's a quick example, similar to the example I gave in your previous question:
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, \
QWidget, QLabel
from PyQt4.QtCore import pyqtSignal
class HoverButton(QPushButton):
mouseHover = pyqtSignal(bool)
def __init__(self, parent=None):
QPushButton.__init__(self, parent)
self.setMouseTracking(True)
def enterEvent(self, event):
self.mouseHover.emit(True)
def leaveEvent(self, event):
self.mouseHover.emit(False)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
QMainWindow.__init__(self, parent)
self.button = HoverButton(self)
self.button.setText('Button')
self.label = QLabel('QLabel uses QFrame...', self)
self.label.move(40, 40)
self.label.setVisible(False)
self.button.mouseHover.connect(self.label.setVisible)
def startmain():
app = QApplication(sys.argv)
mainwindow = MainWindow()
mainwindow.show()
sys.exit(app.exec_())
if __name__ == "__main__":
import sys
startmain()

Resources