PDA

View Full Version : using event filters with Qlistwidget



thedude
30th October 2009, 15:34
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!



bool cecBrowse::eventFilter(QObject *target, QEvent *event)
{
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);
}

thedude
2nd November 2009, 19:32
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.


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");
QDataStream stream(&dropData, QIODevice::ReadOnly);
QString itemString;
int iNum = 0;
while (!stream.atEnd())
{
QVariant qVar;
stream >> qVar;
QString stText = qVar.typeName();
if ( stText == "QModelIndex" )
{
QModelIndex item = qVar.value<QModelIndex>();
itemString = item.data().toString();
break;
}
iNum++;
}
QMessageBox msgBox;
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!!

thedude
2nd November 2009, 21:12
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:


bool BookMarkList::dropMimeData(int index, const QMimeData *data, Qt::DropAction action)
{
QByteArray dropData = data->data("application/x-qabstractitemmodeldatalist");
QDataStream stream(&dropData, QIODevice::ReadOnly);
QString itemString = "";
int iRow, iCol;
while (!stream.atEnd())
{
QMap<int, QVariant> v;
stream >> iRow >> iCol >> v;
QList<QVariant> lsVars;
lsVars = v.values();
QVariant qVar = lsVars.at(0);
itemString = qVar.toString();
}
if ( itemString.length() > 0 )
{
QList<QListWidgetItem *> lsMatch = findItems( itemString, Qt::MatchFixedString );
if ( !lsMatch.isEmpty() )
return true;
}
QListWidget::dropMimeData(index, data, action);
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.

tomhanks
14th November 2011, 19:39
You can try qDebug() for debugging