How does the QItemDelegate work?
Hello,
I have followed the example of creating a spinboxdelegate in a QTableWidget.
http://doc.trolltech.com/4.3/itemvie...xdelegate.html
I have subclassed the QTableWidget in order to specify the return as hotkey for both open(F2) and close(return) the spinbox delegate. To do that I have overrided the keyPressEvent. This is the example code of the KeyPressEvent:
Code:
void MyTableWidget
::keyPressEvent(QKeyEvent *e
) {
if(e->key() == Qt::Key_Return)
{
n
= new QKeyEvent(e
->type
(), Qt
::Key_F2, e
->modifiers
(), e
->text
(), e
->isAutoRepeat
(), e
->count
());
}
else
{
n = e;
}
}
I use:
tableWidget->setItemDelegateForColumn(1, new SpinBoxDelegate);
instead of the example codes:
SpinBoxDelegate delegate;
tableView.setItemDelegate(&delegate);
When I crosscompile & and transfer the example code to an embedded system (Arm9, QTopia 4.3) the spinbox delegate and its methods createEditor and setEditorData is never called when the cell is opened for editing?
I really need this to work. I have no idea why the spinbox delegate does not work on the embedded system? If you need further info to be able to help I will provide it for you.
Thank you in advance...
Re: How does the QItemDelegate work?
Why do you override the key event like that? How about handling the key by calling QAbstractItemView::edit() on the cell you want to edit? Just make sure your key handler is called at all.
Re: How does the QItemDelegate work?
Hi wysota,
I dont know if i follow you...do you mean something like this?
Code:
void MyTableWidget
::keyPressEvent(QKeyEvent *e
) {
if(e->key() == Qt::Key_Return)
{
}
}
Re: How does the QItemDelegate work?
Sorry the previous dont work...I mean like this:
Code:
void MyTableWidget
::keyPressEvent(QKeyEvent *e
) {
}
Re: How does the QItemDelegate work?
Using QShortcut is also an option. If you want to edit with any key you might want to take a look at QAbstractItemView::editTriggers.
Re: How does the QItemDelegate work?
Hello again,
These solutions you have proposed works just fine on my laptop...but neither one of them make it possible to use the spinbox delegate on the arm9 and QTopia? It just opens the cell for editing and there is no contact with the spinbox delegate?
Re: How does the QItemDelegate work?
So, there's a QLineEdit editor or what? Could you assure that SpinBoxDelegate::createEditor() gets called?
Re: How does the QItemDelegate work?
You are allocating the delegate on the stack and it gets out of scope immediately after you return from the method that sets the delegate and the delegate gets destroyed and a default delegate takes its place.
Re: How does the QItemDelegate work?
So, there's a QLineEdit editor or what?
How do you mean? I think there is yes?
Could you assure that SpinBoxDelegate::createEditor() gets called?
On my laptop yes. The SpinBoxDelegate::createEditor() is called on my laptop...I can set a toggle break point in it and debug it when called...
But It never stops at the break point when debugging on the arm9? I also have qDebug output traces in each SpinBoxDelegate method but none is called on the arm9?
Im somewhat confused and cannot understand why? Could it be some limitation in QTopia? Differencies in the plattforms?
Re: How does the QItemDelegate work?
wysota I qoute you:
"You are allocating the delegate on the stack and it gets out of scope immediately after you return from the method that sets the delegate and the delegate gets destroyed and a default delegate takes its place."
This sounds very reasonable, it could be. I will look into this tomorrow monday. I was just passing my job to check my mails, but was stuck on this problem I have had for a while.
Re: How does the QItemDelegate work?
If I can override the F2 (open edit) another hotkey using:
QAbstractItemView::edit(currentIndex(), QAbstractItemView::EditKeyPressed, e);
How do I override the up & down buttonwhen in edit mode?
Re: How does the QItemDelegate work?
Reimplement the key event handler for your editor returned by the delegate or install an event filter on the editor and handle those keys in the filter.
Re: How does the QItemDelegate work?
Thank you for your reply wysota
"Reimplement the key event handler for your editor returned by the delegate or install an event filter on the editor and handle those keys in the filter."
I have done this on a subclassed QDoubleSpinbox:
Code:
void DoubleSpinBox
::keyPressEvent(QKeyEvent *e
) {
// UP
if(e->key() == Qt::Key_Launch6)
{
double value = this->text().toDouble();
value = value + singleStep;
emit valueChanged(value);
}
// DOWN
else if(e->key() == Qt::Key_Launch7)
{
double value = this->text().toDouble();
value = value - singleStep;
emit valueChanged(value);
}
}
...and runned the subclassed spinbox on the embedded system and it works alright.
But I still cant get the spinbox delegate example to work on the embedded system with an overrided keypress?
Code:
void MyTableWidget
::keyPressEvent(QKeyEvent *e
) {
switch(e->key())
{
case Qt::Key_Launch5:
{
}
break;
default:
break;
}
}
...open the cell to be edited but the spinbox delegate is still not called (createEditor and setEditorData)? I have taken your previous advice about stack allocation into thought, but I cant see that this is the case?
Re: How does the QItemDelegate work?
Quote:
Originally Posted by
SailinShoes
...and runned the subclassed spinbox on the embedded system and it works alright.
Actually I'm surprised if your DoubleSpinBox::keyPressEvent() works at all because I don't see the value being set anywhere. All you do is reading the value, adjusting it and then you emit a signal which QDoubleSpinBox is supposed to emit on its own when its value changes. Oh, and again, why not use QShortcut which does the trick in one line of code per step direction:
Quote:
Originally Posted by
SailinShoes
But I still cant get the spinbox delegate example to work on the embedded system with an overrided keypress?
Are you using #ifdef's anywhere in your application to do things differently on the embedded device?
Re: How does the QItemDelegate work?
Hi jpn,
This is my subclassed QDoubleSpinBox.h code :
Code:
#ifndef DOUBLESPINBOX_H
#define DOUBLESPINBOX_H
#include <QDoubleSpinBox>
#include <QKeyEvent>
#include <QDebug>
{
Q_OBJECT
public:
~DoubleSpinBox();
public slots:
virtual void setValue(double value);
private:
double presentvalue;
double singleStep;
};
#endif
...and this is my subclassed QDoubleSpinbox.cpp code :
Code:
#include "DoubleSpinBox.h"
DoubleSpinBox
::DoubleSpinBox(QWidget *parent
) {
}
void DoubleSpinBox
::keyPressEvent(QKeyEvent *e
) {
if(e->key() == Qt::Key_A)
{
double value = this->text().toDouble();
value = value + singleStep;
setValue(value);
}
else if(e->key() == Qt::Key_B)
{
double value = this->text().toDouble();
value = value - singleStep;
setValue(value);
}
}
void DoubleSpinBox::setValue(double value)
{
int decimals;
static double oldValue;
decimals = 0;
singleStep = 0;
qDebug() << "value = "<< value;
if( value >= 0.500 && value <= 0.999 )
{
decimals = 3;
singleStep = 0.001;
if(oldValue == 1.00)
{
value = 0.999;
}
}
else if(value >= 1.00 && value <= 9.99)
{
decimals = 2;
singleStep = 0.01;
if(oldValue == 10.0)
{
value = 9.99;
}
}
else if(value >= 10.0 && value <= 99.9)
{
decimals = 1;
singleStep = 0.1;
if(oldValue == 100)
{
value = 99.9;
}
}
else if(value >= 100 && value <= 999)
{
decimals = 0;
singleStep = 1;
}
setDecimals(decimals);
setSingleStep(singleStep);
oldValue = value;
}
DoubleSpinBox::~DoubleSpinBox()
{
}
I have not tried your solution yet...i will. I still cant open a cell (with a spinbox delegate) for editing when running the embedded system. I have no idea why?
Re: How does the QItemDelegate work?
I have just tried this keyPressEvent in the DoubleSpinBox to see if I can increase decrease the value with the DoubleSpinBox running isolated.
It is not supposed to have the KeyPressEvent. It is just the overrided slot set Value That should be there.
Re: How does the QItemDelegate work?
Why are you making things over-complex? What's the point of shadowing QDoubleSpinBox::setValue() and keeping your own versions of value & single step which are already properties of QDoubleSpinBox? All you need is to start using the built-in class properly.
Re: How does the QItemDelegate work?
Quote:
Originally Posted by
jpn
Why are you making things over-complex? What's the point of shadowing QDoubleSpinBox::setValue() and keeping your own versions of value & single step which are already properties of QDoubleSpinBox? All you need is to start using the built-in class properly.
I want my spinbox to have minimum 0.5 and maximum 999. The value should always have three significant digits. How do I solve that if dont subclass the QDoubleSpinBox?
Re: How does the QItemDelegate work?
Quote:
Originally Posted by
SailinShoes
I want my spinbox to have minimum 0.5 and maximum 999. The value should always have three significant digits. How do I solve that if dont subclass the QDoubleSpinBox?
And if you really need some custom fancy formatting you reimplement QDoubleSpinBox::textFromValue() and QDoubleSpinBox::valueFromText(). You don't "rewrite" non-virtual setValue().
PS. There are buttons for quotes and code snippets. Could you start using them, please?
Re: How does the QItemDelegate work?
Quote:
And if you really need some custom fancy formatting you reimplement QDoubleSpinBox::textFromValue() and QDoubleSpinBox::valueFromText(). You don't "rewrite" non-virtual setValue().
jpn, I will follow your advices.