Results 1 to 8 of 8

Thread: Debugging help

  1. #1
    Join Date
    Aug 2010
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Angry Debugging help

    I have a couple of issues but I think if I can learn the debug syntax, I might be able to find the issue (and some of my future issues!) I have a combobox that gets populated in a routine. In this routine two fields from a table are loaded (first name and last name). There are lin edit fields to make changes. If the user makes a change to the name, I want the combobox to get populated with the corrected name and then the combobox should display the item that is currently displayed. The combobox goes back to the first index each time. I tried using QDebug and got errors and while searching posts I see that people are using qDebug. What is the difference? I want to post the contents of a variable to see what is going on. What is the syntax for QDebug and qDebug. Here is what I tried and the output did not make sense.
    Qt Code:
    1. //*************************************************************************
    2. //routine to check if data has been changed.
    3. //if it has, give the user the option to save the changes
    4. //*************************************************************************
    5.  
    6. bool MainWindow::checkdata()
    7. {
    8. nameIndex=ui->cmbName->currentIndex();
    9. qDebug()<<"index ="<<nameIndex; //this is returning either true or false should be 0,1,2...etc
    10. if (isDirty)
    11. {
    12. int test=QMessageBox::warning(this, "Data has Changed", "You have made changes in the form.\n"
    13. "Do you want to save the changes?", QMessageBox::Yes | QMessageBox::No |QMessageBox::Cancel);
    14. if (test==QMessageBox::Yes)
    15. {
    16. return save();
    17. }else if (test==QMessageBox::Cancel){
    18. updateData(nameIndex);
    19. isDirty=0;
    20. return false;
    21. }else if (test==QMessageBox::No){
    22. updateData(nameIndex);
    23. isDirty=0;
    24. }
    25. }
    26. return true;
    27. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. //*************************************************************************
    2. //saves the changes
    3. //*************************************************************************
    4.  
    5. bool MainWindow::save()
    6. {
    7. QSqlQuery query ;
    8. query.prepare("UPDATE address set LName=:lname, FName=:fname, address=:address WHERE id=:id");
    9. query.bindValue(":name", ui->txtLName->text());
    10. query.bindValue(":fname",ui->txtFName->text());
    11. query.bindValue(":address", ui->txtAddress->text());
    12. query.bindValue(":id", ui->cmbName->itemData(nameIndex).toInt());
    13. //if(!query.exec()); insert message for failed query
    14.  
    15. isDirty=0;
    16.  
    17. qDebug() << query.lastError();
    18. query.exec();// delete for failed query test
    19. nameIndex=ui->cmbName->currentIndex();//should get the current index to load later
    20. updateNameComboBox();
    21. ui->cmbName->setCurrentIndex(nameIndex);//the combobox returns to the first item
    22.  
    23. return true;
    24. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MainWindow::updateRiderComboBox()
    2. {
    3.  
    4. QSqlQuery query;
    5. query.exec("SELECT id,LName, FName FROM address");
    6. qDebug() << riderquery.lastError();
    7. ui->cmbRider->clear();
    8.  
    9. while (riderquery.next())
    10. {
    11. ui->cmbRider->addItem(riderquery.value (2).toString()+ " "+ riderquery.value (1).toString(),riderquery.value (0).toInt());
    12.  
    13. }
    To copy to clipboard, switch view to plain text mode 
    SO how do I better use debug and which one? QDebug or qDebug? and any reason why this code is not working?

  2. #2
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Debugging help

    I guess nameIndex is defined as a bool?

  3. #3
    Join Date
    Aug 2010
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Debugging help

    That was a stupid mistake....yes it was! But the combobox is still set to the first item. This doesn't seem to be working:
    Qt Code:
    1. nameIndex=ui->cmbName->currentIndex();//should get the current index to load later but for some reason always gets set to 0
    2. updateNameComboBox();
    3. ui->cmbName->setCurrentIndex(nameIndex);//the combobox returns to the first item
    To copy to clipboard, switch view to plain text mode 
    name index is always 0.

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Debugging help

    An index looses its meaning after updating a list.

    You should get the index again after updating. You can use the string for example to find the index (difficult if you allow multiple items with the same string). Or use the userdata.

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

    nirab_25 (6th November 2010)

  6. #5
    Join Date
    Aug 2010
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Debugging help

    I guess I might have been going about this in the wrong way, but I am not sure what would be the correct way. I have an address book database with first name, last name, address, etc in the table. Then I have a dialog widget with a combo box and some lineedit widgets. The combobox gets populated with the first and last name combined. The lineedit widgets are used for input/corrections. If the user corrects the name, I want the database updated and the combobox updated as well and then the combobox set to the person that was being edited. What I did was to have a static variable- nameIndex to hold the index of the combobox that is being edited and when the combobox gets repopulated (it should have the same data with the corrections) set the currentIndex to the previously stored number. Why doesn't this work or what would be a better way? Get the ID and use a loop to go through all the items until there is a match with ID?
    Also, what is the difference with QDebug and qDebug? Why does one work and the other not? Which one is better? and how do I use QDebug?

  7. #6
    Join Date
    Aug 2010
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Debugging help

    I know that static variables are not recommended and at the time, I didn't see how to avoid it....and the result was the error I was experiencing (in addition to trying to maintain the index of the combobax when I didn't need to) I got it fixed. but the question still remains:
    QDebug vs qDebug....what is the difference which is better?

    Thanks

  8. #7
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 268 Times in 268 Posts
    Wiki edits
    20

    Default Re: Debugging help

    It is explained here:
    http://doc.qt.nokia.com/4.7/qdebug.html

    qDebug() is a convenience function returning a QDebug object making use of the default message handler.

  9. #8
    Join Date
    Aug 2010
    Posts
    107
    Qt products
    Qt4
    Platforms
    Windows
    Thanks
    5

    Default Re: Debugging help

    Thanks.....I will get this figured out eventually!!!

Similar Threads

  1. Replies: 3
    Last Post: 7th September 2010, 00:00
  2. SXE key has not been set when debugging
    By learning_qt in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 24th September 2008, 09:59
  3. Debugging Qt
    By baray98 in forum Qt Programming
    Replies: 0
    Last Post: 7th May 2008, 01:16
  4. Debugging with gdb
    By SteM in forum Newbie
    Replies: 4
    Last Post: 9th August 2007, 15:40
  5. Debugging
    By Voldemort in forum Qt Programming
    Replies: 13
    Last Post: 14th May 2007, 21:38

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.