QTableView 的 qt setItem 导致 "program not responding"

qt setItem for QTableView causes "program not responding"

我在向 qt creator

中的 table 添加值时遇到问题

在我下面的代码中,我有一个函数可以将数据从数据库插入 table。

QSqlQuery q;
//counts the number of rows that contain the value acicn
q.prepare("select count(*) from checkdata where acic_num=:acicn");
q.bindValue(":acicn", acicn);
q.exec();

int rowtablecount = 0;
if(q.next()){
    rowtablecount = q.value(0).toInt();
    //using value acicn, inserts into the table the number of rows found in database
    QAbstractItemModel *mdl;
    mdl = ui->editEntry_tbl->model();
        mdl->insertRows(0,rowtablecount);
}else{
    qDebug() << "table error in edit entry";
}
q.finish();

QSqlQuery q2;
q2.prepare("select *from acic where acic_num=:acicnum");
q2.bindValue(":acicnum", acicn);
q2.exec();
//above extracts data from database table acic, below from table checkdata

q.prepare("select * from checkdata where acic_num=:acicnum");
q.bindValue(":acicnum", acicn);
q.exec();

QStandardItemModel *model;
QString temp;
while(q.next()){
    for(int r=0; r<rowtablecount-1; r++){
        temp =q.value(6).toString();   //date paid
        QStandardItem *cellFiller8 = new QStandardItem(temp);
        model->setItem(r,10,cellFiller8); //<<--- PART WHERE PROGRAM FAILS AND HANGS, SHOWING PROGRAM NOT RESPONDING

        temp =q.value(5).toString();   //amount of check
        QStandardItem *cellFiller6 = new QStandardItem(temp);
        model->setItem(r,7,cellFiller6);

        temp =q.value(4).toString();   //account code
        QStandardItem *cellFiller5 = new QStandardItem(temp);
        model->setItem(r,6,cellFiller5);

        temp =q.value(3).toString();   //particulars
        QStandardItem *cellFiller4 = new QStandardItem(temp);
        model->setItem(r,5,cellFiller4);

        temp =q.value(2).toString();   //payee name
        QStandardItem *cellFiller3 = new QStandardItem(temp);
        model->setItem(r,4,cellFiller3);

        temp =q.value(1).toString();    //alobs
        QStandardItem *cellFiller2 = new QStandardItem(temp);
        qDebug() << temp;
        model->setItem(r,3,cellFiller2);

        temp =q.value(0).toString();    //check number
        QStandardItem *cellFiller1 = new QStandardItem(temp);
        qDebug() << temp;
        model->setItem(r,2,cellFiller1);
    }
}
while(q2.next()){
    for(int r=0; r<rowtablecount-1; r++){
        temp =q2.value(3).toString();   //amount of acic
        QStandardItem *cellFiller7 = new QStandardItem(temp);
        model->setItem(r,8,cellFiller7);

        temp =q2.value(0).toString();   //check date
        QStandardItem *cellFiller = new QStandardItem(temp);
        qDebug() << "before setitem" << temp;
        model->setItem(r,1,cellFiller);
        qDebug() << "after setItem";

    }
}

基本上,使用model->setItem(r,1,cellFiller) 会导致程序挂起。我发现这非常令人困惑,因为我多次使用相同的方法填写 QTableView,但这是第一次发生这种情况。

知道为什么会这样吗?

您忘记初始化 QStandardItemModel *model 变量。