I am using QTableview + QAbastractTableModel subclass to display order trading data. It works great. But when I use QSortFilterProxyModel for table sorting/filtering, the application crashes if I sort the rows and then add new data row. Sometimes, even a click in the table would result in a crash. I have not changed any codes in the Model . The following are the codes related. Please kindly help.
VIEW:
pModel = new TradeManagerModel(0);
proxyModel->setSourceModel(pModel);
proxyModel->setDynamicSortFilter(true);
pTableView->setModel(proxyModel);
pTableView->setSortingEnabled(true);
pTableView = new QTableView();
pModel = new TradeManagerModel(0);
proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(pModel);
proxyModel->setDynamicSortFilter(true);
pTableView->setModel(proxyModel);
pTableView->setSortingEnabled(true);
To copy to clipboard, switch view to plain text mode
MODEL: The data is stored in QMap<QString, QStringList> mGridData.
int TradeManagerModel
::rowCount(const QModelIndex & /*parent*/) const {
return mGridData.size();
}
int TradeManagerModel
::columnCount(const QModelIndex & /*parent*/) const {
return 10;
}
{
if (role == Qt::DisplayRole)
{
return mGridData[mGridData.keys().at(index.row())].at(index.column());
}
}
QVariant TradeManagerModel
::headerData(int section, Qt
::Orientation orientation,
int role
) const {
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal) {
switch (section)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
}
}
}
}
void TradeManagerModel::onTradeItemUpdated(const TradeItem &tradeItem)
{
tmpStrList << tradeItem.tradeTimeStamp
<< tradeItem.symbol
<< tradeItem.action
<<
QString::number(tradeItem.
fillQty) <<
QString::number(tradeItem.
fillPrice) <<
QString::number(tradeItem.
commission) << tradeItem.orderRef
<< execId
<< tradeItem.orderId
<<"";
if(!mGridData.contains(execId))
{
mGridData[execId] = tmpStrList;
emit layoutChanged();
}
}
int TradeManagerModel::rowCount(const QModelIndex & /*parent*/) const
{
return mGridData.size();
}
int TradeManagerModel::columnCount(const QModelIndex & /*parent*/) const
{
return 10;
}
QVariant TradeManagerModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole)
{
return mGridData[mGridData.keys().at(index.row())].at(index.column());
}
return QVariant();
}
QVariant TradeManagerModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (role == Qt::DisplayRole)
{
if (orientation == Qt::Horizontal) {
switch (section)
{
case 0:
return QString("TIME");
case 1:
return QString("SYMBOL");
case 2:
return QString("B/S");
case 3:
return QString("QTY");
case 4:
return QString("PRICE");
case 5:
return QString("COMM");
case 6:
return QString("STRATEGYID");
case 7:
return QString("EXECID");
case 8:
return QString("ORDERID");
case 9:
return QString("");
}
}
}
return QVariant();
}
void TradeManagerModel::onTradeItemUpdated(const TradeItem &tradeItem)
{
QString execId = tradeItem.execId;
QStringList tmpStrList;
tmpStrList << tradeItem.tradeTimeStamp
<< tradeItem.symbol
<< tradeItem.action
<< QString::number(tradeItem.fillQty)
<< QString::number(tradeItem.fillPrice)
<< QString::number(tradeItem.commission)
<< tradeItem.orderRef
<< execId
<< tradeItem.orderId
<<"";
if(!mGridData.contains(execId))
{
mGridData[execId] = tmpStrList;
emit layoutChanged();
}
}
To copy to clipboard, switch view to plain text mode
Bookmarks