Results 1 to 4 of 4

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.

    Previously I put this message in the forum for newbies, but no such luck. So I have decided to change it to this forum. Sorry for the inconvenience.

    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
    Mar 2010
    Posts
    9
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

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

    Hello again.

    I changed the code to check if events are implemented correctly.

    Qt Code:
    1. #include "indicatorwidget.h"
    2.  
    3. #include <iostream>
    4.  
    5. IndicatorComboBox::IndicatorComboBox(QWidget *parent) : QComboBox(parent)
    6. {
    7. setAcceptDrops(true);
    8. }
    9.  
    10. void IndicatorComboBox::mousePressEvent(QMouseEvent *pEvent)
    11. {
    12. std::cout << "mousePressEvent" << std::endl;
    13.  
    14. if (pEvent->button() == Qt::LeftButton) {
    15. m_dragStartPosition = pEvent->pos();
    16. }
    17.  
    18. QComboBox::mousePressEvent(pEvent);
    19. }
    20.  
    21. void IndicatorComboBox::mouseMoveEvent(QMouseEvent *pEvent)
    22. {
    23. std::cout << "mouseMoveEvent" << std::endl;
    24.  
    25. if (pEvent->buttons() & Qt::LeftButton) {
    26. int distance = (pEvent->pos() - m_dragStartPosition).manhattanLength();
    27. if (distance >= QApplication::startDragDistance()) {
    28. startDrag();
    29. }
    30. }
    31.  
    32. QComboBox::mouseMoveEvent(pEvent);
    33. }
    34.  
    35. void IndicatorComboBox::startDrag()
    36. {
    37. QString item = currentText();
    38.  
    39. std::cout << "startDrag" << std::endl;
    40.  
    41. if (!item.isEmpty()) {
    42. QMimeData *mimeData = new QMimeData;
    43. mimeData->setText(item);
    44. QDrag *drag = new QDrag(this);
    45. drag->setMimeData(mimeData);
    46. drag->exec(Qt::CopyAction);
    47. }
    48. }
    49.  
    50. void IndicatorComboBox::dragEnterEvent(QDragEnterEvent *pEvent)
    51. {
    52. IndicatorComboBox *source = qobject_cast<IndicatorComboBox *>(pEvent->source());
    53.  
    54. std::cout << "dragEnterEvent\n" << std::endl;
    55.  
    56. if (source && source != this) {
    57. pEvent->setDropAction(Qt::CopyAction);
    58. pEvent->accept();
    59. }
    60. }
    61.  
    62. void IndicatorComboBox::dragMoveEvent(QDragMoveEvent *pEvent)
    63. {
    64.  
    65. IndicatorComboBox *source = qobject_cast<IndicatorComboBox *>(pEvent->source());
    66.  
    67. std::cout << "dragEnterEvent" << std::endl;
    68.  
    69. if (source && source != this) {
    70. pEvent->setDropAction(Qt::CopyAction);
    71. pEvent->accept();
    72. }
    73. }
    74.  
    75. IndicatorWidget::IndicatorWidget(QWidget *parent) : QWidget(parent)
    76. {
    77. m_horizontalLayout = new QHBoxLayout;
    78. m_indicatorLabel = new QLabel(this);
    79. m_indicatorComboBox = new IndicatorComboBox(this);
    80.  
    81. m_indicatorLabel->setAlignment(Qt::AlignCenter);
    82.  
    83. m_horizontalLayout->addWidget(m_indicatorLabel);
    84. m_horizontalLayout->addWidget(m_indicatorComboBox);
    85.  
    86. m_indicatorLabel->setText(tr("Indicador:"));
    87. m_indicatorComboBox->insertItems(0, QStringList() << tr("RSI") << tr("Volumen"));
    88.  
    89. setLayout(m_horizontalLayout);
    90.  
    91. adjustSize();
    92. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef _INDICATORWIDGET_H_
    2. #define _INDICATORWIDGET_H_
    3.  
    4. #include <QtGui>
    5.  
    6. class IndicatorComboBox : public QComboBox
    7. {
    8. Q_OBJECT
    9.  
    10. protected:
    11. QPoint m_dragStartPosition;
    12.  
    13. void mousePressEvent(QMouseEvent *pEvent);
    14. void mouseMoveEvent(QMouseEvent *pEvent);
    15. void dragEnterEvent(QDragEnterEvent *pEvent);
    16. void dragMoveEvent(QDragMoveEvent *pEvent);
    17.  
    18. void startDrag(void);
    19.  
    20. public:
    21. IndicatorComboBox(QWidget *parent = 0);
    22.  
    23. signals:
    24.  
    25. public slots:
    26.  
    27. };
    28.  
    29. class IndicatorWidget : public QWidget
    30. {
    31. Q_OBJECT
    32.  
    33. protected:
    34. QHBoxLayout *m_horizontalLayout;
    35. QLabel *m_indicatorLabel;
    36. IndicatorComboBox *m_indicatorComboBox;
    37.  
    38. public:
    39. IndicatorWidget(QWidget *parent = 0);
    40.  
    41. signals:
    42.  
    43. public slots:
    44.  
    45. };
    46.  
    47. #endif /* _INDICATORWIDGET_H_ */
    To copy to clipboard, switch view to plain text mode 

    My surprise has been that events mouseMoveEvent (), dragEnterEvent () and dragMoveEvent () not run if you run the event QComboBox: mousePressEvent (line 18). If I delete line 18, all events are executed but the combobox will not open. Any ideas?

    Thanks In advance.

  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?

    Hello again.

    I'm still trying to solve my problem with drag & drop of QComboBox. Can this be done or am I wasting time?

    Thanks in advance.

  4. #4
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

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

    From the Qt docs: here
    The standard views automatically support internal drag and drop, where items are moved around to change the order in which they are displayed. By default, drag and drop is not enabled for these views . . . .
    So, if you just want to drag items around in the popup of your combo box all you need is the following statement:
    Qt Code:
    1. m_indicatorComboBox->view()->setDragDropMode(QAbstractItemView::DragDrop);
    To copy to clipboard, switch view to plain text mode 
    If you want to drag to another standard view you only need a few additional lines of code as explained in the Qt doc linked above.

Similar Threads

  1. Replies: 2
    Last Post: 30th March 2010, 22:51
  2. Qcombobox item rect?
    By shud in forum Qt Programming
    Replies: 0
    Last Post: 22nd November 2009, 12:07
  3. QcomboBox item height
    By Beppe in forum Qt Programming
    Replies: 9
    Last Post: 16th June 2009, 11:31
  4. QComboBox item disable?
    By Equilibrium in forum Qt Programming
    Replies: 8
    Last Post: 28th November 2007, 17:41
  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.