Hello all,
I've been using PyQt and qt designer 3 quite a lot lately and was a bit bummed
that I couldn't use the integrated source editor when creating slots
connected to signals for various widgets. To overcome this, I hacked a bit
on pyuic so that it will include the code from various functions defined in
foo.ui.h when creating a python class from foo.ui. I don't know if this will
be helpful at all for others, but it's helped me a lot.
To use it, apply the patch to PyQt-3.2.4/pyuic/form.cpp
Then, in qt designer, when you need to edit a slot and the source
editor appears, enter python code between the curly braces (don't worry about
the correct starting indent level, each line is prepended with a correct
indentation) and then make sure that the ui.h file is in the same directory
as the ui file when using pyuic. Here's an example of one of my slots.
void DebMainWindowFrm::browsePushButtonClicked()
{
import os
done = 0
while not done:
filename = QFileDialog.getOpenFileName("*.deb", "", self,
"DebianPackageSelector", "Select Debian Package File (.deb)")
filename = str(filename).strip()
if filename == "":
return
if not os.path.exists(filename):
status = QMessageBox.critical(self, "Bad Filename", """<qt>
The filename you specified <b>%s</b> does not exist. Would
you like to
select another file
or cancel?</qt>""" % filename, "&Select File", "&Cancel",
None, 0, 1)
if status == 0:
continue
if status == 1:
break
self.debianPathLineEdit.setText(filename)
done = 1
}
If you use this, you'll need to turn off all of the fancy options for the C++
editor in designer. Let me know if anyone has problems.
-- Christian Bird
[EMAIL PROTECTED]
The diff is attached
*** pyuic.orig/form.cpp Mon Jun 17 09:35:42 2002
--- pyuic/form.cpp Mon Jun 17 04:23:49 2002
***************
*** 26,31 ****
--- 26,32 ----
#include <qstringlist.h>
#include <qfile.h>
#include <qfileinfo.h>
+ #include <qregexp.h>
#define NO_STATIC_COLORS
#include <globaldefs.h>
#include <zlib.h>
***************
*** 165,170 ****
--- 166,172 ----
nl = e.parentNode().toElement().elementsByTagName( "include" );
for ( i = 0; i < (int) nl.length(); i++ ) {
QDomElement n2 = nl.item(i).toElement();
+
QString s = n2.firstChild().toText().data();
if ( n2.attribute( "location" ) != "local" ) {
if ( s.right( 5 ) == ".ui.h" && !QFile::exists( s ) )
***************
*** 177,190 ****
// do the local includes afterwards, since global includes have priority on clashes
for ( i = 0; i < (int) nl.length(); i++ ) {
QDomElement n2 = nl.item(i).toElement();
QString s = n2.firstChild().toText().data();
if ( n2.attribute( "location" ) == "local" &&!globalIncludes.contains( s ) ) {
if ( s.right( 5 ) == ".ui.h" && !QFile::exists( s ) )
continue;
! if ( n2.attribute( "impldecl", "in implementation" ) != "in implementation" )
! continue;
localIncludes += s;
}
}
--- 179,236 ----
// do the local includes afterwards, since global includes have priority on clashes
for ( i = 0; i < (int) nl.length(); i++ ) {
+ printf("doing a local include!\n");
QDomElement n2 = nl.item(i).toElement();
QString s = n2.firstChild().toText().data();
+ cout << "including local file: " << s << endl;
if ( n2.attribute( "location" ) == "local" &&!globalIncludes.contains( s ) ) {
if ( s.right( 5 ) == ".ui.h" && !QFile::exists( s ) )
continue;
! if ( QFile::exists( s ) )
! {
! cout << "file exists" << endl;
! QFile f(s);
! f.open(IO_ReadOnly);
! QTextStream headerStream(&f);
! QString line;
! QString functionText;
! QString functionName;
! QRegExp rx("void .*::(.*\\(\\))");
! int pos, inFunction = 0;
! while (line = headerStream.readLine())
! {
! pos = rx.search(line);
! if (pos > -1)
! {
! cout << "found function: " << rx.cap(1) << endl;
! if (inFunction)
! {
! //cout << "Adding function " << functionName << " to functionImpls. Text is:\n";
! //cout << functionText << endl;
! functionImpls.insert( functionName, functionText);
! }
! functionName = rx.cap(1);
! functionText = "";
! inFunction = 1;
! }
! functionText += line + "\n";
! }
! if (inFunction)
! {
! //cout << "Adding function " << functionName << " to functionImpls. Text is:\n";
! //cout << functionText << endl;
!
! functionImpls.insert( functionName, functionText);
! }
!
! //QString headerText = headerStream.read();
! //cout << "text of the header:\n" << headerText << endl;
!
! }
! //if ( n2.attribute( "impldecl", "in implementation" ) != "in implementation" )
! //continue;
localIncludes += s;
+
}
}
***************
*** 764,773 ****
QMap<QString, QString>::Iterator fit = functionImpls.find( fname );
if ( fit != functionImpls.end() ) {
int begin = (*fit).find( "{" );
! QString body = (*fit).mid( begin + 1, (*fit).find( "}" ) - begin - 1 );
createWarning = body.simplifyWhiteSpace().isEmpty();
if ( !createWarning )
! out << *fit << endl;
}
if ( createWarning ) {
++indent;
--- 810,824 ----
QMap<QString, QString>::Iterator fit = functionImpls.find( fname );
if ( fit != functionImpls.end() ) {
int begin = (*fit).find( "{" );
! QString body = (*fit).mid( begin + 1, (*fit).findRev( "}" ) - begin - 1 );
createWarning = body.simplifyWhiteSpace().isEmpty();
if ( !createWarning )
! {
! ++indent;
! QString formatted_body = body.replace(QRegExp("\n"), QString("\n") + QString(indent));
! out << formatted_body << endl;
! --indent;
! }
}
if ( createWarning ) {
++indent;