Results 1 to 4 of 4

Thread: ActiveX and QComboBox delegate

  1. #1
    Join Date
    Aug 2012
    Posts
    6
    Qt products
    Qt4

    Smile ActiveX and QComboBox delegate

    Hi !

    I'm developping an ActiveX component using a QTableWidget with QComboBox delegates.
    The QTableWidget works perfectly when I use it in a QMainWindow directly but its behaviour changes once embedded in an ActiveX.
    When I select a choice in one of the QComboBox, the focus is not set on the widget and the event itemChanged is not called on QTableWidget when I select an other cell of the table. When I force the focus on the QComboBox by right clicking on it, the event is called when I select an other cell.

    I found a solution by connecting the signal currentIndexChanged(int) to a slot which emit the signals commitData() and closeEditor(). It solves the problem when using the mouse to select the choice but not when using the keyboard. The focus is lost whenever I press the down arrow.

    I'd like to know if it is a known bug of Qt or if I missed something and if there is a good solution to solve it.
    I made a simple project on VS2008 which reproduce this problem.
    https://rapidshare.com/files/5517533...st_ActiveX.zip
    The top QTableWidget is in an ActiveX and has the problem. The bottom QTableWidget is in a frame and works as I want.

    Don't hesitate to test it and ask me more details.
    Thanks.

    Here is my source code :

    main.cpp
    Qt Code:
    1. #include "uitest_activex.h"
    2. #include <QtGui/QApplication>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. UITest_ActiveX w;
    8. w.show();
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 


    uitest_activex.h
    Qt Code:
    1. #ifndef UITEST_ACTIVEX_H
    2. #define UITEST_ACTIVEX_H
    3.  
    4. #include <QtGui/QMainWindow>
    5. #include "ui_uitest_activex.h"
    6.  
    7. class UITest_ActiveX : public QMainWindow
    8. {
    9. public:
    10. UITest_ActiveX(QWidget *parent = 0, Qt::WFlags flags = 0);
    11. ~UITest_ActiveX();
    12.  
    13. private:
    14. Ui::UITest_ActiveXClass ui;
    15. };
    16.  
    17. #endif // UITEST_ACTIVEX_H
    To copy to clipboard, switch view to plain text mode 

    uitest_activex.cpp
    Qt Code:
    1. #include "uitest_activex.h"
    2. #include <QtGui/QVBoxLayout>
    3. #include <ActiveQt/QAxWidget>
    4.  
    5. #include <QTGui/QTableWidget>
    6. #include "..\UITest_AxComponent\comboboxdelegate.h"
    7.  
    8. UITest_ActiveX::UITest_ActiveX(QWidget *parent, Qt::WFlags flags)
    9. : QMainWindow(parent, flags)
    10. {
    11. ui.setupUi(this);
    12.  
    13. QFrame* inv_frame = new QFrame();
    14. inv_frame->resize(1200, 1200);
    15. inv_frame->setMinimumSize(QSize(800, 600));
    16.  
    17. QVBoxLayout* lnv_vertical_layout = new QVBoxLayout(inv_frame);
    18.  
    19. //TEST1 : in an ActiveX Component
    20. QAxWidget* inv_ax_helpdesk = new QAxWidget(inv_frame) ;
    21. inv_ax_helpdesk->setControl( "{5E3FC5ED-FBA6-47AE-B1A3-31567AF9C2F3}" ) ;
    22. inv_ax_helpdesk->setObjectName( QString::fromUtf8( "inv_ax_helpdesk" ) ) ;
    23. inv_ax_helpdesk->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    24. lnv_vertical_layout->addWidget(inv_ax_helpdesk);
    25. //END TEST 1
    26.  
    27. //TEST2 : not in an ActiveX Component
    28. QTableWidget* lnv_table_widget = new QTableWidget();
    29. lnv_table_widget->setRowCount(10);
    30. lnv_table_widget->setColumnCount(2);
    31. lnv_vertical_layout->addWidget( lnv_table_widget );
    32.  
    33. for(int li_column = 0; li_column < 2; ++li_column)
    34. {
    35. for(int li_row = 0; li_row < 10; ++li_row)
    36. {
    37. lnv_table_widget->setItem(li_row, li_column, new QTableWidgetItem(0));
    38. lnv_table_widget->item(li_row, li_column)->setData(Qt::DisplayRole, 1);
    39. }
    40. }
    41.  
    42. lnv_table_widget->setItemDelegateForColumn(0, new ComboBoxDelegate());
    43.  
    44. for(int li_index = 0; li_index < 30; ++li_index)
    45. {
    46. lnv_table_widget->openPersistentEditor(lnv_table_widget->item(li_index, 0));
    47. }
    48. //END TEST 2
    49.  
    50. this->setCentralWidget(inv_frame);
    51. }
    52.  
    53. UITest_ActiveX::~UITest_ActiveX()
    54. {
    55. }
    To copy to clipboard, switch view to plain text mode 

    comboboxdelegate.h
    Qt Code:
    1. #ifndef COMBOBOXDELEGATE_H
    2. #define COMBOBOXDELEGATE_H
    3.  
    4. #include <QtGui/QStyledItemDelegate.h>
    5. #include <QtGui/QComboBox>
    6. #include "defines.h"
    7.  
    8. class UITEST_AXCOMPONENT_EXPORT ComboBoxDelegate : public QStyledItemDelegate
    9. {
    10. Q_OBJECT
    11.  
    12. private:
    13. QComboBox* inv_combobox;
    14.  
    15. public:
    16. ComboBoxDelegate(QObject* anv_parent = 0);
    17. ~ComboBoxDelegate();
    18.  
    19. QWidget* createEditor(QWidget* auo_parent, const QStyleOptionViewItem& anv_option, const QModelIndex& anv_modelIndex) const;
    20. void setEditorData(QWidget* auo_editor, const QModelIndex& anv_modelIndex) const;
    21. void setModelData(QWidget* auo_editor, QAbstractItemModel* anv_model, const QModelIndex& anv_modelIndex) const;
    22.  
    23. private slots:
    24. void commitAndCloseEditor();
    25. };
    26.  
    27. #endif
    To copy to clipboard, switch view to plain text mode 

    ocomboboxdelegate.cpp
    Qt Code:
    1. #include <QtGui/QtGui>
    2.  
    3. #include "comboboxdelegate.h"
    4.  
    5. ComboBoxDelegate::ComboBoxDelegate(QObject* anv_parent) :
    6. QStyledItemDelegate(anv_parent)
    7. {
    8. inv_combobox = 0;
    9. }
    10.  
    11. ComboBoxDelegate::~ComboBoxDelegate()
    12. {
    13. if(inv_combobox != 0)
    14. {
    15. delete inv_combobox;
    16. }
    17. }
    18.  
    19. QWidget* ComboBoxDelegate::createEditor(QWidget* anv_parent, const QStyleOptionViewItem& anv_option , const QModelIndex& anv_model_index) const
    20. {
    21. QComboBox* lw_new_editor = new QComboBox(anv_parent);
    22. lw_new_editor->autoFillBackground();
    23.  
    24. lw_new_editor->addItem("Choice0", 0);
    25. lw_new_editor->addItem("Choice1", 1);
    26. lw_new_editor->addItem("Choice2", 2);
    27.  
    28. //QObject::connect(lw_new_editor, SIGNAL(currentIndexChanged(int)), this, SLOT(commitAndCloseEditor()) );
    29.  
    30. return lw_new_editor;
    31. }
    32.  
    33. void ComboBoxDelegate::setEditorData(QWidget* anv_editor, const QModelIndex& anv_model_index) const
    34. {
    35. QComboBox* lnv_combobox = static_cast<QComboBox*>(anv_editor);
    36. QVariant lv_data = anv_model_index.model()->data(anv_model_index, Qt::DisplayRole);
    37.  
    38. int li_index = lnv_combobox->findData(lv_data, Qt::UserRole);
    39. lnv_combobox->setCurrentIndex(li_index);
    40. }
    41.  
    42. void ComboBoxDelegate::setModelData(QWidget* anv_editor, QAbstractItemModel* anv_model, const QModelIndex& anv_model_index) const
    43. {
    44. QComboBox* lnv_combobox = static_cast<QComboBox*>(anv_editor);
    45. int li_index = lnv_combobox->currentIndex();
    46.  
    47. //récupération des data dans la ComboBox
    48. QVariant lnv_data = lnv_combobox->itemData(li_index, Qt::UserRole);
    49.  
    50. //recopie des data dans le modèle de données
    51. anv_model->setData(anv_model_index, lnv_data, Qt::DisplayRole);
    52.  
    53. QModelIndex lnv_model_index = anv_model->index(anv_model_index.row(), anv_model_index.column() + 1);
    54. anv_model->setData(lnv_model_index, lnv_data, Qt::DisplayRole);
    55. }
    56.  
    57. void ComboBoxDelegate::commitAndCloseEditor()
    58. {
    59. qDebug() << "ComboBoxDelegate::commitAndCloseEditor";
    60. QComboBox* editor = qobject_cast<QComboBox*>(sender());
    61.  
    62. emit commitData(editor);
    63. emit closeEditor(editor);
    64. }
    To copy to clipboard, switch view to plain text mode 

    uitest_axcomponent.h
    Qt Code:
    1. #ifndef UITEST_AXCOMPONENT_H
    2. #define UITEST_AXCOMPONENT_H
    3.  
    4. #include <QtGui/QWidget>
    5. #include <ActiveQt/QAxBindable>
    6.  
    7. #include "ui_uitest_axcomponent.h"
    8.  
    9. class UITest_AxComponent : public QWidget, public QAxBindable
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. UITest_AxComponent(QWidget *parent = 0);
    15.  
    16. private:
    17. Ui::UITest_AxComponentClass ui;
    18. };
    19.  
    20. #endif // UITEST_AXCOMPONENT_H
    To copy to clipboard, switch view to plain text mode 

    uitest_axcomponent.cpp
    Qt Code:
    1. #include "uitest_axcomponent.h"
    2.  
    3. #include <ActiveQt/QAxFactory>
    4. #include <QtGui/QTableWidget>
    5. #include <QtGui/QVBoxLayout>
    6.  
    7. #include "comboboxdelegate.h"
    8.  
    9. UITest_AxComponent::UITest_AxComponent(QWidget *parent)
    10. : QWidget(parent)
    11. {
    12. ui.setupUi(this);
    13.  
    14. QVBoxLayout* lnv_vertical_layout = new QVBoxLayout(this);
    15.  
    16. QTableWidget* lnv_table_widget = new QTableWidget();
    17. lnv_table_widget->setRowCount(10);
    18. lnv_table_widget->setColumnCount(2);
    19. lnv_vertical_layout->addWidget( lnv_table_widget );
    20.  
    21. for(int li_column = 0; li_column < 2; ++li_column)
    22. {
    23. for(int li_row = 0; li_row < 10; ++li_row)
    24. {
    25. lnv_table_widget->setItem(li_row, li_column, new QTableWidgetItem(0));
    26. lnv_table_widget->item(li_row, li_column)->setData(Qt::DisplayRole, 1);
    27. }
    28. }
    29.  
    30. lnv_table_widget->setItemDelegateForColumn(0, new ComboBoxDelegate());
    31.  
    32. for(int li_index = 0; li_index < 30; ++li_index)
    33. {
    34. lnv_table_widget->openPersistentEditor(lnv_table_widget->item(li_index, 0));
    35. }
    36. }
    37.  
    38. QAXFACTORY_DEFAULT(UITest_AxComponent,
    39. "{5E3FC5ED-FBA6-47AE-B1A3-31567AF9C2F3}",
    40. "{129C2AEF-95FF-41CD-933A-BB6797C15EA7}",
    41. "{D649E3DF-8A75-4760-BE24-C95099DA12E3}",
    42. "{95A80282-D524-472A-893B-34362AB50097}",
    43. "{5925E413-B343-47BA-9C8A-9D63E0F06F2C}")
    To copy to clipboard, switch view to plain text mode 
    Last edited by klaarx; 14th August 2012 at 09:34.

  2. #2
    Join Date
    Aug 2012
    Posts
    6
    Qt products
    Qt4

    Default Re: ActiveX and QComboBox delegate

    nobody has an idea ?

  3. #3
    Join Date
    Aug 2012
    Posts
    6
    Qt products
    Qt4

    Default Re: ActiveX and QComboBox delegate

    should I report it as a bug ?

  4. #4
    Join Date
    Aug 2012
    Posts
    6
    Qt products
    Qt4

    Default Re: ActiveX and QComboBox delegate

    Please, I still need help !

Similar Threads

  1. Replies: 2
    Last Post: 29th December 2012, 15:14
  2. setEditorData() for QComboBox Delegate
    By fatecasino in forum Newbie
    Replies: 10
    Last Post: 6th April 2011, 03:00
  3. Replies: 0
    Last Post: 10th March 2011, 12:44
  4. QCombobox as QTableView delegate
    By martonmiklos in forum Qt Programming
    Replies: 7
    Last Post: 13th June 2010, 16:23
  5. Again QTableWidget and QComboBox delegate
    By Aki-Matti in forum Qt Programming
    Replies: 2
    Last Post: 4th March 2008, 14:40

Tags for this Thread

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.