Why QMainWindow is in top left corner while QDialog is in center position? - python-3.x

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.

Related

PyQt5: QLineEdit doesn't show inside a QGroupBox

I'm creating a PyQt5 application in which I want to put some of QLineEdit widgets inside a QGroupBox.
When I run my application, GroupBox is visible and LineEdit is not.
In CreateLinesEdit() I commented a line which sets the line edit visible, but it opens lineEdit in a new window.
from PyQt5.QtWidgets import QGroupBox, QApplication, QLineEdit, QVBoxLayout, QWidget, QHBoxLayout
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.InitWindow()
def InitWindow(self):
self.BoxLayout()
self.AddBox()
self.CreateLinesEdit()
self.show()
def BoxLayout(self):
self.groupBoxScreen = QGroupBox()
self.vbox = QVBoxLayout()
self.vbox_screenGame = QVBoxLayout()
self.hbox_lineEdit = QHBoxLayout()
def AddBox(self):
self.vbox_screenGame.addItem(self.hbox_lineEdit)
self.groupBoxScreen.setLayout(self.vbox_screenGame)
self.vbox.addWidget(self.groupBoxScreen)
self.setLayout(self.vbox)
def CreateLinesEdit(self):
self.lines_edit = []
for i in range(0, 4):
LineEdit = QLineEdit()
# LineEdit.setVisible(True)
self.lines_edit.append(LineEdit)
self.hbox_lineEdit.addWidget(self.lines_edit[i])
if __name__ == "__main__":
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())

Disable mouse pointer in QGraphicsView

I want to disable the mouse pointer in a QGraphicsView.
What line of code do I need to add in the following example?
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGraphicsView
class GraphicsWindow(QGraphicsView):
def __init__(self, parent=None):
super().__init__(parent)
self.showFullScreen()
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
graphics_window = GraphicsWindow()
graphics_window.show()
sys.exit(app.exec_())
Qt::BlankCursor A blank/invisible cursor, typically used when the cursor shape needs to be hidden.
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QGraphicsView
class GraphicsWindow(QGraphicsView):
def __init__(self, parent=None):
super().__init__(parent)
self.showFullScreen()
self.setCursor(Qt.BlankCursor) # < ------
def keyPressEvent(self, event):
if event.key() == Qt.Key_Escape:
self.close()
if __name__ == "__main__":
app = QApplication(sys.argv)
graphics_window = GraphicsWindow()
graphics_window.show()
sys.exit(app.exec_())

PyQt5 MainWindow get postion relative to screen

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?

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