DoubleClick functionality in TreeWidget which is uses a delegate when hover over item
I have a treeWidget which displays the items as buttons when hover over them. I use a custom class derived from QStyledItemDelegate to do that.
The problem is that I also need the functionality of doubleClick on an item. SlotItemPressed() never gets called the way it is now. Is there a possibility to do both?
Code:
{
Q_OBJECT
public:
MyTreeWidget
(QWidget *parent
= NULL);
~MyTreeWidget() {}
public slots:
{
qDebug() << "slotItemPressed\n";
}
};
Here is the implementation MyTreeWidget.cpp
Code:
{
setMouseTracking(true);
}
{
setCurrentIndex( indexAt(event->pos()));
}
Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over
Does anybody have some inputs? Please let me know if my explanation wasn't clear enough. I'm happy to provide more details...
Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over
I've copy-pasted your code and it works with standard QTreeWidgetItems, maybe post your StyledItemDelegate class as well.
Here is code I've tested:
Code:
// test.h
#ifndef TEST_H
#define TEST_H
#include <QtGui>
Q_OBJECT
public:
MyTreeWidget
( QWidget * parent
=NULL);
~MyTreeWidget(){}
public slots:
{
qDebug() << "slotItemPressed" << index;
}
};
#endif
// test.cpp
#include "test.h"
{
setMouseTracking(true);
}
{
setCurrentIndex( indexAt(event->pos()));
}
int main(int argc, char *argv[])
{
MyTreeWidget * w = new MyTreeWidget();
w->show();
return a.exec();
}
Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over
Thanks for the reply. The problem is that the delegate is pretty much "in the way" when instantiated while hover over. The underlying QTreeWidgetItem won't get the double click.
Here is the StyleDelegate class:
Code:
#pragma once
#include <QStyledItemDelegate>
class MyDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
~MyDelegate() {}
public:
private:
};
Code:
#include "MyDelegate.h"
#include <QtGui>
{
}
{
if(index.column() == 0)
{
model->setData(index, btn->text());
} else {
QStyledItemDelegate::setModelData(editor, model, index);
}
}
{
if(index.column() == 0)
{
QString s1
(index.
model()->data
(index, Qt
::EditRole).
toString());
QString s2
(index.
data(Qt
::DisplayRole).
toString());
return btn;
}
return 0;
}
{
if(!btn)
return;
btn->setText(index.model()->data(index, Qt::EditRole).toString());
}
And in main(), I call:
Code:
MyTreeWidget* tree = new MyTreeWidget;
tree->setItemDelegate(new MyDelegate(tree));
tree->setColumnCount(1);
item01->setFlags(item01->flags() | Qt::ItemIsEditable);
tree->addTopLevelItem(item01);
item01->setText(0, "item01");
item02->setFlags(item02->flags() | Qt::ItemIsEditable);
tree->addTopLevelItem(item02);
item02->setText(0, "item02");
tree->show();
Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over
There is nothing in your delegate that would prevent signals to be delivered. You probably have double click enabled as one of the edit triggers for your view. Try changing that and it should work.
Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over
No, I don't set the only trigger I set is in TreeWidget:
Any other ideas?
Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over
Please provide a minimal compilable example reproducing the problem.
Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over
here is all the code to reproduce the problem: SlotItemPressed() will never be called when double clicking the row:
Code:
// MyTreeWidget.h
#pragma once
#include <QTreeWidget>
#include <iostream>
#include <QDebug>
{
Q_OBJECT
public:
MyTreeWidget
(QWidget *parent
= NULL);
public slots:
};
// MyTreeWidget.cpp
#include "MyTreeWidget.h"
#include <QMouseEvent>
#include <QWidget>
{
setMouseTracking(true);
}
{
setCurrentIndex( indexAt(event->pos()));
}
{
qDebug() << "I never get here\n";
}
#include "moc_MyTreeWidget.cpp"
// MyDelegate.h
#pragma once
#include <QStyledItemDelegate>
class MyDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
public:
};
// MyDelegate.cpp
#include "MyDelegate.h"
#include <QtGui>
MyDelegate
::MyDelegate(QObject *parent
) : QStyledItemDelegate
(parent
){
}
{
if(index.column() == 0)
{
model->setData(index, btn->text());
} else {
QStyledItemDelegate::setModelData(editor, model, index);
}
}
{
if(index.column() == 0)
{
QString s1
(index.
model()->data
(index, Qt
::EditRole).
toString());
QString s2
(index.
data(Qt
::DisplayRole).
toString());
return btn;
}
return 0;
}
{
if(!btn)
return;
btn->setText(index.model()->data(index, Qt::EditRole).toString());
}
// main.cpp
#include <QtGui/QApplication>
#include "MyDelegate.h"
#include "MyTreeWidget.h"
int main(int argc, char *argv[])
{
MyTreeWidget* tree = new MyTreeWidget;
tree->setItemDelegate(new MyDelegate(tree));
tree->setColumnCount(1);
item01->setFlags(item01->flags() | Qt::ItemIsEditable);
tree->addTopLevelItem(item01);
item01->setText(0, "item01");
item02->setFlags(item02->flags() | Qt::ItemIsEditable);
tree->addTopLevelItem(item02);
item02->setText(0, "item02");
tree->show();
return app.exec();
}
Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over
Works fine for me... Unless of course the editor is opened and intercepts the double click but that's rather obvious.
Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over
Quote:
Originally Posted by
wysota
Works fine for me... Unless of course the editor is opened and intercepts the double click but that's rather obvious.
You mean it works for you and SlotItemPressed() gets called? You mentioned earlier: "There is nothing in your delegate that would prevent signals to be delivered." Does the delegate now intercept the double click or deliver it?
Re: DoubleClick functionality in TreeWidget which is uses a delegate when hover over
Quote:
Originally Posted by
torres
You mean it works for you and SlotItemPressed() gets called?
Yes.
Quote:
You mentioned earlier: "There is nothing in your delegate that would prevent signals to be delivered." Does the delegate now intercept the double click or deliver it?
Why would the delegate intercept double click? You'd have to reimplement editorEvent() for that to happen.