'how to set the position of qdialog in pyside6
Unable to position the dialog @ the bottom right of window in order to set the position setGeometry(...) method is used but its always @ center. How to position the dialog to the bottom right?
Execution starts from keyWindow.py(main) and when numeric keys are pressed it opens dialog which is located in keyDialog.py
keyWindow.py
import sys
from PySide6.QtWidgets import QWidget, QMainWindow, QApplication, QHBoxLayout, QVBoxLayout, QDialog
from PySide6.QtGui import QKeyEvent
from PySide6.QtCore import Qt
import KeyDialog
class MainWindow(QMainWindow):
numericDic = {
Qt.Key_0: 0,
Qt.Key_1: 1,
Qt.Key_2: 2,
Qt.Key_3: 3,
Qt.Key_4: 4,
Qt.Key_5: 5,
Qt.Key_6: 6,
Qt.Key_7: 7,
Qt.Key_8: 8,
Qt.Key_9: 9
}
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.resize(600, 500)
def keyPressEvent(self, keyEvent: QKeyEvent):
if keyEvent.isAutoRepeat():
return
key = keyEvent.key()
print(f"keyPressEvent {key}")
if key in MainWindow.numericDic.keys() or key == Qt.Key_Plus or key == Qt.Key_Minus:
keyDialog = KeyDialog(self)
keyDialog.exec()
# def keyReleaseEvent(self, keyEvent: QKeyEvent):
# if keyEvent.isAutoRepeat():
# return
# key = keyEvent.key()
# print(f"keyReleaseEvent {key}")
if __name__ == "__main__":
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec())
keyDialog.py
from PySide6.QtWidgets import (QWidget, QMainWindow, QHBoxLayout, QVBoxLayout, QDialog,
QGridLayout, QDoubleSpinBox, QApplication)
from PySide6.QtCore import Qt
class KeyDialog(QDialog):
def __init__(self, parent=None):
super().__init__(parent=parent)
self.initUI()
def initUI(self):
width, height = self.getScreenSize()
ownWidth = 200
ownHeight = 40
bottomGap = 30
rightGap = 40
self.setGeometry(width - ownWidth - rightGap, height - ownHeight - bottomGap, ownWidth, ownHeight)
self.layout = QGridLayout()
self.box = QDoubleSpinBox()
self.box.editingFinished.connect(self.boxValueChanged)
self.box.setMinimum(float("-inf"))
self.box.setMaximum(float("+inf"))
self.box.setValue(3.5)
self.box.setSingleStep(0.5)
self.layout.addWidget(self.box, 0, 0)
self.setLayout(self.layout)
def getScreenSize(self):
return (QApplication.primaryScreen().size().width(), QApplication.primaryScreen().size().height())
def boxValueChanged(self):
print(f"new Value {self.box.value()}")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
