Results 1 to 3 of 3

Thread: Pass pointer on Qlist as function's parameter

  1. #1
    Join Date
    Feb 2016
    Posts
    13
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Pass pointer on Qlist as function's parameter

    Hi everybody!

    I have a structure and array in QList :

    struct TRadioSounding {
    QString station_code; // station_code is a 5 digital code, for example 41217
    QString station_name; // station_name is a 4 character code
    float station_latitude; // station location
    float station_longitude; // station location
    float station_elevation; // station location
    QDateTime datetime; // data and day time of the radio sounding
    bool fitting; // flag showing that the current sounding corresponds to parameters of processing analysis
    };


    typedef QList<TRadioSounding> TRadioSoundings;

    ----------------------------------------------------------------
    then I want to send pointer on QList th the function and change internal fields of some elements of QList array:


    int process_all_radiousoundings(TRadioSoundings *soundings)
    {
    int count_fitting_soundings = 0; // number of soundings fitting to the given characteristics

    for (int i = 0; i < soundings->count(); i++)
    {

    soundings[i]->fitting = true;
    }
    }


    But I got an error :

    error: 'TRadioSoundings {aka class QList<TRadioSounding>}' has no member named 'fitting'
    soundings[i].fitting = true;
    ^


    So how can I overpass it or correct my code ?


    Added after 55 minutes:


    int process_all_radiousoundings(TRadioSoundings & soundings)
    {

    soundings[i].fitting = true;
    Last edited by abshaev; 7th February 2018 at 15:03.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Pass pointer on Qlist as function's parameter

    That would be good
    Qt Code:
    1. soundings[i]->fitting = true;
    To copy to clipboard, switch view to plain text mode 
    if
    Qt Code:
    1. typedef QList<*TRadioSounding> TRadioSoundings;
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Pass pointer on Qlist as function's parameter

    soundings[i]->fitting = true;
    No. In the OP's code, TSoundings is a QList of TSounding instances. He is passing a pointer to this QList to his method. Inside the method, the proper way to access members of the QList using QList::operator[]() would be:

    Qt Code:
    1. ((*settings)[i]).fitting = true;
    2.  
    3. // or
    4.  
    5. (settings->operator[]( i )).fitting = true;
    To copy to clipboard, switch view to plain text mode 

    In the first case, *settings turns the pointer into a reference to a QList and therefore you can use operator[] directly. In the second case, settings remains a pointer to a QList, so the operator[] must be accessed using pointer syntax.

    This syntax:

    Qt Code:
    1. settings[i]
    To copy to clipboard, switch view to plain text mode 

    says that the argument is an array of QList instances (just like you would write "double * x" to point to an array of doubles) so that syntax says to return the i-th QList in the array. This will cause an access violation for any value of i beyond zero.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Pass a parameter to new QWidget
    By Blitzor DDD in forum Qt Programming
    Replies: 7
    Last Post: 30th July 2016, 04:00
  2. Replies: 8
    Last Post: 23rd December 2012, 17:50
  3. how to pass a QList pointer
    By ehnuh in forum Qt Programming
    Replies: 2
    Last Post: 5th November 2012, 16:30
  4. Replies: 14
    Last Post: 1st December 2009, 20:45
  5. Replies: 6
    Last Post: 4th April 2006, 07:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.