PDA

View Full Version : QComboBox Item Right Click



wirasto
13th January 2013, 10:49
How to catch rightclick event on QComboBox item? I tried reimplement mousePressEvent for my custom combobox, but not luck :( The event just for ComboBox it self, not for Item.

Viper666
13th January 2013, 11:04
http://doc.qt.digia.com/qt/qcombobox.html#setItemDelegate implement own QAbstractItemDelegate and implement own events. Afte that set item delegate. And wala :D it will work :D

wirasto
13th January 2013, 12:31
Can I use mousePressEvent in abstractitemdelegate?

Btw, I want make different emit signal for left and right click on combobox item.

Viper666
13th January 2013, 12:49
Yes of course and now example for mouseevent:

void mouseReleaseEvent ( QMouseEvent * event )
{
if(event->button() == Qt::RightButton)
{
emit rightClick();
}
if(event->button() == Qt::LeftButton)
{
emit leftClick();
}

};
You can use mouseReleaseEvent or MousePressEvent

wirasto
13th January 2013, 14:44
Not work :(



void ComboBoxDelegate::mousePressEvent(QMouseEvent *e)
{
if (e->button()==Qt::LeftButton) {
qDebug() << "Klik Kiri";
}

if (e->button()==Qt::RightButton) {
qDebug() << "Klik Kanan";
}
}


Btw, how can I know index of item selected?

Viper666
13th January 2013, 16:30
Oh sorry my mistake i mean QItemDelegate
At first you must have working delegate like in this example http://programmingexamples.net/wiki/Qt/Delegates/ComboBoxDelegate and when you own delegate works you can implement mouseEvent if it will not work post here you code.

wirasto
13th January 2013, 16:38
Of course I use QItemDelegate. But not work :(



#ifndef COMBOBOXDELEGATE_H
#define COMBOBOXDELEGATE_H

#include <QItemDelegate>

class ComboBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
explicit ComboBoxDelegate(QObject *parent = 0);

protected:
void mousePressEvent(QMouseEvent *e);

signals:

public slots:

};

#endif // COMBOBOXDELEGATE_H





#include "comboboxdelegate.h"
#include <QMouseEvent>
#include <QDebug>

ComboBoxDelegate::ComboBoxDelegate(QObject *parent) :
QItemDelegate(parent)
{
}

void ComboBoxDelegate::mousePressEvent(QMouseEvent *e)
{
if (e->button()==Qt::LeftButton) {
qDebug() << "Klik Kiri";
}

if (e->button()==Qt::RightButton) {
qDebug() << "Klik Kanan";
}
}


I only need catch mouse event. And get what item index is selected...

Viper666
13th January 2013, 17:18
I will write a example for you. But now i can' sorry. I don't know why it doesn't work but i think it has some solution

wirasto
13th January 2013, 17:27
Oh, no problem.
Btw, thanks for your response :)

Viper666
13th January 2013, 19:44
ok i solved it.
Solution: in QItemDelegate isn't mousePressEvent or something like that this are events from Qobject but this don't work for QItemDelegate you need implement editorEvent:

bool Item::editorEvent(QEvent *event, QAbstractItemModel *model, const QStyleOptionViewItem &option, const QModelIndex &index)
{

if (event->type() == QEvent::MouseButtonPress)
{
QMouseEvent *mouseEvent = static_cast<QMouseEvent*>(event);
if(mouseEvent->button() == Qt::LeftButton)
qDebug() << "leftButton";



if(mouseEvent->button() == Qt::RightButton)
qDebug() << "rightButton";
}
}

:D