Hi,
I am using QMimeData .userData() function to set a pointer to a custom object directly in a QMimeData object.
This is working fine so far, but since I use that method, when I close my application, I got an error showing “Application stopped working”, and I can’t seem to find where this error come from. Is there a way to debug and know where this error come from? In the error log it says fault come from “Qt5Core.dll”
I made a small video showing the error in action – 30sec video here [youtube.com]
There are small bug in the interface that still needs fixing (problem with paint delegate..)
Thanks if you can help me, If you need financial compensation I would gladly need an helper to finish this QListView model faster
Here is my ListModel code:
IntervalListModel::IntervalListModel(QList<Interval*> lstInterval, QObject *parent) : QAbstractListModel(parent) {
this->lstInterval = lstInterval;
}
QVariant IntervalListModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (role != Qt::DisplayRole)
return QVariant();
if (orientation == Qt::Horizontal)
return QString("Column %1").arg(section);
else
return QString("Row %1").arg(section);
}
QVariant IntervalListModel::data(const QModelIndex &index, int role) const {
if (!index.isValid())
return QVariant();
if (index.row() >= lstInterval.size() || index.row() < 0)
return QVariant();
if (role == Qt::DisplayRole || role == Qt::EditRole) {
Interval *interval = lstInterval.at(index.row());
QVariant v = qVariantFromValue((void *) interval);
return v;
}
else {
return QVariant();
}
}
bool IntervalListModel::insertRows(int position, int rows, const QModelIndex &index) {
Q_UNUSED(index);
beginInsertRows(QModelIndex(), position, position + rows - 1);
qDebug() << "INSERT ROW";
for (int row = 0; row < rows; ++row) {
QTime time1(0, 5, 0);
Interval *interval = new Interval(time1, Interval::INTERVAL, "display_msg",
Interval::FLAT, .50, .50, 20,
Interval::FLAT, 100, 100, 15,
Interval::FLAT, .60, .60, 50);
lstInterval.insert(position, interval);
}
endInsertRows();
return true;
}
bool IntervalListModel::removeRows(int position, int rows, const QModelIndex &index)
{
Q_UNUSED(index);
beginRemoveRows(QModelIndex(), position, position + rows - 1);
qDebug() << "REMOVE ROW";
for (int row = 0; row < rows; ++row) {
lstInterval.removeAt(position);
}
endRemoveRows();
return true;
}
bool IntervalListModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
qDebug() << "SET DATA";
if (index.isValid() && role == Qt::EditRole) {
Interval *newIntervalPtr;
newIntervalPtr = (Interval*) value.value<void *>();
Interval *oldInterval = lstInterval.at(index.row());
/// Change old interval ptr for the new one
oldInterval = newIntervalPtr;
lstInterval.replace(index.row(), oldInterval);
emit dataChanged(index, index);
return true;
}
return false;
}
QList<Interval*> IntervalListModel::getLstInterval() {
return this->lstInterval;
}
Qt::ItemFlags IntervalListModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags defaultFlags = QAbstractListModel::flags(index);
if (index.isValid())
return Qt::ItemIsDragEnabled | Qt::ItemIsEditable |defaultFlags;
else
return Qt::ItemIsDropEnabled | defaultFlags;
}
QStringList IntervalListModel::mimeTypes() const {
QStringList types;
types << "maxtrainer/interval";
return types;
}
bool IntervalListModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) {
qDebug() << "DROPMINEDATA";
if (action == Qt::IgnoreAction)
return true;
if (!data->hasFormat("maxtrainer/interval"))
return false;
if (column > 0)
return false;
int beginRow;
if (row != -1)
beginRow = row;
else if (parent.isValid())
beginRow = parent.row();
else
beginRow = rowCount(QModelIndex());
/// Retrieve object from MimeData
QObjectUserData *dataObject= data->userData(0);
Interval *interval2= (Interval*) dataObject;
qDebug() << "test convertAfterDrop"<< interval2->getDisplayMessage();
insertRow(beginRow, QModelIndex());
QModelIndex idx = index(beginRow, 0, QModelIndex());
QVariant v = qVariantFromValue((void *) interval2);
setData(idx, v, Qt::EditRole);
return true;
}
QMimeData *IntervalListModel::mimeData(const QModelIndexList &indexes) const {
qDebug() << "MIME_DATA";
QMimeData *mimeData = new QMimeData();
foreach (QModelIndex index, indexes) {
if (index.isValid()) {
QVariant variant = data(index, Qt::DisplayRole);
Interval *interval = (Interval*) variant.value<void *>();
qDebug() << "ok pointer Retrieved from QVariant" << interval->getDisplayMessage();
mimeData->setUserData(0, ((QObjectUserData*)interval) );
}
}
mimeData->setData("maxtrainer/interval", QByteArray() ); ///Nothing to save here, saved in UserData
return mimeData;
}
Qt::DropActions IntervalListModel::supportedDropActions() const {
return Qt::MoveAction;
}
↧