gpt4 book ai didi

python - 如何使对话框或弹出窗口可以保留在主窗口后面并且不覆盖主窗口

转载 作者:行者123 更新时间:2023-12-01 07:31:05 26 4
gpt4 key购买 nike

我是pyqt5的新手,我想要弹出一个窗口,并让该窗口可以保留在主窗口后面并且不可点击(类似于让新窗口作为另一个进程窗口)

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class Demo(QMainWindow):
def __init__(self):
super().__init__()
test_button = QPushButton('test')
test_button.clicked.connect(self.onClick)
self.setCentralWidget(test_button)

def onClick(self):
# dlg = QDialog(self)
dlg = QMainWindow(self)
dlg.setWindowFlag(Qt.WindowStaysOnTopHint, False)
dlg.show()

app = QApplication([])
demo = Demo()
demo.show()
app.exec()

新窗口始终保留在主窗口上,我需要将窗口隐藏在它后面。

最佳答案

void QWidget::move(int x, int y)

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *


class Demo(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Main Window')
test_button = QPushButton('test')
test_button.clicked.connect(self.onClick)
self.setCentralWidget(test_button)

def onClick(self):
# dlg = QDialog(self)
dlg = QMainWindow(self)
dlg.setWindowTitle('Dialog Window')

dlg.move(self.geometry().x() + self.geometry().width() + 30, # <---
self.geometry().y() - 30)

dlg.setWindowFlag(Qt.WindowStaysOnTopHint, False)
dlg.show()

def sizeHint123(self):
return QSize(200, 200)

app = QApplication([])
demo = Demo()
demo.show()
app.exec()

enter image description here

<小时/>

更新

import sys
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *


class Demo(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle('Main Window')

self.setWindowFlags(Qt.WindowStaysOnTopHint) # +++

test_button = QPushButton('test')
test_button.clicked.connect(self.onClick)
self.setCentralWidget(test_button)

def onClick(self):
# dlg = QDialog(self)
self.dlg = QMainWindow() # --- self
self.dlg.setWindowTitle('Dialog Window')

self.dlg.move(self.geometry().x() + self.geometry().width() + 30,
self.geometry().y() - 30)

# dlg.setWindowFlag(Qt.WindowStaysOnTopHint, False)
self.dlg.show()

def sizeHint123(self):
return QSize(200, 200)

app = QApplication([])
demo = Demo()
demo.show()
app.exec()

enter image description here

关于python - 如何使对话框或弹出窗口可以保留在主窗口后面并且不覆盖主窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57195804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com