Python2.4 und Qt ui-files

Python und das Qt-Toolkit, erstellen von GUIs mittels des Qt-Designers.
Antworten
fg_psycho
User
Beiträge: 1
Registriert: Freitag 21. Juli 2006, 10:19

Hallo,

ich habe eine bereits fertige Applikation unter python2.2 und Qt3.05 am Laufen. Nun möchte ich auf python2.4 und Qt3.3.4 upgraden und laufe in folgendes Problem:

Das include-file Python.h muss wohl neuerdings als erstes file - noch vor den Systemincludes - inkludiert werden, anderenfalls bekomme ich die Meldung:
In file included from /usr/include/python/Python.h:8,
from ...
from ...
/usr/include/python/pyconfig.h:835:1: warning: "_POSIX_C_SOURCE" redefined
In selbst geschriebenen Code mag das vielleicht ja noch gehen, aber was passiert mit dynamisch erzeugtem code aus den Qt .ui-files? Dort kann man zwar mit:
<includes>
<include location="local" impldecl="in declaration">Python.h</include>
</includes>
das include angeben, jedoch wird dieses immer nach den System-Includes angezogen und ist somit nutzlos. Kann man die Reihenfolge der includes irgendwo angeben, oder gibt es für python2.4 andere Make-Tools? Für Vorschläge wäre sehr dankbar.

Dank im Voraus
JR
User
Beiträge: 286
Registriert: Montag 20. Februar 2006, 16:43
Wohnort: Berlin

Liebe Pythongemeinde,

das gleiche Problem habe ich auch.

Hat jemand einen Rat?

Ich schreibe in C++/ Qt

Folgendes lässt sich übersetzen:

Code: Alles auswählen

#include "python3.2m/Python.h"

#include <QtCore>
#include <QCoreApplication>
#include <QDebug>

int main( int argc, char *argv[] ) {
	return 0;
}
Folgendes aber nicht!

Code: Alles auswählen

#include <QtCore>
#include <QCoreApplication>
#include <QDebug>

#include "python3.2m/Python.h"

int main( int argc, char *argv[] ) {
	return 0;
}
Gleiche Problematik und problematisch ist es tatsächlich, da ich ebenso mit einer IDE arbeite. Genau gesat mit Eclipse CDT inklusive Qt-Plugin.

Wer hat eine Lösung, Erfahrung mit demselben Problem??

Liebe Grüße
JR
BlackJack

@JR: Ist ignorieren der Warnung eine Option!?
JR
User
Beiträge: 286
Registriert: Montag 20. Februar 2006, 16:43
Wohnort: Berlin

Hi!

Das ist leider keine Warnung, sondern ein richtiger Compilerfehler:

Code: Alles auswählen

g++ -c -pipe -O0 -O3 -g -Wall -W -D_REENTRANT -D_UBUNTU -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I../../../lib/Cpp/Python-3.2.2/include -Idebug -o debug/main.o main.cpp
In file included from ../../../lib/Cpp/Python-3.2.2/include/python3.2m/Python.h:8,
                 from main.cpp:5:
../../../lib/Cpp/Python-3.2.2/include/python3.2m/pyconfig.h:1173:1: warning: "_POSIX_C_SOURCE" redefined
In file included from /usr/include/string.h:26,
                 from /usr/include/qt4/QtCore/qbytearray.h:48,
                 from /usr/include/qt4/QtCore/qstring.h:46,
                 from /usr/include/qt4/QtCore/qobject.h:48,
                 from /usr/include/qt4/QtCore/qabstracteventdispatcher.h:45,
                 from /usr/include/qt4/QtCore/QtCore:3,
                 from main.cpp:1:
/usr/include/features.h:158:1: warning: this is the location of the previous definition
In file included from ../../../lib/Cpp/Python-3.2.2/include/python3.2m/Python.h:8,
                 from main.cpp:5:
../../../lib/Cpp/Python-3.2.2/include/python3.2m/pyconfig.h:1195:1: warning: "_XOPEN_SOURCE" redefined
In file included from /usr/include/string.h:26,
                 from /usr/include/qt4/QtCore/qbytearray.h:48,
                 from /usr/include/qt4/QtCore/qstring.h:46,
                 from /usr/include/qt4/QtCore/qobject.h:48,
                 from /usr/include/qt4/QtCore/qabstracteventdispatcher.h:45,
                 from /usr/include/qt4/QtCore/QtCore:3,
                 from main.cpp:1:
/usr/include/features.h:160:1: warning: this is the location of the previous definition
In file included from ../../../lib/Cpp/Python-3.2.2/include/python3.2m/Python.h:52,
                 from main.cpp:5:
../../../lib/Cpp/Python-3.2.2/include/python3.2m/pyatomic.h:59: warning: unused parameter ‘address’
In file included from ../../../lib/Cpp/Python-3.2.2/include/python3.2m/Python.h:67,
                 from main.cpp:5:
../../../lib/Cpp/Python-3.2.2/include/python3.2m/object.h:402: error: expected unqualified-id before ‘;’ token
main.cpp:7: warning: unused parameter ‘argc’
main.cpp:7: warning: unused parameter ‘argv’
make[1]: Verlasse Verzeichnis '/home/JR/Development/Workspaces/Qt/PythonEmbedding'
make[1]: *** [debug/main.o] Fehler 1
make[1]: Das Target »first« wurde wegen Fehlern nicht aktualisiert.
make: *** [debug] Fehler 2
BlackJack

@JR: *Das* ist aber ein anderer Fehler als im ersten Beitrag von diesem Thema. Hellsehen können wir nicht — das musst Du also schon sagen.

Warum importierst Du es nicht in der unproblematischen Reihenfolge? Und jetzt sag nicht Deine IDE lässt Dich das nicht tun.
JR
User
Beiträge: 286
Registriert: Montag 20. Februar 2006, 16:43
Wohnort: Berlin

Ja, diesen Weg beschreite ich jetzt.
Also schlanke extra Klasse, die möglichst nur den Python-Header verwendet.
So geht es erstmal.
Schick, Python zu embedden :)

Nur ein als Datei vorliegendes Skript bekomme ich noch nicht gerufen.

Falls das jemand mal braucht:
Aufruf/ Benutzen des Python-Interpreters von C++/ Qt aus:

Code: Alles auswählen

// Attention! The #include of Python.h does not work,
// if one includes <QtCore> and many other Qt header before this include.
#include "python3.2m/Python.h"

#include <QDebug>
#include <QList>
#include <QString>

int executeSimpleString();
int executeSimpleStringAndGetResult();

int main() {
	executeSimpleStringAndGetResult();
//	executeSimpleString();
}
//---------------------------------------------------------------------------------------------------------------------
int executeSimpleStringAndGetResult() {
	Py_Initialize();
	QString s = "import math\n";
	s.append( "result = math.sin( 0.5 * math.pi )\n" );
	s.append( "print( type( result ) )\n" );

	const char* charS = s.toAscii().data();
	PyRun_SimpleStringFlags( charS, 0 );
	PyObject * module = PyImport_AddModule( "__main__" );
	if ( module > 0 ) {
		PyObject * dictionary = PyModule_GetDict( module );
		if ( dictionary > 0 ) {
			PyObject * result = PyDict_GetItemString( dictionary, "result" );
			if ( result > 0 ) {
				double res = PyFloat_AsDouble( result );
				qDebug() << res;
			}
		}
	}
	Py_Finalize();
	return 0;
}
//---------------------------------------------------------------------------------------------------------------------
int executeSimpleString() {
	Py_Initialize();
	QString s = "import math\n";
	s.append( "for x in range(1, 11):\n" );
	s.append( "  res = math.pow( x, 2 )\n" );
	s.append( "  print( 'x = ' + str( x ) + ' res = ' + str( res ) )\n" );

	const char* charS = s.toAscii().data();
	PyRun_SimpleStringFlags( charS, 0 );

	Py_Finalize();
	return 0;
}
Das Qt-Projektfile benötigt folgenden Eintrag (natürlich angepasst):

Code: Alles auswählen

    INCLUDEPATH += $$quote(/home/JR/Development/lib/Cpp/Python-3.2.2/include)
    LIBS += -L$$quote($$/home/JR/Development/lib/Cpp/Python-3.2.2/lib) -lpython3.2m
Und beim Ausführen des Executables (z.B. namens "PythonEmbedding") muss die Umgebungsvariable LD_LIBRARY_PATH den Verzeichnispfad zum Directory lib enthalten.
Im vorliegenden Beispiel also folgendes Shellskript zum Starten der Mini-App:

Code: Alles auswählen

export LD_LIBRARY_PATH=/home/JR/Development/lib/Cpp/Python-3.2.2/lib
./PythonEmbedding
Viele Grüße
JR

P.S.: Hilfreich ist folgender Link zur C/C++ API von Python: http://docs.python.org/py3k/genindex.html
Antworten