Results 1 to 4 of 4

Thread: using event filters with Qlistwidget

  1. #1
    Join Date
    Oct 2009
    Location
    Port Moody
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy 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!

    Qt Code:
    1. bool cecBrowse::eventFilter(QObject *target, QEvent *event)
    2. {
    3. if (target == ui->lbSearchResults) {
    4. if (event->type() == QEvent::DragMove) {
    5. //displayString( "event" );
    6. //QListWidget *listWidge = static_cast<QListWidget *>(target);
    7. //removeDuplicates(listWidge);
    8. ui->leTitleSearch->setText(ui->leTitleSearch->text() + ">");
    9. //event->setAccepted(false);
    10. event->ignore();
    11. return true;
    12. } else
    13. return false;
    14. }
    15. return QWidget::eventFilter(target, event);
    16. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Oct 2009
    Location
    Port Moody
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default 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.

    Qt Code:
    1. Q_DECLARE_METATYPE(QModelIndex); //at start of BookMarkList.cpp file (after #includes)
    2.  
    3. bool BookMarkList::dropMimeData(int index, const QMimeData *data, Qt::DropAction action)
    4. {
    5. QByteArray dropData = data->data("application/x-qabstractitemmodeldatalist");
    6. QDataStream stream(&dropData, QIODevice::ReadOnly);
    7. QString itemString;
    8. int iNum = 0;
    9. while (!stream.atEnd())
    10. {
    11. QVariant qVar;
    12. stream >> qVar;
    13. QString stText = qVar.typeName();
    14. if ( stText == "QModelIndex" )
    15. {
    16. QModelIndex item = qVar.value<QModelIndex>();
    17. itemString = item.data().toString();
    18. break;
    19. }
    20. iNum++;
    21. }
    22. QMessageBox msgBox;
    23. msgBox.setText( itemString );
    24. msgBox.exec();
    25. //QListWidget::dropMimeData(index, data, action);
    26. return true;
    27. }
    To copy to clipboard, switch view to plain text mode 

    Any help would be greatly appreciated as I have not been able to find any information on this.
    Thanks!!

  3. #3
    Join Date
    Oct 2009
    Location
    Port Moody
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default 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:

    Qt Code:
    1. bool BookMarkList::dropMimeData(int index, const QMimeData *data, Qt::DropAction action)
    2. {
    3. QByteArray dropData = data->data("application/x-qabstractitemmodeldatalist");
    4. QDataStream stream(&dropData, QIODevice::ReadOnly);
    5. QString itemString = "";
    6. int iRow, iCol;
    7. while (!stream.atEnd())
    8. {
    9. QMap<int, QVariant> v;
    10. stream >> iRow >> iCol >> v;
    11. QList<QVariant> lsVars;
    12. lsVars = v.values();
    13. QVariant qVar = lsVars.at(0);
    14. itemString = qVar.toString();
    15. }
    16. if ( itemString.length() > 0 )
    17. {
    18. QList<QListWidgetItem *> lsMatch = findItems( itemString, Qt::MatchFixedString );
    19. if ( !lsMatch.isEmpty() )
    20. return true;
    21. }
    22. QListWidget::dropMimeData(index, data, action);
    23. return true;
    24. }
    To copy to clipboard, switch view to plain text mode 

    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.

  4. #4
    Join Date
    Mar 2011
    Posts
    4
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows Symbian S60

    Default Re: using event filters with Qlistwidget

    You can try qDebug() for debugging

Similar Threads

  1. Replies: 4
    Last Post: 19th February 2009, 11:10
  2. Handling mouse over event for QListWidget
    By Abc in forum Qt Programming
    Replies: 2
    Last Post: 22nd July 2008, 18:32
  3. how to send a emulated mouse event to QListWidget
    By yxmaomao in forum Qt Programming
    Replies: 4
    Last Post: 22nd July 2008, 02:49
  4. Qt event queue overloading?
    By gct in forum Qt Programming
    Replies: 3
    Last Post: 17th March 2008, 18:39
  5. should event filters be used only for debugging
    By babu198649 in forum Qt Programming
    Replies: 1
    Last Post: 21st November 2007, 14:59

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.