Results 1 to 9 of 9

Thread: Converting C++ to Qt4

  1. #1
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Converting C++ to Qt4

    Hello. I have this class in pure C++.

    Qt Code:
    1. #ifndef VALUEVECTOR_HPP_
    2. #define VALUEVECTOR_HPP_
    3.  
    4. #include <iostream>
    5. #include <vector>
    6. #include <algorithm>
    7. using std::cout;
    8. using std::endl;
    9. using std::vector;
    10. using std::find;
    11. #include "Object.hpp"
    12. #include "ComaPtr.hpp"
    13. #include "Value.hpp"
    14.  
    15. class ValueVector : public Object {
    16. typedef ComaPtr<Value> type;
    17. public:
    18.  
    19. ValueVector() {
    20. }
    21.  
    22. ValueVector(Value* value) {
    23. clearChildren();
    24. addChildren(value);
    25. }
    26.  
    27. ValueVector(unsigned length) {
    28. clearChildren();
    29. size(length);
    30. }
    31.  
    32. ValueVector(const ValueVector& rhs) {
    33. *this = rhs;
    34. }
    35.  
    36. type& operator[](unsigned index) {
    37. return m_children[index];
    38. }
    39.  
    40. bool operator==(ValueVector& vv) {
    41. if(size() != vv.size()) {
    42. return false;
    43. }
    44.  
    45. for(unsigned i = 0; i < size(); ++i) {
    46. for(unsigned j = 0; j < vv.size(); ++j) {
    47. if(m_children[i] != vv[i]){
    48. return false;
    49. }
    50. }
    51. }
    52. return true;
    53. }
    54.  
    55. bool operator!=(ValueVector& vv) {
    56. if(size() != vv.size()) {
    57. return true;
    58. }
    59.  
    60. for(unsigned i = 0; i < size(); ++i) {
    61. for(unsigned j = 0; j < vv.size(); ++j) {
    62. if(m_children[i] != vv[i]){
    63. return true;
    64. }
    65. }
    66. }
    67. return false;
    68. }
    69.  
    70. void addChildren(type children) {
    71. assert(find(m_children.begin(), m_children.end(), children) == m_children.end());
    72. m_children.push_back(children);
    73. }
    74.  
    75. unsigned capacity() const {
    76. return m_children.capacity();
    77. }
    78.  
    79. void capacity(unsigned length) {
    80. m_children.reserve(length);
    81. }
    82.  
    83. void clearChildren() {
    84. m_children.clear();
    85. }
    86.  
    87. void setChildren(unsigned index, type children) {
    88. assert(index < size());
    89. m_children[index] = children;
    90. }
    91.  
    92. unsigned size() const {
    93. return m_children.size();
    94. }
    95.  
    96. void size(unsigned length) {
    97. m_children.resize(length);
    98. }
    99. private:
    100. vector<type> m_children;
    101. };
    102.  
    103. typedef ComaPtr<ValueVector> ValueVectorPtr;
    104.  
    105. #endif /*VALUEVECTOR_HPP_*/
    To copy to clipboard, switch view to plain text mode 

    And I was googling around for like a Smart Pointer in Qt4.4 but I couldn't seem to come up with any answers except for QGuardedPtr<> but only seems to appear in Qt3 which I don't use.

    ComaPtr<> is like boost::shared_ptr except my pointer class addRef and release which is all done in ComaPtr<> which only works on classes that inherit from my custom reference counting class Object. And was wondering. how do I set up QVector to hold an array of pointers of Value (example class). And automatically reference count it?
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  2. #2
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Converting C++ to Qt4

    Did you take a look at QSharedDataPointer?

    Also, nothing stops you from using boost::shared_ptr together with Qt...

  3. #3
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Converting C++ to Qt4

    Thank you. So one question. I think from reading the doc on it. It will automatically allow a end-user to do like Value* v = new Value(22); and not have to worry about deleting it?
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Converting C++ to Qt4

    no, something like that is just about impossible in C++.

    The way I read it, it works like boost::shared_ptr.

    QSharedDataPointer<Value> v = new Value(22);

    You have to assign the 'naked' pointer to a QSharedDataPointer. Once the (last copy of this) QSharedDataPointer goes out of scope the reference count gets zero and the object deleted.
    (Note that this is a huge difference to boost::shared_ptr: a QSharedDataPointer "detaches" when you call a non-const op on it (effectifely creating a separate copy).
    This is what you want for a value-like class like QString (see below). It might not be at all what you want.)
    If you do not want to implement a shared value class, stick to boost::shared_ptr, imo.


    The other thing is, that this class was designed for implementing 'value-like' shared objects. Like a QString.
    You will hardly ever write
    Qt Code:
    1. QString *s = new QString;
    To copy to clipboard, switch view to plain text mode 
    QString hides this and maybe uses a QSharedDataPointer internally.
    The docs illustrated how to do this.
    (This is basically a more adavanced form of the pimpl idiom with better support for efficient (and correct) copying of objects.)

    HTH

  5. #5
    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: Converting C++ to Qt4

    QSharedDataPointer operates on QSharedData, so this is probably not going to work.

    Something like:
    Qt Code:
    1. SomeTypeVeryWisePointer ptr = new SomeType;
    To copy to clipboard, switch view to plain text mode 
    doesn't make much sense if you want the data to be deleted when ptr goes out of scope. If you want that, simply write:
    Qt Code:
    1. SomeType obj;
    To copy to clipboard, switch view to plain text mode 
    When the variable goes out of scope, the compiler will delete it for you.

    There is also a chance you want a reference counting pointer:
    Qt Code:
    1. SharedPtr ptr1 = new SomeType;
    2. SharedPtr ptr2 = ptr1;
    3. delete ptr1; // this is just an example...
    4. ptr2->stillWorking();
    To copy to clipboard, switch view to plain text mode 
    In that case QSharedDataPointer (or rather QExplicitelySharedDataPointer) will be helpful if you implement SomeType as a shared class. Then you'll be able to write:
    Qt Code:
    1. SomeType obj1;
    2. SomeType obj2 = obj1;
    To copy to clipboard, switch view to plain text mode 
    And both facade objects will point to the same set of data.

    You might also want a guarded pointer (similar to what QMutexLocker does). In that case you can implement it yourself.
    Qt Code:
    1. template <class T> class GuardedPtr {
    2. public:
    3. GuardedPtr(T *o) { ptr = o; }
    4. ~GuardedPtr(){ delete ptr; }
    5. private:
    6. T *ptr;
    7. };
    To copy to clipboard, switch view to plain text mode 

    and then use it like this:
    Qt Code:
    1. xyz *ptr = new xyz;
    2. GuardedPtr<ptr> guard(ptr);
    3. //...
    4. // ptr gets deleted when guard goes out of scope
    To copy to clipboard, switch view to plain text mode 

    Just be aware it's a stupid class that may cause much trouble.
    Last edited by wysota; 10th July 2008 at 18:15.

  6. The following user says thank you to wysota for this useful post:

    ComaWhite (11th July 2008)

  7. #6
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Converting C++ to Qt4

    I think I finally got it working. But I thought you would call detach before setting the value and not after?


    Qt Code:
    1. #include <QExplicitlySharedDataPointer>
    2. #include <QSharedData>
    3. #include <QDebug>
    4.  
    5. class ValueData : public QSharedData {
    6. public:
    7. ValueData()
    8. : m_value(1) {
    9. }
    10. ValueData(int value)
    11. : m_value(value) {
    12. }
    13. ~ValueData() { }
    14. ValueData(const ValueData& v)
    15. m_value(v.m_value) {
    16. }
    17. void print() {
    18. qDebug() << m_value;
    19. }
    20.  
    21. int m_value;
    22. };
    23.  
    24. class ValueT {
    25. public:
    26. ValueT() {
    27. d = new ValueData;
    28. }
    29.  
    30. ValueT(int value) {
    31. d = new ValueData(value);
    32. }
    33.  
    34. void setValue(int value) {
    35. d->m_value = value;
    36. d.detach();
    37. }
    38. void print() {
    39. d->print();
    40. }
    41. private:
    42. QExplicitlySharedDataPointer<ValueData> d;
    43. };
    To copy to clipboard, switch view to plain text mode 

    But one last question. What happens if the end user does
    Qt Code:
    1. ValueT* v = new ValueT(123);
    To copy to clipboard, switch view to plain text mode 

    Will it cause a a memory leak if s/he doesn't call delete?
    Last edited by ComaWhite; 11th July 2008 at 00:36.
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  8. #7
    Join Date
    Mar 2008
    Posts
    2
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Converting C++ to Qt4

    Yes, then you must delete it yourself

  9. #8
    Join Date
    Mar 2008
    Location
    Houston, Texas, USA
    Posts
    277
    Thanks
    9
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Maemo/MeeGo

    Default Re: Converting C++ to Qt4

    It doesn't like when you create your own operator= or copy constructor. Because I tried to make like 3 of them and when I did. It would never print to console unless I removed them
    Qt-4.7.3 | Gentoo ~amd64 | KDE-4.7
    Aki IRC Client for KDE4 | Qt Documentation

  10. #9
    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: Converting C++ to Qt4

    Quote Originally Posted by ComaWhite View Post
    I think I finally got it working. But I thought you would call detach before setting the value and not after?
    With an explicitely shared pointer you should call detach only if you want to separate the two objects (so copy is always a shallow copy by default). If you want to always separate them, use QSharedDataPointer which calls detach itself (copy on write).
    Last edited by jacek; 11th July 2008 at 20:04. Reason: fixed bbcode

Similar Threads

  1. Replies: 10
    Last Post: 9th June 2016, 17:02
  2. Problem in converting QString to QChar array?
    By KaKa in forum Qt Programming
    Replies: 2
    Last Post: 19th March 2007, 00:38
  3. Problem converting .ui files from Qt3 to 4
    By Amanda in forum Qt Programming
    Replies: 6
    Last Post: 28th October 2006, 04:34
  4. Converting QT 4 to VC++
    By vvbkumar in forum Qt Programming
    Replies: 3
    Last Post: 22nd June 2006, 13:54
  5. Converting my UI to Qt4
    By Honestmath in forum Qt Programming
    Replies: 1
    Last Post: 14th April 2006, 23:58

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.