Results 1 to 5 of 5

Thread: Add QThread objects to QLinkedList

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Add QThread objects to QLinkedList

    I'm new here on the forum and I need some answers as to what I have done wrong in my coding.
    I usually code in Java, C# and some extend of C++ so I might have mixed something out.

    What I'm trying to do is to create a class that have stores threads of a custom class into a LinkedList.
    The error that I came out is Error 1 error C2248: 'QObject::QObject' : cannot access private member declared in class 'QObject' c:\qt\4.4.3\src\corelib\thread\qthread.h 132


    I myself think that the error is caused by the incorrect use of the LinkedList append method in the listClass add method, any help would be appreciated.

    This is not the complete/original code but it's fairly similar except having the includes.

    Qt Code:
    1. class CustomObj : public QThread
    2. {
    3. Q_OBJECT
    4. public:
    5. CustomObj();
    6. protected:
    7. void run();
    8. }
    9.  
    10. CustomObj::CustomObj()
    11. {
    12. }
    13.  
    14. void CustomObj::run()
    15. {
    16. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class listClass
    2. {
    3. public:
    4. listClass();
    5. void add(CustomObj cus);
    6. private:
    7. QLinkedList<CustomObj> *list;
    8. }
    9.  
    10. listClass::listClass()
    11. {
    12. list = new QLinkedList<CustomObj>;
    13. }
    14.  
    15. void listClass::add(CustomObj cus)
    16. {
    17. list->append(cus);
    18. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2009
    Location
    Enschede, Netherlands
    Posts
    462
    Thanked 69 Times in 67 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add QThread objects to QLinkedList

    You cannot copy QObjects (you are trying to access the private copy operator). Keep pointers to the objects in the list instead of the object itself.
    Horse sense is the thing that keeps horses from betting on people. --W.C. Fields

    Ask Smart Questions

  3. #3
    Join Date
    Jan 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Add QThread objects to QLinkedList

    Cheers, I knew something was wrong there. I tried changing it to a pointer but it doesn't match the parameter required for the QLinkedList append method.

    Qt Code:
    1. void listClass::add(CustomObj cus)
    2. {
    3. CustomObj *pointer = &cus;
    4. list->append(pointer);
    5. }
    To copy to clipboard, switch view to plain text mode 

    I know it's not the right place to ask this general C++ question but could you please provide an example or a code snippet for this typical question.

  4. #4
    Join Date
    Sep 2008
    Location
    Bangalore
    Posts
    659
    Thanks
    116
    Thanked 42 Times in 41 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add QThread objects to QLinkedList

    assign your QList like this
    Qt Code:
    1. QLinkedList<CustomObj *> wlist
    To copy to clipboard, switch view to plain text mode 
    "Behind every great fortune lies a crime" - Balzac

  5. #5
    Join Date
    Jan 2010
    Posts
    73
    Thanks
    6
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Add QThread objects to QLinkedList

    This code concerns me:
    Qt Code:
    1. void listClass::add(CustomObj cus)
    2. {
    3. CustomObj *pointer = &cus;
    4. list->append(pointer);
    5. }
    To copy to clipboard, switch view to plain text mode 
    My concern is that you understand when an object is destroyed and who owns the pointer. I do not know you well enough to know if you understand these issues or not. If you do not, be certain to say so. Consider what happens if I create an object and then add it to a list or simply take the pointer of the object. For example:
    Qt Code:
    1. MyObj* Pointer = NULL;
    2. if (Pointer == NULL)
    3. {
    4. MyObj x;
    5. Pointer = &x;
    6. }
    To copy to clipboard, switch view to plain text mode 
    In this example, at the end of the if statement, the variable x goes out of scope and the destructor is called. This means that Pointer is left pointing at a variable that is no longer valid. On the other hand, if you have a list of pointers, there can always be an issue with who owns the pointer and who should delete it. As long as you understand these issues, then there is no problem.

Similar Threads

  1. Sorting a qLinkedList
    By pwaldron in forum Qt Programming
    Replies: 3
    Last Post: 19th January 2010, 21:59
  2. QList or QLinkedList
    By eleanor in forum Newbie
    Replies: 6
    Last Post: 9th November 2007, 09:40
  3. QLinkedList iterator
    By ^NyAw^ in forum Qt Programming
    Replies: 8
    Last Post: 18th October 2007, 16:15
  4. QLinkedList and iterators
    By eu.x in forum Newbie
    Replies: 1
    Last Post: 19th April 2007, 19:38
  5. Replies: 7
    Last Post: 18th July 2006, 21:33

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.