Announcement

Collapse
No announcement yet.

Pointerprob. in C++

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Pointerprob. in C++

    Yet again, another pointer issue , these things really bother me

    Could someone please explain, what's wrong with the code in the while-loop ?
    What do I have to (add/remove) to get this to work ?
    The first line seems to work fine, it returns the name of the field in the Database, the second one returns nada, zero, zip, squad, nothing, while it should return the contents of that field.

    void mainWindow::Query()
    {
    QSqlQuery *query;
    QSqlRecord *record;

    query = new QSqlQuery ( "SELECT name, surname FROM user" );
    record = new QSqlRecord();

    *record = query->record();

    while ( query->next() )
    {

    // This line works and returns the name of the field in the database.
    QMessageBox::information ( this, "Field", record->fieldName ( 1 ) );


    // This line doesn't and should return the contents or that field.
    QMessageBox::information ( this, "Field contents", record->value ( 1 ).toString() );

    }

    delete record;
    delete query;
    record = 0, query = 0;
    }
    TIA
    Acer Aspire 5100<br />Kubuntu Hardy 8.04

    #2
    Re: Pointerprob. in C++

    Try moving "*record = query->record();" inside your loop. You are setting your record before the query is pointed to the first entry and the record is never getting updated to the current entry inside your loop.

    I don't think pointers are necessary here. I think the Qt objects are probably set up to store data on the heap if they need to. I can see the query object storing its data on the heap because of the likelyhood of having a huge result. I don't think the record object would store data on the heap. It would have to be a real high field count to make it sensible. I would run without pointers and the dynamically allocated memory until I had issues like a stack overflow (which I doubt you will).

    Qt needs the use of new quite often in creating objects, but this is mainly in the gui elements. As a rule of thumb (yes there are exceptions) use new when the constructor has a parameter for QObject *parent. Qt assistant has a lot of examples and is very handy.


    FKA: tanderson

    Comment


      #3
      Re: Pointerprob. in C++

      Thx for the reply.....but this is actually a small test-project to work out things with pointers and to get a grip on them . Nothing really important. I'll give it a shot and see what happens.
      Acer Aspire 5100<br />Kubuntu Hardy 8.04

      Comment

      Working...
      X