سؤال

i am creating a table and uinserting the row in it like this.only one row is inserted in the table and that too conditional and if condition isnt satisfied than an error is shown. the problem is that when a row is inserted on satisfying condition then,the other row when inserted on again satisfying condition doesnt re write that row but uses the next two column of that row keepinf the previous two column empty.

Example

first result:

2 hello

second result:

     3 hello

Code

void searchWindow::TextReturn()
{
        int id = (text->text()).toInt();
        map<int,QString>::iterator itt;
        itt = appWindow::dataa.find(id);
        if(itt != appWindow::dataa.end())           //returns 1 if we found something
        {
                m_mode1 = new QStandardItemModel(0,2,this);
                m_mode1->setHorizontalHeaderItem(0, new QStandardItem(QString("ID")));
                m_mode1->setHorizontalHeaderItem(1, new QStandardItem(QString("DATA")));

                m_items << new QStandardItem((QString("%1").arg(id)));
                m_items << new QStandardItem((*itt).second);
                m_mode1->appendRow(m_items);
                m_tablee->update();
                text->setText("");
                m_tablee->setModel(m_mode1);
        }
        else
        {
                m_mode1 = new QStandardItemModel(0,2,this);
                m_mode1->setHorizontalHeaderItem(0, new QStandardItem(QString("SEQUENCE")));
                m_mode1->setHorizontalHeaderItem(1, new QStandardItem(QString("MESSAGE")));

                m_msgBox = new QMessageBox();
                m_msgBox->setWindowTitle("Alert");
                m_msgBox->setText("INVALID  ID  ENTERED");
                m_msgBox->show();
                text->setText("");

                m_tablee->setModel(m_mode1);

        }
}

thanks for any help in advance

هل كانت مفيدة؟

المحلول

I dont know if this will fix your problem, because you have a lot of ambiguous member references in this example, but are you sure that you are clearing that QList m_list each time afterwards? Possibly you are just accumulating more items each time.

Try getting rid of the QList+addRow and just do:

m_model->setItem(0,0,item1)
m_model->setItem(0,1,item2)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top