[PyKDE] about QPEApplication and QMainWindow

David Douard douard at magic.fr
Sun Aug 25 18:42:00 BST 2002


Hi !

Le Samedi 17 Août 2002 11:41, vous avez écrit :
> David Douard wrote:

<...snip...>

> >
> > Last, I use a QMainWindow derived class as main widget. But the problem
> > is that the QMainApplication does NOT declare slots which are used by
> > QApplication to send messages like "newDocument" or "accept". In the C++
> > world, you just have to subclass QMainApplication declaring thoses
> > fuctions... But if I subclass it with a python class, messages are not
> > transmited... I haven't looked deeply, but I  guess it is not possible
> > for a C++ class to send a message (at the c++ level. When I say send, I
> > mean calling the slot function as done in QPEApplication code for
> > "setDocument", for example) to a purely python defined slot... (Am I
> > wrong ?)
>
> Yes you are wrong. You can connect a C++ signal to any Python callable
> object (ie. a function or a method). If your Python slots are not being
> called then the problem is probably elsewhere.
>
> > In this case you (I mean Phil) should provide a QPEMainApplication class
> > with an associated  python  binding which just declare thoses required
> > slot functions as virtual, so user can redefine them at Python level. It
> > is what I have done for my little app, but it would be nice to have that
> > explained and implemented in PyQt...
>
> This isn't necessary.

Ok, so have you any idea why my code does not receive the setDocument messages 
?

The main widget (the one passed to QPEApp::showMainDocumentWidget), is derived 
from QMainWindow, with a defined setDocument function (which takes the doc 
name as arg).
For your info, this is an extract of the code in my (GPL) version of qtopia:
_________________________________________________________
qpeapplication.cpp:
void QPEApplication::pidMessage( const QCString &msg, const QByteArray & data)
{
...
...
    } else if ( msg == "setDocument(QString)" ) {
	d->keep_running = TRUE;
	QDataStream stream( data, IO_ReadOnly );
	QString doc;
	stream >> doc;
	QWidget *mw = mainWidget();
	if ( !mw )
	    mw = d->qpe_main_widget;
	if ( mw )
	    Global::setDocument( mw, doc );
    } else if ( msg == "nextView()" ) {
...
}

PS: global::setDocument is also called when starting the app, but it is in a 
very similar way.
_________________________________________________________
global.cpp:
void Global::setDocument( QWidget* receiver, const QString& document )
{
    Emitter emitter(receiver,document);
}

class Emitter : public QObject {
    Q_OBJECT
public:
    Emitter( QWidget* receiver, const QString& document )
    {
	connect(this, SIGNAL(setDocument(const QString&)),
	    receiver, SLOT(setDocument(const QString&)));
	emit setDocument(document);
	disconnect(this, SIGNAL(setDocument(const QString&)),
	       receiver, SLOT(setDocument(const QString&)));
    }

signals:
    void setDocument(const QString&);
};
__________________________________________________________

Again, when I am using the standard above code, I do not receive any 
"setDocument" message...


I have also written an almost empty QPEMainWindow class (and it's PyQT 
wrapper) like this :

__________________________________________________________
qpemainwindow.h:
class QPEMainWindow : public QMainWindow
{
  Q_OBJECT

public:
  QPEMainWindow( QWidget * parent = 0, const char * name = 0, WFlags f = 
WType_TopLevel ) : QMainWindow(parent, name, f);
  
public slots:
  virtual void setDocument(const QString &) {};
  virtual void accept() {};

};
____________________________________________________________

When I use this code (my main widget is then a subclass of QPEMainWindow), I 
DO receive all setDocument messages...
This is why I suggested you to provide this class as a part of QtPy...

Have you any idea why this code works, while the with the original 
QMainWindow, it does not work ?

Thanks,

David





More information about the PyQt mailing list