Results 1 to 7 of 7

Thread: Is there is shareable QFloatArray1D?

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    8
    Thanked 18 Times in 17 Posts

    Default Is there is shareable QFloatArray1D?

    This QFloatArray1D should look like
    Qt Code:
    1. class QFloatArray1D {
    2. public:
    3. QFloatArray1D( float* valueArray, int length );
    4. ...
    5. const float* array( int* length ) const;
    6. void setArray( float* valueArray, int length );
    7. float valueAt( int idx ) const;
    8. ..
    9. };
    To copy to clipboard, switch view to plain text mode 

    The reason is that I need to create a MyGraphicsTraceItem as

    Qt Code:
    1. class MyGraphicsTraceItem : public QGraphicsItem
    2. {
    3. public:
    4.  
    5. MyGraphicsTraceItem( float* traceData,
    6. int traceLength,
    7. float traceIndexFrom,
    8. float indexDelta,
    9. QGraphicsItem *parent = 0 );
    10.  
    11. private:
    12.  
    13. float* theTraceData;
    14. int theTraceLength;
    15. float theTraceIndexFrom;
    16. float theTraceIndexDelta;
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 

    But I feel something is wrong with this code, so I would wish to have a constructor like
    Qt Code:
    1. public:
    2. MyGraphicsTraceItem( const QFloatArray1D& traceData,
    3. float traceIndexFrom,
    4. float indexDelta,
    5. QGraphicsItem *parent = 0 );
    6. private:
    7.  
    8. QFloatArray1D theTraceData; // <== explicitly shared
    9. float theTraceIndexFrom;
    10. float theTraceIndexDelta;
    To copy to clipboard, switch view to plain text mode 

    Also, what does Q_DECLARE_PRIVATE do, and how can I use it?

    Thanks

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Is there is shareable QFloatArray1D?

    You can use QVector:
    Qt Code:
    1. QVector<float> traceData;
    2. traceData.push_back(...);
    3.  
    4. MyGraphicsTraceItem* traceItem = new MyGraphicsTraceItem(traceData.data(), ...);
    To copy to clipboard, switch view to plain text mode 

    Also, what does Q_DECLARE_PRIVATE do, and how can I use it?
    See our wiki: [WIKI]Private implementation[/WIKI]
    Last edited by jpn; 27th January 2009 at 17:37.
    J-P Nurmi

  3. #3
    Join Date
    Dec 2006
    Posts
    426
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    8
    Thanked 18 Times in 17 Posts

    Default Re: Is there is shareable QFloatArray1D?

    Quote Originally Posted by jpn View Post
    You can use QVector:
    Qt Code:
    1. QVector<float> traceData;
    2. traceData.push_back(...);
    3.  
    4. MyGraphicsTraceItem* traceItem = new MyGraphicsTraceItem(&traceData[0], ...=;
    To copy to clipboard, switch view to plain text mode 


    See our wiki: [WIKI]Private implementation[/WIKI]
    Using QVector<float> will duplicate my data? I already have a float* array in memory somewhere in my application...

    In QGraphicsLineItem, the QGraphicsLineItemPrivate is derived from QGraphicsItemPrivate, How can I make my MyGraphicsTraceItemPrivate do the same? File qgraphicsitem_p.h is not published so I assume we are not supposed to do it, but then why QGraphicsLineItemPrivate can do it?

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Is there is shareable QFloatArray1D?

    Quote Originally Posted by lni View Post
    Using QVector<float> will duplicate my data? I already have a float* array in memory somewhere in my application...
    Why not make this code "somewhere in your application" use QVector? Then you can pass a C-array of floats to the legacy code (I suppose you don't use C-arrays just for fun) but still have all the benefits of a nice container class.

    In QGraphicsLineItem, the QGraphicsLineItemPrivate is derived from QGraphicsItemPrivate, How can I make my MyGraphicsTraceItemPrivate do the same? File qgraphicsitem_p.h is not published so I assume we are not supposed to do it, but then why QGraphicsLineItemPrivate can do it?
    It's internal implementation of Qt you are not supposed to care about. You are not supposed to do the same in your application. In general, pimpl doesn't give that much benefit in application projects... The link I gave explains what it's about. Please read it.
    J-P Nurmi

  5. #5
    Join Date
    Dec 2006
    Posts
    426
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    8
    Thanked 18 Times in 17 Posts

    Default Re: Is there is shareable QFloatArray1D?

    Quote Originally Posted by jpn View Post
    Why not make this code "somewhere in your application" use QVector? Then you can pass a C-array of floats to the legacy code (I suppose you don't use C-arrays just for fun) but still have all the benefits of a nice container class.
    Interesting... didn't notice QVector can return C-array..But why not also provide a method for QVector to take C-array, such as

    QVector::QVector( int size, T* array ), where QVector take the ownership (or not) for the input array? My C++ will call C which returns me the array...

    Existing codes are "old", but if it takes C-array, we probably don't need as much effort...

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts

    Default Re: Is there is shareable QFloatArray1D?

    Quote Originally Posted by lni View Post
    Interesting... didn't notice QVector can return C-array..
    Yes, QVector stores its items in adjacent memory locations.

    But why not also provide a method for QVector to take C-array
    I think you can do it with QVarLengthArray, which is more low-level than QVector. But I'd get rid of the C-array in the first place.

    Existing codes are "old", but if it takes C-array, we probably don't need as much effort...
    That's the nice thing with QVector. You can easily pass the data to a legacy C function.
    J-P Nurmi

  7. #7
    Join Date
    Dec 2006
    Posts
    426
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    8
    Thanked 18 Times in 17 Posts

    Default Re: Is there is shareable QFloatArray1D?

    Quote Originally Posted by jpn View Post
    I think you can do it with QVarLengthArray, which is more low-level than QVector. But I'd get rid of the C-array in the first place.
    Very good, thanks!

    But is there a reason that I don't know why QVector can't take c-array in the constructor, assuming the QVector will take the ownership and will free the array when done?

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
  •  
Qt is a trademark of The Qt Company.