jjcarles
31st March 2010, 09:03
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.
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.
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.
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.