Results 1 to 3 of 3

Thread: Is it possible to drag an item of a QComboBox?

  1. #1
    Join Date
    Mar 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Is it possible to drag an item of a QComboBox?

    Hi.

    I'm trying to create a class that allows me to drag & drop items from a QComboBox.

    Qt Code:
    1. class IndicatorComboBox : public QComboBox
    2. {
    3. Q_OBJECT
    4.  
    5. protected:
    6. QPoint m_dragStartPosition;
    7.  
    8. void mousePressEvent(QMouseEvent *pEvent);
    9. void mouseMoveEvent(QMouseEvent *pEvent);
    10.  
    11. public:
    12. IndicatorComboBox(QWidget *parent = 0);
    13.  
    14. signals:
    15.  
    16. public slots:
    17.  
    18. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. IndicatorComboBox::IndicatorComboBox(QWidget *parent) : QComboBox(parent)
    2. {
    3. }
    4.  
    5. void IndicatorComboBox::mousePressEvent(QMouseEvent *pEvent)
    6. {
    7. if (pEvent->button() == Qt::LeftButton) {
    8. m_dragStartPosition = pEvent->pos();
    9. }
    10.  
    11. QComboBox::mousePressEvent(pEvent);
    12. }
    13.  
    14. void IndicatorComboBox::mouseMoveEvent(QMouseEvent *pEvent)
    15. {
    16. if (!(pEvent->buttons() & Qt::LeftButton)) {
    17. return;
    18. }
    19.  
    20. if (currentIndex() < 0) {
    21. return;
    22. }
    23.  
    24. if ((pEvent->pos() - m_dragStartPosition).manhattanLength() < QApplication::startDragDistance()) {
    25. return;
    26. }
    27.  
    28. QDrag *drag = new QDrag(this);
    29. QMimeData *mimeData = new QMimeData;
    30.  
    31. mimeData->setText(currentText());
    32. drag->setMimeData(mimeData);
    33.  
    34. //drag->start();
    35. Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);
    36. }
    To copy to clipboard, switch view to plain text mode 

    My problem is that if there is the line "QComboBox::mousePressEvent(pEvent); " I can't drag the items. And, if I remove the line, the combobox will not open (but the drag works fine).

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Is it possible to drag an item of a QComboBox?

    Did you think about calling the base class implementation of the mouseMoveEvent as well?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Mar 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Is it possible to drag an item of a QComboBox?

    Thanks for responding. I had not thought calling the base class implementation of the "mouseMoveEvent". I tried your idea, but has not worked.. I think the problem is in the event "mousePressEvent" but I don't know how to solve it. Or QComboBox works or works drag & drop, but not both. Any idea?

Similar Threads

  1. Qcombobox item rect?
    By shud in forum Qt Programming
    Replies: 0
    Last Post: 22nd November 2009, 12:07
  2. Styling each item in a QComboBox
    By rishid in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2008, 09:10
  3. QComboBox item disable?
    By Equilibrium in forum Qt Programming
    Replies: 8
    Last Post: 28th November 2007, 17:41
  4. Cannot select an item in QComboBox
    By Declan in forum Qt Programming
    Replies: 2
    Last Post: 19th April 2007, 08:08
  5. QComboBox and item
    By drow in forum Qt Programming
    Replies: 4
    Last Post: 14th June 2006, 09:04

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.