Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: How does the QItemDelegate work?

  1. #1
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default 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:

    Qt Code:
    1. void MyTableWidget::keyPressEvent(QKeyEvent *e)
    2. {
    3.  
    4.  
    5. if(e->key() == Qt::Key_Return)
    6. {
    7.  
    8. n = new QKeyEvent(e->type(), Qt::Key_F2, e->modifiers(), e->text(), e->isAutoRepeat(), e->count());
    9.  
    10. }
    11. else
    12. {
    13.  
    14. n = e;
    15.  
    16. }
    17.  
    18. QTableWidget::keyPressEvent(n);
    19.  
    20. }
    To copy to clipboard, switch view to plain text mode 
    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...
    Last edited by jpn; 2nd March 2008 at 10:28. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  3. #3
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How does the QItemDelegate work?

    Hi wysota,

    I dont know if i follow you...do you mean something like this?

    Qt Code:
    1. void MyTableWidget::keyPressEvent(QKeyEvent *e)
    2. {
    3.  
    4.  
    5. if(e->key() == Qt::Key_Return)
    6. {
    7.  
    8. QAbstractItemView::edit(currentIndex())
    9.  
    10. }
    11.  
    12. QTableWidget::keyPressEvent(n);
    13.  
    14. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 2nd March 2008 at 11:48. Reason: missing [code] tags

  4. #4
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How does the QItemDelegate work?

    Sorry the previous dont work...I mean like this:

    Qt Code:
    1. void MyTableWidget::keyPressEvent(QKeyEvent *e)
    2. {
    3.  
    4. QAbstractItemView::edit(currentIndex(), QAbstractItemView::EditKeyPressed, e);
    5. QTableWidget::keyPressEvent(e);
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 2nd March 2008 at 11:47. Reason: missing [code] tags

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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.
    J-P Nurmi

  6. #6
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default 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?

  7. #7
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How does the QItemDelegate work?

    So, there's a QLineEdit editor or what? Could you assure that SpinBoxDelegate::createEditor() gets called?
    J-P Nurmi

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  9. #9
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default 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?

  10. #10
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default 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.

  11. #11
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default 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?

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default 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.

  13. #13
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default 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:

    Qt Code:
    1. void DoubleSpinBox::keyPressEvent(QKeyEvent *e)
    2. {
    3.  
    4. // UP
    5. if(e->key() == Qt::Key_Launch6)
    6. {
    7.  
    8. double value = this->text().toDouble();
    9. value = value + singleStep;
    10. emit valueChanged(value);
    11.  
    12. }
    13. // DOWN
    14. else if(e->key() == Qt::Key_Launch7)
    15. {
    16.  
    17. double value = this->text().toDouble();
    18. value = value - singleStep;
    19. emit valueChanged(value);
    20.  
    21. }
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 
    ...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?

    Qt Code:
    1. void MyTableWidget::keyPressEvent(QKeyEvent *e)
    2. {
    3.  
    4. switch(e->key())
    5. {
    6.  
    7. case Qt::Key_Launch5:
    8. {
    9.  
    10. QAbstractItemView::edit(currentIndex(), QAbstractItemView::EditKeyPressed, e);
    11.  
    12. }
    13. break;
    14. default:
    15. break;
    16.  
    17. }
    18.  
    19. QTableWidget::keyPressEvent(e);
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 
    ...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?
    Last edited by jpn; 3rd March 2008 at 16:29. Reason: missing [code] tags

  14. #14
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How does the QItemDelegate work?

    Quote Originally Posted by SailinShoes View Post
    ...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:
    Qt Code:
    1. QDoubleSpinBox* spinBox = new QDoubleSpinBox(parent);
    2. QShortcut* upShortcut = new QShortcut(Qt::Key_Launch6, spinBox, SLOT(stepUp()));
    3. ...
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by SailinShoes View Post
    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?
    J-P Nurmi

  15. #15
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How does the QItemDelegate work?

    Hi jpn,

    This is my subclassed QDoubleSpinBox.h code :

    Qt Code:
    1. #ifndef DOUBLESPINBOX_H
    2. #define DOUBLESPINBOX_H
    3.  
    4.  
    5.  
    6. #include <QDoubleSpinBox>
    7. #include <QKeyEvent>
    8. #include <QDebug>
    9.  
    10.  
    11.  
    12. class DoubleSpinBox : public QDoubleSpinBox
    13. {
    14.  
    15. Q_OBJECT
    16.  
    17. public:
    18. DoubleSpinBox(QWidget *parent);
    19. virtual void keyPressEvent(QKeyEvent *e);
    20. ~DoubleSpinBox();
    21.  
    22. public slots:
    23. virtual void setValue(double value);
    24.  
    25. private:
    26. double presentvalue;
    27. double singleStep;
    28.  
    29. };
    30.  
    31. #endif
    To copy to clipboard, switch view to plain text mode 

    ...and this is my subclassed QDoubleSpinbox.cpp code :

    Qt Code:
    1. #include "DoubleSpinBox.h"
    2.  
    3. DoubleSpinBox::DoubleSpinBox(QWidget *parent)
    4. : QDoubleSpinBox(parent)
    5. {
    6.  
    7.  
    8.  
    9. }
    10.  
    11. void DoubleSpinBox::keyPressEvent(QKeyEvent *e)
    12. {
    13.  
    14. if(e->key() == Qt::Key_A)
    15. {
    16.  
    17. double value = this->text().toDouble();
    18. value = value + singleStep;
    19. setValue(value);
    20.  
    21. }
    22. else if(e->key() == Qt::Key_B)
    23. {
    24.  
    25. double value = this->text().toDouble();
    26. value = value - singleStep;
    27. setValue(value);
    28.  
    29. }
    30.  
    31. }
    32.  
    33. void DoubleSpinBox::setValue(double value)
    34. {
    35.  
    36. int decimals;
    37. static double oldValue;
    38.  
    39. decimals = 0;
    40. singleStep = 0;
    41.  
    42. qDebug() << "value = "<< value;
    43.  
    44. if( value >= 0.500 && value <= 0.999 )
    45. {
    46.  
    47. decimals = 3;
    48. singleStep = 0.001;
    49.  
    50. if(oldValue == 1.00)
    51. {
    52.  
    53. value = 0.999;
    54.  
    55. }
    56.  
    57. }
    58. else if(value >= 1.00 && value <= 9.99)
    59. {
    60.  
    61. decimals = 2;
    62. singleStep = 0.01;
    63.  
    64. if(oldValue == 10.0)
    65. {
    66.  
    67. value = 9.99;
    68.  
    69. }
    70.  
    71. }
    72. else if(value >= 10.0 && value <= 99.9)
    73. {
    74.  
    75. decimals = 1;
    76. singleStep = 0.1;
    77.  
    78. if(oldValue == 100)
    79. {
    80.  
    81. value = 99.9;
    82.  
    83. }
    84.  
    85. }
    86. else if(value >= 100 && value <= 999)
    87. {
    88.  
    89. decimals = 0;
    90. singleStep = 1;
    91.  
    92. }
    93.  
    94. setDecimals(decimals);
    95. setSingleStep(singleStep);
    96.  
    97. oldValue = value;
    98.  
    99. QDoubleSpinBox::setValue(value);
    100.  
    101. }
    102.  
    103. DoubleSpinBox::~DoubleSpinBox()
    104. {
    105.  
    106.  
    107.  
    108. }
    To copy to clipboard, switch view to plain text mode 
    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?
    Last edited by jpn; 3rd March 2008 at 18:54. Reason: missing [code] tags

  16. #16
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default 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.

  17. #17
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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.
    J-P Nurmi

  18. #18
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How does the QItemDelegate work?

    Quote Originally Posted by jpn View Post
    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?
    Last edited by jpn; 3rd March 2008 at 19:19. Reason: missing [quote] tags

  19. #19
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: How does the QItemDelegate work?

    Quote Originally Posted by SailinShoes View Post
    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?
    J-P Nurmi

  20. #20
    Join Date
    Mar 2008
    Posts
    57
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: How does the QItemDelegate work?

    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.

Similar Threads

  1. QItemDelegate Editor Crash
    By mclark in forum Qt Programming
    Replies: 13
    Last Post: 22nd March 2018, 05:06
  2. premature call to setmodeldata with QItemDelegate
    By placebo in forum Qt Programming
    Replies: 1
    Last Post: 25th November 2007, 18:39
  3. QActions don't work with menubar hidden
    By Pepe in forum Qt Programming
    Replies: 1
    Last Post: 16th August 2007, 02:04
  4. Change work area OS
    By pakulo in forum Qt Programming
    Replies: 15
    Last Post: 15th May 2007, 08:20
  5. QTextEdit Justify align making work
    By dec0ding in forum Qt Programming
    Replies: 2
    Last Post: 13th January 2006, 13:02

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.