iMath
iMath

Reputation: 2478

make the QMessageBox or QmainWindow in front of any overlapping sibling widgets?

If the active window belongs to some other process,how to make the QMessageBox or QmainWindow of this example in front of any overlapping sibling widgets when timeout ? I tried raise_() and activateWindow() ,but both don’t work on 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_())

Upvotes: 2

Views: 2073

Answers (1)

SpongeBobFan
SpongeBobFan

Reputation: 964

Try to modify window flags by using QWidget::setWindowFlags() method of your QMessageBox or QMainWindow. You should use Qt::WindowStaysOnTopHint flag for your purpose.
It will be something like window->setWindowFlags(window->windowFlags() | Qt::WindowStaysOnTopHint).
If you will not succeed with just setWindowFlags(window->windowFlags() | Qt::WindowStaysOnTopHint), you will need to use Qt::CustomizeWindowHint | Qt::WindowStaysOnTopHint with another flags. Experiment with it and you'll succeed.

Upvotes: 1

Related Questions