Important: Please read the Qt Code of Conduct -

Open a PyQt dialog from c++ QMainWindow

  • Hi, I have a program write in C++, is a QMainWindow,
    wich some QDialogs also writen in C++, now i need to
    use some code packages from python.

    Then i want to open a PyQt5 QDialog from c++,
    The python file is named

    import sys
    from PyQt5.QtWidgets import (QDialog, QVBoxLayout, QDialogButtonBox, QDateTimeEdit, QApplication)
    from PyQt5.QtCore import Qt, QDateTime
    from PyQt5 import QtCore, QtWidgets
    class dlgDateTime(QDialog):
        def __init__(self, parent = None):
            #super(dlgDateTime, self).__init__(parent)
            layout = QVBoxLayout(self)
            # nice widget for editing the date
            self.datetime = QDateTimeEdit(self)
            # OK and Cancel buttons
            self.buttons = QDialogButtonBox(
                QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
                Qt.Horizontal, self)
        # get current date and time from the dialog
        def dateTime(self):
            return self.datetime.dateTime()
    def fTestPrint():
    def fOpenDlg():
        app = QApplication(sys.argv)
        dlg = dlgDateTime()
    if __name__ == '__main__':

    When I run the script from IDLE it work ok, the dialog appear.
    But when i try from c++ the program crash or nothing happen.

    void dlgPythonTest::sltTest3()
      // initialize python
      PyObject* mMainModule = PyImport_AddModule( "__main__" ); 
      PyObject* mMainDict = PyModule_GetDict( mMainModule );
       PyRun_String("import sys",Py_single_input, mMainDict, mMainDict );
    // Next call work
       PyRun_String("from time import time, ctime\n",Py_single_input, mMainDict, mMainDict );
       PyRun_SimpleString("print ('Today is')");
       PyRun_String("print (ctime())",Py_single_input, mMainDict, mMainDict );
       PyRun_String("sys.path.append(\"D:/python/\")",Py_single_input, mMainDict, mMainDict );
       PyRun_String("import dlgFromPython",Py_single_input, mMainDict, mMainDict );
       //Call to fTestPrint() Work
       PyRun_String("dlgFromPython.fTestPrint()",Py_single_input, mMainDict, mMainDict );
       //Call to fOpenDlg() nothing happen
       PyRun_String("dlgFromPython.fOpenDlg()",Py_single_input, mMainDict, mMainDict );

    Also when open a c++ dialog from my c++ QMainWindow
    the reference to the QMainWindow is passed to the constructor of the dialog:

    dlgVectorLayerColorScheme dlg(this);

    How can do that in the python dialog
    Also in necessary to create another QApplication in the python dialog?
    Wich is the propper way to open a dialog from my c++ application


  • Hi,
    when I use Python from within my C++ program I show the dialog from within the C++ program directly and afterwards provide the Python program with the contents of the dialog via pipe or config file.

  • @m.sue Thanks, but your answer is very short to my basic knowledge.
    Some code sample will be more usefull for me.

  • Lifetime Qt Champion


    It seems you already know how to write a dialog with Qt.

    What @m-sue suggested is that you rewrite your PyQt dialog in C++ and then pass whatever you want from the dialog as parameter to your python script.

  • @SGaist Thanks, now understand what @m.sue say, c++ is my prefered way to write dialogs, so i agree that passing parameters will be a more easy aproach.

    But i only was analyzing and exploring how interact with python.

    There is a gis sofware named QGIS that use many plugins writed in python,
    with the respective dialogs also in Python. I read some of its code, but i
    is complex and just want a few dialogs not a entire plugin management system.
    Also in the web the examples of integration are very basics.


  • Lifetime Qt Champion

    This stack overflow post might give you some additional information.

  • @SGaist Thanks, I will learn about PythonQt and see what happend.

Log in to reply