Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

[Solved] PyQt Dialog Window Not Popping up in Function



  • Hi,

    I've spent many days trying to figure this out...

    My dialog window is not popping up when being called from inside a function.

    I've done this before & have been successful, but this particular one is not working.

    Could there be a setting I messed up or missed?

    This is my code:

    @from PyQt4 import QtGui, uic, QtCore, Qt

    def MAIN():
    widget = uic.loadUi("/PATH/TO/file.ui")
    widget.show()

    MAIN()@

    (I tried to simplify it as much as possible, but nothing works...)

    But it works if I do this:

    @from PyQt4 import QtGui, uic, QtCore, Qt

    widget = uic.loadUi("/PATH/TO/file.ui")
    widget.show()
    @

    Any ideas? Thanks!!

    If the ui code would be useful, please let me know.



  • I've also replaced the dialog ui file for a window ui file & same thing...This is quite puzzling.

    Any help would be appreciated!!



  • Try the following with your .ui file:

    @
    from PyQt4.QtGui import *
    from PyQt4 import uic

    class MainWindow(QMainWindow):
    def init(self, parent=None):
    QMainWindow.init(self, parent)

        self.ui.uic.loadUi("./example.ui")
        self.ui.show()
    

    if name=="main":
    from sys import argv, exit
    a=QApplication(argv)
    m=MainWindow()
    exit(a.exec_())
    @

    It tends to be better to have an enclosing widget (QWidget, QMainWindow etc.). I don't use UI files very often but this is what I always do.

    Hope it helps.



  • Hi,

    Thanks for your reply, but is there any way to do it w/o classes/OOP?



  • Try this:

    @
    from PyQt4.QtGui import *
    from PyQt4 import uic
    from sys import argv, exit

    def Main():
    a=QApplication(argv)
    ui=uic.loadUi("./example.ui")
    ui.show()
    return a.exec_()

    Main()
    @



  • Thanks again. I'm doing this inside another program, called Autodesk Maya 2012/2013, so that just crashes it with this error message:

    QCoreApplication::exec: The event loop is already running
    QClipboard: Unable to receive an event from the clipboard manager in a reasonable time
    Segmentation fault (core dumped)



  • why would you call Main() twice?




Log in to reply