PDA

View Full Version : Is it possible to drag an item of a QComboBox?



jjcarles
30th March 2010, 22:00
Hi.

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



class IndicatorComboBox : public QComboBox
{
Q_OBJECT

protected:
QPoint m_dragStartPosition;

void mousePressEvent(QMouseEvent *pEvent);
void mouseMoveEvent(QMouseEvent *pEvent);

public:
IndicatorComboBox(QWidget *parent = 0);

signals:

public slots:

}




IndicatorComboBox::IndicatorComboBox(QWidget *parent) : QComboBox(parent)
{
}

void IndicatorComboBox::mousePressEvent(QMouseEvent *pEvent)
{
if (pEvent->button() == Qt::LeftButton) {
m_dragStartPosition = pEvent->pos();
}

QComboBox::mousePressEvent(pEvent);
}

void IndicatorComboBox::mouseMoveEvent(QMouseEvent *pEvent)
{
if (!(pEvent->buttons() & Qt::LeftButton)) {
return;
}

if (currentIndex() < 0) {
return;
}

if ((pEvent->pos() - m_dragStartPosition).manhattanLength() < QApplication::startDragDistance()) {
return;
}

QDrag *drag = new QDrag(this);
QMimeData *mimeData = new QMimeData;

mimeData->setText(currentText());
drag->setMimeData(mimeData);

//drag->start();
Qt::DropAction dropAction = drag->exec(Qt::CopyAction | Qt::MoveAction);
}



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.

wysota
30th March 2010, 22:11
Did you think about calling the base class implementation of the mouseMoveEvent as well?

jjcarles
30th March 2010, 22:51
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?