PDA

View Full Version : QTableView crashes after doubleclick



nickla
18th March 2011, 17:30
I have QTableView with QAbctractTableModel. One column can edit.

When I doubleclick on it, editor is shown. I try to doubleclick another cell of this collumn and my application crashes.

This is my code for editing:


QVariant TxTableModelOrder::data(const QModelIndex &index, int role) const
{
if (role == Qt::DisplayRole) {
return m_Data.getCellValue(index.row(), index.column());
}
if (role == Qt::EditRole) {
return m_Data.getCellValue(index.row(), index.column());
}
if (role == Qt::BackgroundColorRole) {
TxOrder order;
return order.getStatusColor(m_Data.getCellValue(index.row (), QString("order_status")).toInt());
}
return QVariant();
}

Qt::ItemFlags TxTableModelOrder::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QAbstractTableModel::flags(index);
QString columnName = m_Data.getColumnName(index.column());

if (columnName == "driver_callsign") {
flags |= Qt::ItemIsEditable;
}

return flags;
}

bool TxTableModelOrder::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::EditRole) {
QString columnName = m_Data.getColumnName(index.column());

if (columnName == "driver_callsign") {
QString primaryKey = m_Data.getRowFieldValue(index.row(), m_Data.getKeyField());
QString callsign = value.toString();
TxOrder order;

try {
if (callsign.isEmpty()) {
order.clearDriverOnOrder(primaryKey);
} else {
order.setDriverToOrderByCallsign(primaryKey, callsign);
}
} catch (TxException ex) {
qDebug() << ex.getMessage();
}
}
}
return true;
}


I can not find what is going wrong! Help please.

Added after 15 minutes:

Error message is:

QObject::installEventFilter(): Cannot filter events for objects in a different thread.

Santosh Reddy
18th March 2011, 19:08
Do you use QThreads?

nickla
18th March 2011, 20:22
No! I use QTimer for data download.



TxTableModelOrder::TxTableModelOrder(QTableView *parent) :
QAbstractTableModel(parent)
{
this->parent = parent;
timer = new QTimer(this);

connect(&http, SIGNAL(done(bool)), this, SLOT(httpDone(bool)));
connect(timer, SIGNAL(timeout()), this, SLOT(timerDone()));
timer->start(1000);
}



Added after 1 8 minutes:

I have fixed this problem. I want somebody who will get this error to know what is wrong.

Look at this code line 13 and 15:


bool TxTableModelOrder::setData(const QModelIndex &index, const QVariant &value, int role)
{
if (role == Qt::EditRole) {
QString columnName = m_Data.getColumnName(index.column());

if (columnName == "driver_callsign") {
QString primaryKey = m_Data.getRowFieldValue(index.row(), m_Data.getKeyField());
QString callsign = value.toString();
TxOrder order;

try {
if (callsign.isEmpty()) {
order.clearDriverOnOrder(primaryKey);
} else {
order.setDriverToOrderByCallsign(primaryKey, callsign);
}
} catch (TxException ex) {
qDebug() << ex.getMessage();
}
timer->start(1000);
}
}
return true;
}


There are http sync requests to server on these lines. That means we wait till server sends response, but QTableView can not wait when user decided to doubleclick in other cell and app crash.

Use async request in setData - this is fix for my problem.