【问题标题】:make the QMessageBox or QmainWindow in front of any overlapping sibling widgets?在任何重叠的兄弟小部件前面制作 QMessageBox 或 QmainWindow?
【发布时间】:2013-03-28 22:30:51
【问题描述】:

如果活动窗口属于其他进程,超时时如何使本例的 QMessageBox 或 QmainWindow 在任何重叠的兄弟窗口小部件前面? 我试过 raise_(​​) 和 activateWindow() ,但都不能在 WinXP 上运行

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *

class MainWindow(QWidget):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)
        self.resize(800,600)

        self.lcdNumber = QLCDNumber()
        self.lcdNumber.setNumDigits(8)

        layout =  QVBoxLayout(self)
        layout.addWidget(self.lcdNumber)

        self.currentTime = QTime(0,0,0)
        self.lcdNumber.display(self.currentTime.toString('hh:mm:ss'))

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.updateLcdNumberContent)
        self.timer.start(1000)

    def updateLcdNumberContent(self):

        self.currentTime = self.currentTime.addSecs(1)
        self.lcdNumber.display(self.currentTime.toString('hh:mm:ss'))


        if self.currentTime == QTime(0,0,4) :
            msgBox = QMessageBox(self)
            msgBox.setWindowTitle('iTimer')
            msgBox.setIcon (QMessageBox.Information)
            msgBox.setText("Time Out !!")

            stopButton = msgBox.addButton("Stop", QMessageBox.ActionRole)
            ignoreButton = msgBox.addButton(QMessageBox.Ignore)

            stopButton.clicked.connect(self.timer.stop)


            msgBox.show()
#            self.raise_()
#            self.activateWindow()



if __name__ == '__main__':
    app =QApplication(sys.argv)
    frame = MainWindow()
    frame.show()
    sys.exit(app.exec_())

【问题讨论】:

    标签: qt pyqt pyside


    【解决方案1】:

    尝试使用 QMessageBox 或 QMainWindow 的QWidget::setWindowFlags() 方法修改窗口标志。您应该使用 Qt::WindowStaysOnTopHint 标志来达到您的目的。
    类似于window->setWindowFlags(window->windowFlags() | Qt::WindowStaysOnTopHint)
    如果仅使用setWindowFlags(window->windowFlags() | Qt::WindowStaysOnTopHint) 无法成功,则需要将Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint 与其他标志一起使用。尝试一下,你就会成功。

    【讨论】:

      猜你喜欢
      • 2021-01-14
      • 2013-05-26
      • 1970-01-01
      • 2021-07-11
      • 2019-08-27
      • 1970-01-01
      • 1970-01-01
      • 2011-08-24
      • 2013-05-03
      相关资源
      最近更新 更多