Results 1 to 12 of 12

Thread: QShared

  1. #1
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QShared

    Hi,
    I have a code in QT-3, and it is like this,

    ================================================== ================
    class CVideoFrame
    {
    friend class CVideoDevice;
    private:
    struct VideoDataShared: public QShared
    {
    const QImage *RGB;
    const QImage *Y, *U, *V;

    unsigned long sequence;
    unsigned long time_stamp;
    } *data;

    }

    CVideoFrame::CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v)
    {
    data = new VideoDataShared;
    if (data == 0)
    return;

    data->RGB = rgb;
    data->Y = y;
    data->U = u;
    data->V = v;
    data->sequence = 0;
    data->time_stamp = 0;
    m_Number = number;
    }



    CVideoFrame::CVideoFrame(const CVideoFrame &f)
    {
    data = f.data;
    if (data != 0)
    {
    data->ref();
    }
    }
    ================================================== ===============
    and it is working.

    I need to write this cod in QT-4.3 and I try it like this,

    ================================================== ================

    struct Shared
    {
    Shared() : count(1) {}
    void ref() { ++count; }
    bool deref() { return !--count; }
    uint count;
    };


    class CVideoFrame
    {

    friend class CVideoDevice;
    private:
    struct VideoDataShared: public Shared
    {
    const QImage *RGB;
    const QImage *Y, *U, *V;

    unsigned long sequence;
    unsigned long time_stamp;
    } *data;

    }
    CVideoFrame::CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v)
    {
    data = new VideoDataShared;
    if (data == 0)
    return;

    data->RGB = rgb;
    data->Y = y;
    data->U = u;
    data->V = v;
    data->sequence = 0;
    data->time_stamp = 0;
    m_Number = number;
    }


    CVideoFrame::CVideoFrame(const CVideoFrame &f)
    {
    data = f.data;
    if (data != 0)
    {
    data->ref();
    }
    }

    ================================================== =============
    and I can make this program withut error. But my program is not working properly. I think, this section have some error.

    CVideoFrame::CVideoFrame(const CVideoFrame &f)
    {
    data = f.data;
    if (data != 0)
    {
    data->ref();
    //I think this line is not working.....
    }
    }

    please help me to solve this problem.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QShared

    I don't think you should implement "Shared" yourself. You probably want to use QSharedData.

  3. #3
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QShared


  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QShared

    "If possible, we recommend that you use QSharedData and QSharedDataPointer instead."

  5. #5
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QShared

    Hi,

    I modify my program like this,

    Qt Code:
    1. #include <QSharedData>
    2.  
    3. class CVideoFrame
    4. {
    5. friend class CVideoDevice;
    6. private:
    7. struct VideoDataShared: public QSharedData
    8. {
    9. const QImage *RGB;
    10. const QImage *Y, *U, *V;
    11. unsigned long sequence;
    12. unsigned long time_stamp;
    13. } *data;
    14. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui>
    2. #include "VideoFrame.h"
    3.  
    4. CVideoFrame::CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v)
    5. {
    6.  
    7. data = new VideoDataShared;
    8. if (data == 0)
    9. return;
    10.  
    11. data->RGB = rgb;
    12. data->Y = y;
    13. data->U = u;
    14. data->V = v;
    15. data->sequence = 0;
    16. data->time_stamp = 0;
    17. m_Number = number;
    18. }
    19.  
    20.  
    21. CVideoFrame::CVideoFrame(const CVideoFrame &f)
    22. {
    23. data = f.data;
    24. if (data != 0)
    25. {
    26. data->ref;
    27. }
    28. }
    29.  
    30.  
    31. uint CVideoFrame::GetRefCount() const
    32. {
    33. if (data != 0)
    34. return 0;
    35. return data->count;
    36. }
    To copy to clipboard, switch view to plain text mode 

    But when i try to make the program, it display an error message like this,

    In member function ‘uint CVideoFrame::GetRefCount() const’:
    error: ‘struct CVideoFrame::VideoDataShared’ has no member named ‘count’

    how can I solve this?
    Please help me
    Last edited by wysota; 10th October 2007 at 11:49. Reason: missing [code] tags

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QShared

    Your class doesn't have a "count" member, so you can't call it. And you don't need it with implicit sharing. You can't just "translate" your application from using Qt3 classes into using Qt4 classes as Qt3 and Qt4 use different concepts when it comes to data sharing. You have to redesign your class.

  7. #7
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QShared

    Hi,
    I am not expert in QT. I am trying to learn. I get a program from net and it is QT-3. I try to create in QT-4.3. Please help me to solve my problem. This code is from Qt-3 and I need to recreate it in QT-4.3. Please help me

    code in QT-3
    ================================================== =====
    Qt Code:
    1. #include <qimage.h>
    2. #include <qshared.h>
    3. class CVideoFrame
    4. {
    5. friend class CVideoDevice;
    6. private:
    7. struct VideoDataShared: public QShared
    8. {
    9. const QImage *RGB;
    10. const QImage *Y, *U, *V;
    11. unsigned long sequence;
    12. unsigned long time_stamp;
    13. } *data;
    14. uint m_Number;
    15. private:
    16. uint GetRefCount() const;
    17. protected:
    18. CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v);
    19. public:
    20. CVideoFrame(const CVideoFrame &);
    21. virtual ~CVideoFrame();
    22. CVideoFrame &operator =(const CVideoFrame &);
    23. const QImage *GetRGB() const;
    24. const QImage *GetY() const;
    25. const QImage *GetU() const;
    26. const QImage *GetV() const;
    27. uint GetNumber() const ;
    28. void SetSequence(unsigned long seq);
    29. unsigned long GetSequence() const;
    30. void SetTimeStamp(unsigned long stamp);
    31. unsigned long GetTimeStamp() const;
    32. };
    To copy to clipboard, switch view to plain text mode 
    --------------------------------------------------------------------------------------------------------------------

    Qt Code:
    1. #include "VideoFrame.h"
    2.  
    3. CVideoFrame::CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v)
    4. {
    5.  
    6. qDebug("CREATE DATAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    7.  
    8.  
    9. data = new VideoDataShared;
    10. if (data == 0)
    11. return;
    12.  
    13. data->RGB = rgb;
    14. data->Y = y;
    15. data->U = u;
    16. data->V = v;
    17. data->sequence = 0;
    18. data->time_stamp = 0;
    19. m_Number = number;
    20. qDebug(" m_Number is %d and number is %d >>>>>>>>>>>>>>>>>>>>",m_Number, number);
    21.  
    22. }
    23.  
    24. CVideoFrame::CVideoFrame(const CVideoFrame &f)
    25. {
    26.  
    27. qDebug("SECOND CONSTRUCTORRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
    28.  
    29. data = f.data;
    30.  
    31. qDebug("time_stamp is %ld", data->time_stamp);
    32. qDebug("sequence is %ld", data->sequence);
    33.  
    34. if (data != 0)
    35. {
    36. data->ref();
    37. }
    38.  
    39. }
    40.  
    41.  
    42. CVideoFrame::~CVideoFrame()
    43. {
    44. /* if (data && data->deref()) {
    45.   delete data;
    46.   data = 0;
    47.   }
    48. */
    49.  
    50. }
    51.  
    52. // private
    53.  
    54. uint CVideoFrame::GetRefCount() const
    55. {
    56. // if (data != 0)
    57. // return 0;
    58.  
    59. return 1;
    60. //// return data->count;
    61. }
    62.  
    63. // public
    64.  
    65. CVideoFrame &CVideoFrame::operator =(const CVideoFrame &f)
    66. {
    67. //// if (f.data == 0)
    68. //// return *this;
    69. /* f.data->ref();
    70.   if (data && data->deref()) {
    71.   delete data;
    72.   data = 0;
    73.   }
    74.   data = f.data;
    75.   return *this;
    76. */
    77.  
    78. }
    79.  
    80. const QImage *CVideoFrame::GetRGB() const
    81. {
    82. /// qDebug("qDebuggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg %d",data->RGB);
    83. return data->RGB;
    84. }
    85.  
    86. const QImage *CVideoFrame::GetY() const
    87. {
    88. /////////// return data->Y;
    89. }
    90.  
    91. const QImage *CVideoFrame::GetU() const
    92. {
    93. ////////////// return data->U;
    94. }
    95.  
    96. const QImage *CVideoFrame::GetV() const
    97. {
    98. //////////////// return data->V;
    99. }
    100.  
    101.  
    102. uint CVideoFrame::GetNumber() const
    103. {
    104. /// qDebug("<<m_Number is ..................%d<<",m_Number);
    105. return m_Number;
    106. }
    107.  
    108. void CVideoFrame::SetSequence(unsigned long seq)
    109. {
    110. //// data->sequence = seq;
    111. }
    112.  
    113.  
    114. /**
    115.   \brief Get sequence number
    116.  
    117.   VideoFrames are sequentially numbered as they are fetches from videodevice
    118.   or file. The sequence number is a monotonous increasing number.
    119.  
    120.   Note: when fetching frames from a video device, sequence numbers are not
    121.   contiguous in case frames are dropped by the capture process.
    122.  
    123. */
    124. unsigned long CVideoFrame::GetSequence() const
    125. {
    126. ////////////// return data->sequence;
    127. }
    128.  
    129. void CVideoFrame::SetTimeStamp(unsigned long stamp)
    130. {
    131. //////////////// data->time_stamp = stamp;
    132. }
    133.  
    134. /**
    135.   \brief Get time stamp
    136.   \return Time in milliseconds
    137.  
    138.   VideoFrames are time stamped, to make it easier to order/reference them. The
    139.   timestamp is measured in milliseconds.
    140.  
    141.   Note: The first video frame of a video sequence is not guaranteed to have a
    142.   timestamp of 0! The only guarantee you have is that the timestamp is a monotonous
    143.   increasing number.
    144. */
    145.  
    146. unsigned long CVideoFrame::GetTimeStamp() const
    147. {
    148. ///// return data->time_stamp;
    149. }
    To copy to clipboard, switch view to plain text mode 

    ================================================== ======

    and I try to create it in QT-4 like this,

    code in QT-4

    ================================================== =============
    Qt Code:
    1. #include <QSharedData>
    2. class CVideoFrame
    3. {
    4. friend class CVideoDevice;
    5. private:
    6. struct VideoDataShared: public QSharedData
    7. {
    8. const QImage *RGB;
    9. const QImage *Y, *U, *V;
    10.  
    11. unsigned long sequence;
    12. unsigned long time_stamp;
    13. } *data;
    14. uint m_Number ;
    15. private:
    16. uint GetRefCount() const;
    17. protected:
    18. CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v);
    19. public:
    20. uint m_Number ;
    21. CVideoFrame(const CVideoFrame &);
    22. virtual ~CVideoFrame();
    23. CVideoFrame &operator =(const CVideoFrame &);
    24. const QImage *GetRGB() const;
    25. const QImage *GetY() const;
    26. const QImage *GetU() const;
    27. const QImage *GetV() const;
    28. int GetNumber() const ;
    29. void SetSequence(unsigned long seq);
    30. unsigned long GetSequence() const;
    31. void SetTimeStamp(unsigned long stamp);
    32. unsigned long GetTimeStamp() const;
    33. };
    To copy to clipboard, switch view to plain text mode 
    ------------------------------------------------

    Qt Code:
    1. #include <QtGui>
    2. #include "VideoFrame.h"
    3.  
    4. CVideoFrame::CVideoFrame(uint number, const QImage *rgb, const QImage *y, const QImage *u, const QImage *v)
    5. {
    6. data = new VideoDataShared;
    7. if (data == 0)
    8. return;
    9.  
    10. data->RGB = rgb;
    11. data->Y = y;
    12. data->U = u;
    13. data->V = v;
    14. data->sequence = 0;
    15. data->time_stamp = 0;
    16. m_Number = number;
    17.  
    18. }
    19.  
    20. CVideoFrame::CVideoFrame(const CVideoFrame &f)
    21. {
    22.  
    23. data = f.data;
    24. if (data != 0)
    25. {
    26. data->ref;
    27. }
    28.  
    29. }CVideoFrame::~CVideoFrame()
    30. {
    31. /*if ( data && data->deref())
    32.   {
    33.   delete data;
    34.   data = 0;
    35.   }
    36. */
    37.  
    38.  
    39. }
    40.  
    41. // private
    42.  
    43. uint CVideoFrame::GetRefCount() const
    44. {
    45. // if (data != 0)
    46. // return 0;
    47.  
    48. return 1;
    49. ///////return data->count;
    50. }
    51.  
    52. // public
    53. CVideoFrame &CVideoFrame::operator =(const CVideoFrame &f)
    54. {
    55.  
    56. // if (f.data == 0)
    57. // return *this;
    58. // f.data->ref;
    59.  
    60. ///f.data->ref();
    61. /* if (data && data->deref()) {
    62.   delete data;
    63.   data = 0;
    64.   }
    65. */
    66.  
    67. // data = f.data;
    68. // return *this;
    69. }
    70.  
    71. const QImage *CVideoFrame::GetRGB() const
    72. {
    73. return data->RGB;
    74. }
    75.  
    76. const QImage *CVideoFrame::GetY() const
    77. {
    78. /////////////////////////// return data->Y;
    79. }
    80.  
    81. const QImage *CVideoFrame::GetU() const
    82. {
    83. ////////////////////////////// return data->U;
    84. }
    85.  
    86. const QImage *CVideoFrame::GetV() const
    87. {
    88. ///////////////////////// return data->V;
    89. }
    90.  
    91.  
    92. int CVideoFrame::GetNumber() const
    93. {
    94. return m_Number;
    95. }
    96.  
    97. void CVideoFrame::SetSequence(unsigned long seq)
    98. {
    99. /////////////////////// data->sequence = seq;
    100. }
    101.  
    102. unsigned long CVideoFrame::GetSequence() const
    103. {
    104. /////////////////////// return data->sequence;
    105. }
    106. void CVideoFrame::SetTimeStamp(unsigned long stamp)
    107. {
    108. //////////////////////// data->time_stamp = stamp;
    109. }
    110.  
    111. unsigned long CVideoFrame::GetTimeStamp() const
    112. {
    113. ///////////////// return data->time_stamp;
    114. }
    To copy to clipboard, switch view to plain text mode 

    ================================================== =================


    and in another function I call the function " frame->GetRGB() ". I think the error is this line.


    Qt Code:
    1. void CImagePanelRGB::UpdateImage()
    2. {
    3. CVideoFrame *frame = 0;
    4.  
    5. frame = m_pVideo->GetLatestVideoFrame();
    6. if (frame == 0){
    7. QImage *ImgRGB = new QImage();
    8. }
    9. else {
    10. ImgRGB = frame->GetRGB()->copy(); //////////////check this line
    11. }
    12. delete frame;
    13. update();
    14.  
    15. }
    To copy to clipboard, switch view to plain text mode 

    -----------------------------------------------------------
    Please help me to solve this problem
    Last edited by wysota; 11th October 2007 at 09:19. Reason: missing [code] tags

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QShared

    But what exactly is the problem? If you don't tell us what is wrong with the code (does it compile? does it crash? does it do something you don't want?), we won't be able to help you.

    And please use the [code] tags to embed code in your posts.

  9. #9
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QShared

    Hi,
    I can make and run the program in QT-4. But I didn't get any output from this code in QT-4. But the old program working in QT-3. When I comment the line

    frame->GetRGB()


    in QT-3 it also doesn't display the image in screen. So i think that, that line is error.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QShared

    It seems that ImgRGB is a QImage pointer and QImage::copy() returns the object, not a pointer to it. First of all I suggest you change pointers to QImage to QImage objects as in Qt4 there is no point in having bare QImage pointers. Then make sure you assign proper types (pointers to pointers and objects to objects). Then we can continue.

  11. #11
    Join Date
    Jul 2007
    Posts
    166
    Thanks
    25
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QShared

    Hi,
    please check this code it is in QT-3

    Qt Code:
    1. QVector<QImage> m_RGB;
    2. for (u = 0; u < m_Buffers; u++) {
    3. m_RGB.insert(u, new QImage(vid_io_buffer + vid_io_offsets[u], w, h, 32,0,0, QImage::IgnoreEndian));
    4. }
    To copy to clipboard, switch view to plain text mode 
    it is working in QT-3. How can I create this in QT-4. I do it like this,

    Qt Code:
    1. QVector<QImage *> m_RGB;
    2.  
    3. for (u = 0; u < m_Buffers; u++) {
    4. m_RGB.insert(u, *(new QImage(vid_io_buffer + vid_io_offsets[u], w, h, 32, QImage::Format_RGB32 )));
    To copy to clipboard, switch view to plain text mode 

    it doesn't deisplay any error at the time of make, but it doesn't display any image at run time. I think this is the most important part or that program, because when i comment this line in qt-3, then that program crashed at the time of run.
    Please help me. please find the error in my code.


    }
    Last edited by wysota; 11th October 2007 at 12:39. Reason: missing [code] tags

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QShared

    Could you please start using the proper tags to embed you code?

    Why do you keep using pointers in Qt4 code? Is there a reason for that? I told you there is no point in using pointers to QImage in Qt4, because it's an implicitely shared class! The code you have given won't even compile.

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.