using event filters with Qlistwidget
Hi, I'm having a problem capturing an event with a QListWidget. To be more specific what I'm trying to do is not allow a drag&drop from one QListWidget to another if that item (QString value) already exists. In the code below I've commented out the actual 'removal' of duplicates code, and just put a simple display mechanism to show when an event is captured.
I have set the eventfilter in the constructor of the class to enabled:
'ui->lbBookmarks->installEventFilter(this);'
It seems though that the events are not being captured, ie my lineEdit is not being changed. I have tried different values for QEvent::xxxxx, and it has triggered events, but it was always sporadic, one time it would capture the event, the next drag it wouldn't capture???
Not sure where I've gone wrong here, or if there is a better way of handling this.
Thanks!
Code:
{
if (target == ui->lbSearchResults) {
if (event
->type
() == QEvent::DragMove) { //displayString( "event" );
//QListWidget *listWidge = static_cast<QListWidget *>(target);
//removeDuplicates(listWidge);
ui->leTitleSearch->setText(ui->leTitleSearch->text() + ">");
//event->setAccepted(false);
event->ignore();
return true;
} else
return false;
}
return QWidget::eventFilter(target, event
);
}
Re: using event filters with Qlistwidget
Further to my previous post, I've tried to solve this one on my own, however I've hit another hurdle. So I've subclassed QListWidget (called it BookMarkList) in order to intercept the drag&drop and hopefully ignore any drops of existing text. However I can't seem to get the value of the drag text. When I run the program I only seem to get a blank string in 'itemString'. Note that the msgBox is only being used for debugging.
Code:
Q_DECLARE_METATYPE(QModelIndex);
//at start of BookMarkList.cpp file (after #includes)
bool BookMarkList
::dropMimeData(int index,
const QMimeData *data, Qt
::DropAction action
) {
QByteArray dropData
= data
->data
("application/x-qabstractitemmodeldatalist");
int iNum = 0;
while (!stream.atEnd())
{
stream >> qVar;
if ( stText == "QModelIndex" )
{
itemString = item.data().toString();
break;
}
iNum++;
}
msgBox.setText( itemString );
msgBox.exec();
//QListWidget::dropMimeData(index, data, action);
return true;
}
Any help would be greatly appreciated as I have not been able to find any information on this.
Thanks!!
Re: using event filters with Qlistwidget
Nevermind, problem solved... yet again this forum proved itself a usefull resource (2 posts and no responses).
Anyway the solution was found in qabstractitemmodel.cpp in the QT source files. Here is the reimplementation that worked:
Code:
bool BookMarkList
::dropMimeData(int index,
const QMimeData *data, Qt
::DropAction action
) {
QByteArray dropData
= data
->data
("application/x-qabstractitemmodeldatalist");
int iRow, iCol;
while (!stream.atEnd())
{
QMap<int, QVariant> v;
stream >> iRow >> iCol >> v;
QList<QVariant> lsVars;
lsVars = v.values();
itemString = qVar.toString();
}
if ( itemString.length() > 0 )
{
QList<QListWidgetItem *> lsMatch = findItems( itemString, Qt::MatchFixedString );
if ( !lsMatch.isEmpty() )
return true;
}
return true;
}
Of course this code would have to be modified to allow for other sources and not just the assumed other listwidget on the program. Would have been nice if they had added a property to QListWidget, something like: 'bool allowDuplicates', but oh well.
Re: using event filters with Qlistwidget
You can try qDebug() for debugging