Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Open a PyQt dialog from c++ QMainWindow
Forum Updated to NodeBB v4.3 + New Features

Open a PyQt dialog from c++ QMainWindow

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 3 Posters 2.4k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    marceloarguello700
    wrote on last edited by
    #1

    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 dlgFromPython.py

    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().__init__()
            #super(dlgDateTime, self).__init__(parent)
            layout = QVBoxLayout(self)
            # nice widget for editing the date
            self.datetime = QDateTimeEdit(self)
            self.datetime.setCalendarPopup(True)
            self.datetime.setDateTime(QDateTime.currentDateTime())
            layout.addWidget(self.datetime)
    
            # OK and Cancel buttons
            self.buttons = QDialogButtonBox(
                QDialogButtonBox.Ok | QDialogButtonBox.Cancel,
                Qt.Horizontal, self)
            layout.addWidget(self.buttons)
    
            self.buttons.accepted.connect(self.accept)
            self.buttons.rejected.connect(self.reject)
    
        # get current date and time from the dialog
        def dateTime(self):
            return self.datetime.dateTime()
    
    def fTestPrint():
        print("fTestPrint")
    		
    def fOpenDlg():
        print("fOpenDlg")
        app = QApplication(sys.argv)
        dlg = dlgDateTime()
        dlg.exec_()
    	
    if __name__ == '__main__':
        fTestPrint()
        fOpenDlg()
    

    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
      Py_Initialize();
      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 );
    
      Py_Finalize();
    }
    

    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);
    dlg.exec();
    

    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

    Greetings

    1 Reply Last reply
    0
    • m.sueM Offline
      m.sueM Offline
      m.sue
      wrote on last edited by
      #2

      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.
      -Michael.

      M 1 Reply Last reply
      1
      • m.sueM m.sue

        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.
        -Michael.

        M Offline
        M Offline
        marceloarguello700
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          0
          • SGaistS SGaist

            Hi,

            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.

            M Offline
            M Offline
            marceloarguello700
            wrote on last edited by
            #5

            @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.

            Greetings

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              This stack overflow post might give you some additional information.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              M 1 Reply Last reply
              0
              • SGaistS SGaist

                This stack overflow post might give you some additional information.

                M Offline
                M Offline
                marceloarguello700
                wrote on last edited by
                #7

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

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved