Problem about subclassing tableModel
-
Hi ALL,
I use qt 4.8.4.
I subclass QAbstractTableModel and get data from server using Tcp.I get data in rowCount() and data().int MyTableModel::rowCount(const QModelIndex &parent)const { cSocket.sendData(ModelType,1); waitFlag=1; while(waitFlag); return rowNumber; }
And the waitFlag is changed after receiving data from server;
void MyTableModel::recPacket1(qint64 type,qint64 rowC) { if(ModelType!=type) return; rowNumber=rowC; qDebug()<<"ModelType:"<<type<<"RowCount:"<<rowC; waitFlag=0; }
But the setModel() block the process.In this.
ui->cagroListTableView->setModel(cagroListModel);
I only received one packet on server.But Server send data to client,and client can't process it because setModel blocks(I think).
Who have code that includes getting data from server in rowCount()?Or how to change this code?What reason causes the problem? -
Hi and welcome to devnet,
Your while loop is blocking everything. You should rather implement this asynchronously. So set rowNumber to 0 at the beginning and when you get your data and update the model then you can trigger the appropriate signals that will tell the views to refresh themselves so that rowCount and friends will be called with the newly arrived values.
Hope it helps