PDA

View Full Version : Debugging help



poporacer
6th November 2010, 05:13
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.

//************************************************** ***********************
//routine to check if data has been changed.
//if it has, give the user the option to save the changes
//************************************************** ***********************

bool MainWindow::checkdata()
{
nameIndex=ui->cmbName->currentIndex();
qDebug()<<"index ="<<nameIndex; //this is returning either true or false should be 0,1,2...etc
if (isDirty)
{
int test=QMessageBox::warning(this, "Data has Changed", "You have made changes in the form.\n"
"Do you want to save the changes?", QMessageBox::Yes | QMessageBox::No |QMessageBox::Cancel);
if (test==QMessageBox::Yes)
{
return save();
}else if (test==QMessageBox::Cancel){
updateData(nameIndex);
isDirty=0;
return false;
}else if (test==QMessageBox::No){
updateData(nameIndex);
isDirty=0;
}
}
return true;
}

//************************************************** ***********************
//saves the changes
//************************************************** ***********************

bool MainWindow::save()
{
QSqlQuery query ;
query.prepare("UPDATE address set LName=:lname, FName=:fname, address=:address WHERE id=:id");
query.bindValue(":name", ui->txtLName->text());
query.bindValue(":fname",ui->txtFName->text());
query.bindValue(":address", ui->txtAddress->text());
query.bindValue(":id", ui->cmbName->itemData(nameIndex).toInt());
//if(!query.exec()); insert message for failed query

isDirty=0;

qDebug() << query.lastError();
query.exec();// delete for failed query test
nameIndex=ui->cmbName->currentIndex();//should get the current index to load later
updateNameComboBox();
ui->cmbName->setCurrentIndex(nameIndex);//the combobox returns to the first item

return true;
}

void MainWindow::updateRiderComboBox()
{

QSqlQuery query;
query.exec("SELECT id,LName, FName FROM address");
qDebug() << riderquery.lastError();
ui->cmbRider->clear();

while (riderquery.next())
{
ui->cmbRider->addItem(riderquery.value (2).toString()+ " "+ riderquery.value (1).toString(),riderquery.value (0).toInt());

}


SO how do I better use debug and which one? QDebug or qDebug? and any reason why this code is not working?

tbscope
6th November 2010, 05:19
I guess nameIndex is defined as a bool?

poporacer
6th November 2010, 05:27
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:

nameIndex=ui->cmbName->currentIndex();//should get the current index to load later but for some reason always gets set to 0
updateNameComboBox();
ui->cmbName->setCurrentIndex(nameIndex);//the combobox returns to the first item
name index is always 0.

tbscope
6th November 2010, 05:38
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.

poporacer
6th November 2010, 16:32
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?

poporacer
7th November 2010, 05:28
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

tbscope
7th November 2010, 05:39
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.

poporacer
7th November 2010, 18:28
Thanks.....I will get this figured out eventually!!!