Trolltech Home | QSA-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 1

QSA-interest Archive, November 2006
Problems with return values of custom type


Message 1 in thread

Hi there,

I have a problem that seems to be rather basic, but I couldn't find
info about it on the net.
I have two classes that derive from QObject:

class DbQuery: public QObject {
   Q_OBJECT
   public slots:
      /* quite many slots, wrapping QSqlRecord */
};

class DbConnection: public QObject {
   Q_OBJECT
   public slots:
      bool connect();
      void disconnect();
      DbQuery exec(QString sql);
};

I create a DbConnection object in my program and add it to my
scripting-project with addObject.
This works fine and I can connect to the database. I can call
Application.dbconnection.exec("select * from bla"); too.
But when I try to do this:

var query = Application.dbconnection.exec("select * from bla");
while( query.next() ) {
// ...
}

query is undefined.
Any thought on this issue?

By the way, QSA is by far the easiest, most integrated and seamless
scripting-framework for C++ that I've seen so far. Really, great work!

Thanks,
   Daniel Albuschat

-- 
 [ signature omitted ] 

Message 2 in thread

Daniel Albuschat wrote:
> Hi there,
> 
> I have a problem that seems to be rather basic, but I couldn't find
> info about it on the net.
> I have two classes that derive from QObject:
> 
> class DbQuery: public QObject {
>   Q_OBJECT
>   public slots:
>      /* quite many slots, wrapping QSqlRecord */
> };
> 
> class DbConnection: public QObject {
>   Q_OBJECT
>   public slots:
>      bool connect();
>      void disconnect();
>      DbQuery exec(QString sql);
> };

You cannot return DbQuery as an object on the stack as QSA doesn't know 
how to generically treat these types. You have to return it as a pointer.

Btw... QObject has a private copy constructor because it cannot be 
copied so if you try the code above from C++ it shouldn't even compile ;-)

best regards,
Gunnar

To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx


Message 3 in thread

> 
> You cannot return DbQuery as an object on the stack as QSA doesn't know 
> how to generically treat these types. You have to return it as a pointer.
> 
> Btw... QObject has a private copy constructor because it cannot be 
> copied so if you try the code above from C++ it shouldn't even compile ;-)
> 

The way you say that it sounds like a great achievement that QSA accepts 
that code ;-) Wouldn't it be cool if QSA throws some kind of error along 
the way?


/eno

To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx


Message 4 in thread

Err, wrong reply-to header.
Here's the e-mail I've sent to Gunnar only, accidently.

---------- Forwarded message ----------
From: Daniel Albuschat <d.albuschat@xxxxxxxxx>
Date: 21.11.2006 19:49
Subject: Re: Problems with return values of custom type
To: Gunnar Sletta <gunnar@xxxxxxxxxxxxx>


2006/11/21, Gunnar Sletta <gunnar@xxxxxxxxxxxxx>:
> Daniel Albuschat wrote:
> > Hi there,
> >
> > I have a problem that seems to be rather basic, but I couldn't find
> > info about it on the net.
> > I have two classes that derive from QObject:
> >
> > class DbQuery: public QObject {
> >   Q_OBJECT
> >   public slots:
> >      /* quite many slots, wrapping QSqlRecord */
> > };
> >
> > class DbConnection: public QObject {
> >   Q_OBJECT
> >   public slots:
> >      bool connect();
> >      void disconnect();
> >      DbQuery exec(QString sql);
> > };
>
> You cannot return DbQuery as an object on the stack as QSA doesn't know
> how to generically treat these types. You have to return it as a pointer.
>
> Btw... QObject has a private copy constructor because it cannot be
> copied so if you try the code above from C++ it shouldn't even compile ;-)

I even tried it with pointers no, but it's not working that way either. I get "
TypeError. 'next' undefined or not a function"
with the following script code:

function Hello()
{
    var db = Application.db_connection;
    if( !db.connect("sysdba","5735","localhost","C:\\Data\\Data1.fdb","iso8859_1")
)
        debug(db.lastError());
    else {
        var qry = db.exec("select * from prot_maingroups");
        while( qry.next() ) {
        }
    }
}

Here's some C++ code as well. Enjoy:

class DbQuery: public QObject {
  Q_OBJECT
  private:
    QSqlQuery q;
  public:
    DbQuery() { }
    DbQuery(QSqlQuery q): q(q) { }
  public slots:
    int at () const { return q.at(); }
    void clear () { return q.clear(); }
    const QSqlDriver * driver () const { return q.driver(); }
    bool exec ( const QString & query ) { return q.exec(query); }
    bool exec () { return q.exec(); }
    QString executedQuery () const { return q.executedQuery(); }
    bool first () { return q.first(); }
    bool isActive () const { return q.isActive(); }
    bool isForwardOnly () const { return q.isForwardOnly(); }
    bool isNull ( int field ) const { return q.isNull(field); }
    bool isSelect () const { return q.isSelect(); }
    bool isValid () const { return q.isValid(); }
    bool last () { return q.last(); }
    QSqlError lastError () const { return q.lastError(); }
    QVariant lastInsertId () const { return q.lastInsertId(); }
    QString lastQuery () const { return q.lastQuery(); }
    bool next () { return q.next(); }
    int numRowsAffected () const { return q.numRowsAffected(); }
    bool prepare ( const QString & query ) { return q.prepare(query); }
    bool previous () { return q.previous(); }
    QSqlRecord record () const { return q.record(); }
    const QSqlResult * result () const { return q.result(); }
    bool seek ( int index, bool relative = false ) { return
q.seek(index,relative); }
    void setForwardOnly ( bool forward ) { return q.setForwardOnly(forward); }
    int size () const { return q.size(); }
    QVariant value ( int index ) const { return q.value(index); }
};

class DbConnection: public QObject {
  Q_OBJECT
    private:
      QSqlDatabase db;
    public:
      QSProject *pro;
      DbConnection();
    public slots:
      bool connect(QString user, QString pass, QString host, QString
file, QString charset);
      bool connected() const;
      void disconnect();
      QString lastError();
      DbQuery *exec(QString sql);
};

--
 [ signature omitted ] 

Message 5 in thread

Daniel Albuschat wrote:
> 
> I even tried it with pointers no, but it's not working that way
> either. I get " TypeError. 'next' undefined or not a function" with
> the following script code:

Sorry, forgot to mention that custom QObject types have to registered using:

http://doc.trolltech.com/qsa-1.2.1/qsinterpreter.html#registerMetaObject

I hope this makes it work.

best regards,
Gunnar

To unsubscribe - send "unsubscribe" in the subject to qsa-interest-request@xxxxxxxxxxxxx