PDA

View Full Version : Is there is shareable QFloatArray1D?



lni
27th January 2009, 15:39
This QFloatArray1D should look like


class QFloatArray1D {
public:
QFloatArray1D( float* valueArray, int length );
...
const float* array( int* length ) const;
void setArray( float* valueArray, int length );
float valueAt( int idx ) const;
..
};

The reason is that I need to create a MyGraphicsTraceItem as



class MyGraphicsTraceItem : public QGraphicsItem
{
public:

MyGraphicsTraceItem( float* traceData,
int traceLength,
float traceIndexFrom,
float indexDelta,
QGraphicsItem *parent = 0 );

private:

float* theTraceData;
int theTraceLength;
float theTraceIndexFrom;
float theTraceIndexDelta;

};


But I feel something is wrong with this code, so I would wish to have a constructor like



public:
MyGraphicsTraceItem( const QFloatArray1D& traceData,
float traceIndexFrom,
float indexDelta,
QGraphicsItem *parent = 0 );
private:

QFloatArray1D theTraceData; // <== explicitly shared
float theTraceIndexFrom;
float theTraceIndexDelta;


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

Thanks

jpn
27th January 2009, 16:17
You can use QVector:


QVector<float> traceData;
traceData.push_back(...);

MyGraphicsTraceItem* traceItem = new MyGraphicsTraceItem(traceData.data(), ...);



Also, what does Q_DECLARE_PRIVATE do, and how can I use it?
See our wiki: Private implementation

lni
27th January 2009, 16:28
You can use QVector:


QVector<float> traceData;
traceData.push_back(...);

MyGraphicsTraceItem* traceItem = new MyGraphicsTraceItem(&traceData[0], ...=;



See our wiki: Private implementation

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?

jpn
27th January 2009, 16:37
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.

lni
27th January 2009, 16:52
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...

jpn
27th January 2009, 16:58
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.

lni
27th January 2009, 17:31
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?