Page 1 of 2 12 LastLast
Results 1 to 20 of 23

Thread: Constructor call problem

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Constructor call problem

    Hi to all!

    In my project I have following code chunk:
    Qt Code:
    1. void CShoppingCartWidget::addEntry(structOrder& order)
    2. {
    3. // debug block
    4. qDebug() << "order.iMerchandizeID=" << order.iMerchandizeID;
    5. qDebug() << "order.iMerchandizeQuantity=" << order.iMerchandizeQuantity;
    6. qDebug() << "order.rMerchandizePrice=" << order.rMerchandizePrice;
    7. qDebug() << "order.rSubtotal=" << order.rSubtotal;
    8. qDebug() << "order.strDisplayString=" << order.strDisplayString;
    9. qDebug() << "order.strMerchandizeName=" << order.strMerchandizeName;
    10. // **** END of debug block
    11.  
    12. QList<CMerchandizeOrder*> orders=shoppingCartModel()->orders();
    13. //CMerchandizeOrder tempOrder(order);
    14. CMerchandizeOrder* tempOrder=new CMerchandizeOrder(order);
    15. Q_CHECK_PTR(tempOrder);
    To copy to clipboard, switch view to plain text mode 
    As you can see, tempOrder an object of CMerchandizeOrder class that is created inside this code chunk. But, when I try to see with gdb which constructor is called and I set the breakpoints in first lines of all CMerchandizeOrder constructors, the app flow never comes to either breakpoint. The result of this is that the wrong order is added into table (default values instead of gathered from constructor parameter order). What is going on?! Please help! And the debug block shows right values.
    Qt 5.3 Opensource & Creator 3.1.2

  2. #2
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Constructor call problem

    No one has idea what to do? I am really banging my head into a wall with this one ...
    Qt 5.3 Opensource & Creator 3.1.2

  3. #3
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Constructor call problem

    Whats the prototype of CMerchandizeOrder constructors ??

    Also you are using order and orders variable . My guess is you are passing the wrong one. Am not sure though. Can say more if u show what the CMerchandizeOrder ctor takes

  4. #4
    Join Date
    May 2008
    Posts
    155
    Thanked 15 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Constructor call problem

    Quote Originally Posted by MarkoSan View Post
    Hi to all!

    ...
    Q_CHECK_PTR(tempOrder);[/code]As you can see, tempOrder an object of CMerchandizeOrder class that is created inside this code chunk. But, when I try to see with gdb which constructor is called and I set the breakpoints in first lines of all CMerchandizeOrder constructors, the app flow never comes to either breakpoint. The result of this is that the wrong order is added into table (default values instead of gathered from constructor parameter order). What is going on?! Please help! And the debug block shows right values.
    gcc/gdb has a general problem with breakpoint in constructors.
    It (often) generates several copies of the constructor code
    and fails to communicate the correct debug information.

    A workaround for this kind of problem is to move the code of the
    constructor body into some kind of init() member function and
    call this function from the constructor.

  5. The following user says thank you to ktk for this useful post:

    drhex (26th May 2008)

  6. #5
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Constructor call problem

    Well, here is this constructor:
    Qt Code:
    1. class CMerchandizeOrder
    2. {
    3. public:
    4. CMerchandizeOrder();
    5. CMerchandizeOrder(structOrder& order);
    6. ....
    7. };
    To copy to clipboard, switch view to plain text mode 
    and here is implemetantion:
    Qt Code:
    1. CMerchandizeOrder::CMerchandizeOrder()
    2. {
    3. m_OrderValues.iMerchandizeID=0;
    4. m_OrderValues.iMerchandizeQuantity=0;
    5. m_OrderValues.rMerchandizePrice=0.00;
    6. m_OrderValues.rSubtotal=0.00;
    7. //m_OrderValues.strDisplayString=QString("Test Artikel Blank Constructor");
    8. m_OrderValues.strMerchandizeName=QString("Test Artikel");
    9. setDisplayString(m_OrderValues.iMerchandizeID,
    10. m_OrderValues.strMerchandizeName,
    11. m_OrderValues.iMerchandizeQuantity,
    12. m_OrderValues.rMerchandizePrice,
    13. m_OrderValues.rSubtotal);
    14. }
    15.  
    16. CMerchandizeOrder::CMerchandizeOrder(structOrder& order)
    17. {
    18. m_OrderValues.iMerchandizeID=order.iMerchandizeID;
    19. m_OrderValues.iMerchandizeQuantity=order.iMerchandizeQuantity;
    20. m_OrderValues.rMerchandizePrice=order.rMerchandizePrice;
    21. m_OrderValues.rSubtotal=order.rSubtotal;
    22. m_OrderValues.strMerchandizeName=order.strMerchandizeName;
    23. setDisplayString(m_OrderValues.iMerchandizeID,
    24. m_OrderValues.strMerchandizeName,
    25. m_OrderValues.iMerchandizeQuantity,
    26. m_OrderValues.rMerchandizePrice,
    27. m_OrderValues.rSubtotal);
    28. }
    To copy to clipboard, switch view to plain text mode 

    Will try with init function.
    Qt 5.3 Opensource & Creator 3.1.2

  7. #6
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Constructor call problem

    I tried with init function and same result.
    Qt 5.3 Opensource & Creator 3.1.2

  8. #7
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Constructor call problem

    ur code seems fine.... and am also begining to scratch my head
    need to know 2 things -
    1) is CShoppingCartWidget::addEntry reached ?? put qDebug for tempOrder as u did for order. What values you get there ? As for breakpoints in constructor, try qDebug in constructor. Sometimes breakpoints cannot be reached due to some other reason.

    2) how are u adding the tempOrder to the table ? bec the code till now looks fine. may be u are making mistake somewhere else.

  9. #8
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Constructor call problem

    Well, the addEntry() is reached and the model works fine since test data ARE added to table widget becuase I can see them. I've put qDebug() where you asked, the values are ok and here is the method that calls addEntry. I've doublechecked this tmpOrder and data are fetched ok:
    Qt Code:
    1. void COperationWIndow::chooseMerchandize()
    2. {
    3. structOrder tmpOrder; // temp order
    4. QString queryString("SELECT * from merchandize WHERE IdentificationNumber=%1 AND InUse=1;");
    5. int iMerchandizeId=m_pMerchandizeBrowser->m_iSelected+1; // calcualtes id
    6.  
    7. queryString=queryString.arg(iMerchandizeId); // adds id to query string
    8. qDebug() << "Query: " << queryString; // debug
    9. QSqlQuery query(queryString); // sets up query from query string
    10. qDebug() << query.lastError().text(); // debug
    11. if (query.isActive())
    12. {
    13. while (query.next())
    14. {
    15. tmpOrder.iMerchandizeID=query.value(0).toInt();
    16. tmpOrder.iMerchandizeQuantity=1;
    17. tmpOrder.rMerchandizePrice=(qreal)query.value(3).toDouble();
    18. tmpOrder.rSubtotal=tmpOrder.iMerchandizeQuantity*tmpOrder.rMerchandizePrice;
    19. tmpOrder.strMerchandizeName=QString(query.value(2).toString());
    20. tmpOrder.strDisplayString=QString::number(tmpOrder.iMerchandizeID)+strMerchandizeSpaceDelimiter+\
    21. QString(tmpOrder.strMerchandizeName)+strMerchandizeDelimiter+\
    22. QString::number(tmpOrder.rMerchandizePrice, 'f', iMerchandizePricePrecision)+strMerchandizeSpaceDelimiter+\
    23. QString::number(tmpOrder.iMerchandizeQuantity)+strMerchandizeSpaceDelimiter+\
    24. QString::number(tmpOrder.rSubtotal, 'f', iMerchandizePricePrecision);
    25. m_pShoppingCartWidget->addEntry(tmpOrder);
    26. } // while
    27. m_pShoppingCartWidget->resizeColumnsToContents();
    28. m_pShoppingCartWidget->resizeRowsToContents();
    29. if(m_pOrderButton->isEnabled()==false)
    30. m_pOrderButton->setEnabled(true); // enables order buttn
    31. } // if
    32. }
    To copy to clipboard, switch view to plain text mode 

    I've been banging my head for a whole day now.
    Qt 5.3 Opensource & Creator 3.1.2

  10. #9
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Constructor call problem

    What about the qDebug in constructors ?? are they OK too ??
    if they are, then am sure u are overriding those values somewhere else

  11. The following user says thank you to aamer4yu for this useful post:

    MarkoSan (25th May 2008)

  12. #10
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Constructor call problem

    Quote Originally Posted by aamer4yu View Post
    What about the qDebug in constructors ?? are they OK too ??
    if they are, then am sure u are overriding those values somewhere else
    Thanks for useful hint. So, this is getting weirder and weirder . I've modified the both constructors in a way:
    Qt Code:
    1. CMerchandizeOrder::CMerchandizeOrder()
    2. {
    3. resetOrder();
    4. qDebug() << "Default constructor with parameter values:";
    5. qDebug() << "m_OrderValues.iMerchandizeID=" << m_OrderValues.iMerchandizeID;
    6. qDebug() << "m_OrderValues.iMerchandizeQuantity=" << m_OrderValues.iMerchandizeQuantity;
    7. qDebug() << "m_OrderValues.rMerchandizePrice=" << m_OrderValues.rMerchandizePrice;
    8. qDebug() << "m_OrderValues.rSubtotal=" << m_OrderValues.rSubtotal;
    9. qDebug() << "m_OrderValues.strMerchandizeName=" << m_OrderValues.strMerchandizeName;
    10. }
    11.  
    12. CMerchandizeOrder::CMerchandizeOrder(structOrder& order)
    13. {
    14. m_OrderValues.iMerchandizeID=order.iMerchandizeID;
    15. m_OrderValues.iMerchandizeQuantity=order.iMerchandizeQuantity;
    16. m_OrderValues.rMerchandizePrice=order.rMerchandizePrice;
    17. m_OrderValues.rSubtotal=order.rSubtotal;
    18. m_OrderValues.strMerchandizeName=order.strMerchandizeName;
    19. setDisplayString(m_OrderValues.iMerchandizeID,
    20. m_OrderValues.strMerchandizeName,
    21. m_OrderValues.iMerchandizeQuantity,
    22. m_OrderValues.rMerchandizePrice,
    23. m_OrderValues.rSubtotal);
    24. qDebug() << "Constructor with parameter values:";
    25. qDebug() << "m_OrderValues.iMerchandizeID=" << m_OrderValues.iMerchandizeID;
    26. qDebug() << "m_OrderValues.iMerchandizeQuantity=" << m_OrderValues.iMerchandizeQuantity;
    27. qDebug() << "m_OrderValues.rMerchandizePrice=" << m_OrderValues.rMerchandizePrice;
    28. qDebug() << "m_OrderValues.rSubtotal=" << m_OrderValues.rSubtotal;
    29. qDebug() << "m_OrderValues.strMerchandizeName=" << m_OrderValues.strMerchandizeName;
    30. }
    31.  
    32. void CMerchandizeOrder::resetOrder()
    33. {
    34. m_OrderValues.iMerchandizeID=0;
    35. m_OrderValues.iMerchandizeQuantity=0;
    36. m_OrderValues.rMerchandizePrice=0.00;
    37. m_OrderValues.rSubtotal=0.00;
    38. //m_OrderValues.strDisplayString=QString("Test Artikel Blank Constructor");
    39. m_OrderValues.strMerchandizeName=QString("Test Artikel");
    40. setDisplayString(m_OrderValues.iMerchandizeID,
    41. m_OrderValues.strMerchandizeName,
    42. m_OrderValues.iMerchandizeQuantity,
    43. m_OrderValues.rMerchandizePrice,
    44. m_OrderValues.rSubtotal);
    45. }
    To copy to clipboard, switch view to plain text mode 
    . Then I've setup the breakpoint IMMEDIALTY AFTER addEntry() call and this is what I get from debug console:
    Qt Code:
    1. warning: **** CONSTRUCTOR - WITH PARAMETERS values:
    2.  
    3. warning: m_OrderValues.iMerchandizeID= 15
    4.  
    5. warning: m_OrderValues.iMerchandizeQuantity= 1
    6.  
    7. warning: m_OrderValues.rMerchandizePrice= 5.43
    8.  
    9. warning: m_OrderValues.rSubtotal= 5.43
    10.  
    11. warning: m_OrderValues.strMerchandizeName= "McToast"
    12.  
    13. warning: **** DEFAULT CONSTRUCTOR - WITHOUT PARAMETERS values:
    14.  
    15. warning: m_OrderValues.iMerchandizeID= 0
    16.  
    17. warning: m_OrderValues.iMerchandizeQuantity= 0
    18.  
    19. warning: m_OrderValues.rMerchandizePrice= 0
    20.  
    21. warning: m_OrderValues.rSubtotal= 0
    22.  
    23. warning: m_OrderValues.strMerchandizeName= "Test Artikel"
    24.  
    25. warning: **** CONSTRUCTOR - WITH PARAMETERS values:
    26.  
    27. warning: m_OrderValues.iMerchandizeID= 0
    28.  
    29. warning: m_OrderValues.iMerchandizeQuantity= 0
    30.  
    31. warning: m_OrderValues.rMerchandizePrice= 0
    32.  
    33. warning: m_OrderValues.rSubtotal= 0
    34.  
    35. warning: m_OrderValues.strMerchandizeName= "Test Artikel"
    To copy to clipboard, switch view to plain text mode 
    So, if addEntry is called ONE time, why is then first call of constructor (with parameters) ok and then there are other calls of constructor (both) which resets data to 0. WTF?!
    Qt 5.3 Opensource & Creator 3.1.2

  13. #11
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Constructor call problem

    Few more things to look for -
    1) what does QList<CMerchandizeOrder*> orders=shoppingCartModel()->orders(); do ??

    2) in while(query.next()) , how many times is it executed ??

    3) whats the full code in addEntry ??

    my guess is thers prob in addEntry function only. Because thats where you were creating the CMerchandizeOrder objects.

    add some debugging in addEntry. Before and after u create the CMerchandizeOrder object.

    By the way,,, code is going into constructors isnt it

  14. #12
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Constructor call problem

    Well, my answers:

    1. Qt Code:
      1. QList<CMerchandizeOrder*> orders=shoppingCartModel()->orders();
      To copy to clipboard, switch view to plain text mode 
      gets full ist of previous orders in model, so I can check if processed order is already in list of orders so I can then update quantity (+1) and subtotal of order (recalculate quantity*price=subtotal)
    2. query.next() is executesd excalty ONCE
    3. here is addEntry code:
      Qt Code:
      1. void CShoppingCartWidget::addEntry(structOrder& order)
      2. {
      3. // debug block
      4. qDebug() << "order.iMerchandizeID=" << order.iMerchandizeID;
      5. qDebug() << "order.iMerchandizeQuantity=" << order.iMerchandizeQuantity;
      6. qDebug() << "order.rMerchandizePrice=" << order.rMerchandizePrice;
      7. qDebug() << "order.rSubtotal=" << order.rSubtotal;
      8. qDebug() << "order.strDisplayString=" << order.strDisplayString;
      9. qDebug() << "order.strMerchandizeName=" << order.strMerchandizeName;
      10. // **** END of debug block
      11.  
      12. QList<CMerchandizeOrder*> orders=shoppingCartModel()->orders();
      13. CMerchandizeOrder* tempOrder=new CMerchandizeOrder(order);
      14. Q_CHECK_PTR(tempOrder);
      15.  
      16. /*
      17.   tempOrder.iMerchandizeID=order.iMerchandizeID;
      18.   tempOrder.iMerchandizeQuantity=order.iMerchandizeQuantity;
      19.   tempOrder.rMerchandizePrice=order.rMerchandizePrice;
      20.   tempOrder.rSubtotal=order.rSubtotal;
      21.   tempOrder.strDisplayString=order.strDisplayString;
      22.   tempOrder.strMerchandizeName=order.strMerchandizeName;
      23. */
      24.  
      25. if (!orders.contains(tempOrder))
      26. {
      27. // new merchandize
      28. shoppingCartModel()->insertRows(0, 1, QModelIndex());
      29. QModelIndex index=shoppingCartModel()->index(0, 0, QModelIndex());
      30. shoppingCartModel()->setData(index,
      31. //tempOrder.orderValues().strDisplayString,
      32. tempOrder->orderValues().strDisplayString,
      33. Qt::EditRole);
      34. }
      35. else
      36. {
      37. // merchandize exists, update quantity and subtotal
      38. }
      39. }
      To copy to clipboard, switch view to plain text mode 

    So, I was working on the base of qt 4.4.0 example and there the object is created in same fashion like in my module ...
    Qt 5.3 Opensource & Creator 3.1.2

  15. #13
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Constructor call problem

    will have to go thru ur code all again []

    one thing i can suggest for the time being,,,
    if ur code is going in constructor, are u able to pur breakpoint and see the call stack.
    It will give u an idea wher the object is being created

  16. #14
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Constructor call problem

    Quote Originally Posted by aamer4yu View Post
    will have to go thru ur code all again []

    one thing i can suggest for the time being,,,
    if ur code is going in constructor, are u able to pur breakpoint and see the call stack.
    It will give u an idea wher the object is being created
    I do not understand right now what do you mean, we already saw the object is being made in the wasy as it should THE FIRST TIME, but I cannot crack why are constructors (both) being called 4 times (as you saw from debug messages). I double checked and I confirm query.exec() is executed only ONCE.
    Qt 5.3 Opensource & Creator 3.1.2

  17. #15
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Constructor call problem

    I mean,,, are u able to SET A BREAKPOINT in the constructor now ???

    if yes,,, set the breakpoint... and see from the call stack from where the code was called

  18. #16
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Constructor call problem

    The Eclipse IDE lets me set the breakpoint in constructor, but the breakpoint is never reached ...
    Qt 5.3 Opensource & Creator 3.1.2

  19. #17
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Constructor call problem

    then how come you are able to get qDebug messages from the ctor

  20. #18
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Constructor call problem

    Quote Originally Posted by aamer4yu View Post
    then how come you are able to get qDebug messages from the ctor
    I've setup breakpoint in the next line of code after constructor and then I copied messages from debug console. Messages stay in debug console after exiting from constructor ...
    Qt 5.3 Opensource & Creator 3.1.2

  21. #19
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Constructor call problem

    If I setup breakpoint one line before CMerchandizeOrder constructor call and then try to step into CMerchandize constructor call I get something very weird, the dialog box shows which source file to choose to go into and there are several choices, which are all the same - qbytearray.h. I've enclosed a screenshot for better understanding. Is this maybe somekind of qt 4.4.0 bug?!
    Attached Images Attached Images
    Qt 5.3 Opensource & Creator 3.1.2

  22. #20
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Thanks
    111
    Thanked 4 Times in 4 Posts
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Constructor call problem

    Gurus like jacek, jpn, wysotta and other pleeeeeease help me!!!!
    Qt 5.3 Opensource & Creator 3.1.2

Similar Threads

  1. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 20:17
  2. Tricky problem with ARGB widget / UpdateLayeredWindow
    By nooky59 in forum Qt Programming
    Replies: 3
    Last Post: 21st February 2008, 10:35
  3. QTimer problem ... it runs but never triggs
    By yellowmat in forum Newbie
    Replies: 4
    Last Post: 4th July 2006, 12:54
  4. Grid Layout Problem
    By Seema Rao in forum Qt Programming
    Replies: 2
    Last Post: 4th May 2006, 12:45
  5. Replies: 16
    Last Post: 7th March 2006, 15:57

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.