Unsolved 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 dlgFromPython.pyimport 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++ applicationGreetings
-
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.sue Thanks, but your answer is very short to my basic knowledge.
Some code sample will be more usefull for me.
Greeings -
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.
-
@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
-
This stack overflow post might give you some additional information.
-
@SGaist Thanks, I will learn about PythonQt and see what happend.
Greetings