Results 1 to 19 of 19

Thread: Qstyleditemdelegate probeme with Qcombobox

  1. #1
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Exclamation Qstyleditemdelegate probeme with Qcombobox

    Hi every boby

    i have probleme when i start implementing createEditor() function in Qstyleditemclass in Qtablewidget

    her
    Qt Code:
    1. QWidget *comboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3.  
    4. if(creeConnexion())
    5. {
    6. if(index.column() == 1 || index.column() == 0)
    7. {
    8. QComboBox *editor = new QComboBox(parent);
    9. editor->setEditable(true);
    10.  
    11. // Query data
    12. QSqlQuery *query = new QSqlQuery("select designation from Produit") ;
    13. while(query->next())
    14. editor->addItem(query->value(0).toString());
    15.  
    16. return editor;
    17. }
    18.  
    19. }else
    20. // the problem her, i want to return the default editor for other cells but , the app crashed when i user this code below
    21. return QStyledItemDelegate::createEditor(parent,option,index);
    22.  
    23. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    Plz i need your help

  3. #3
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    i wait for your help

  4. #4
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    Update thread !

  5. #5
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    What is creeConnexion()?
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  6. #6
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    Quote Originally Posted by Santosh Reddy View Post
    What is creeConnexion()?
    It's a function to test the connexion on database

    her is the function

    Qt Code:
    1. static bool creeConnexion()
    2. {
    3. db = new QSqlDatabase;
    4. *db = QSqlDatabase::addDatabase("QSQLITE") ;
    5. db->setHostName("localhost");
    6. db->setDatabaseName("gestionstockDB.db");
    7. db->setPassword("");
    8. db->setUserName("");
    9. // Verifier l'ouverture de la base de donn�
    10. if(!db->open())
    11. {
    12. return false;
    13. }
    14. return true;
    15. }
    To copy to clipboard, switch view to plain text mode 

  7. #7
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 453 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    So do you realy want to create and open new database connection each time an item in the table is edited? and are you deleting it when editor is done?

    You use QAbstractItemView::setItemDelegateForColumn(int column, QAbstractItemDelegate *delegate); only for the specific column, then you will not need to check the column number in the createEditor.

    I think you are missing to mention somthing else...
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  8. #8
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    Quote Originally Posted by Santosh Reddy View Post
    So do you realy want to create and open new database connection each time an item in the table is edited? and are you deleting it when editor is done?

    You use QAbstractItemView::setItemDelegateForColumn(int column, QAbstractItemDelegate *delegate); only for the specific column, then you will not need to check the column number in the createEditor.



    I think you are missing to mention somthing else...
    no i don't delete a connexion


    Added after 7 minutes:


    her is exactly what i want:

    I have Qtablewidget
    when the user clicks in column number one, the Qcombobox show up and get it content from querying
    a database named "DBgestionstock", so the user can select an item from a list in Qcombobox

    after implementing QstyledItemDelegate:

    i get the Qcombobox show up with required content and that's what i want. so ( no problem in column number one )
    but for other columns , the cells aren't editable( Qtablewidget cells are editable by default!!)

    her is my complete code

    connect.h

    Qt Code:
    1. static bool creeConnexion()
    2. {
    3. db = new QSqlDatabase;
    4. *db = QSqlDatabase::addDatabase("QSQLITE") ;
    5. db->setHostName("localhost");
    6. db->setDatabaseName("gestionstockDB.db");
    7. db->setPassword("");
    8. db->setUserName("");
    9. // Verifier l'ouverture de la base de donné
    10. if(!db->isOpen())
    11. {
    12. if(!db->open())
    13. {
    14. return false;
    15. }
    16. return true;
    17.  
    18. }
    19. return true;
    20.  
    21. }
    To copy to clipboard, switch view to plain text mode 

    comboboxDelegate.h

    Qt Code:
    1. #include <QStyledItemDelegate>
    2. #include <QComboBox>
    3. class comboBoxDelegate : public QStyledItemDelegate
    4. {
    5. Q_OBJECT
    6. public:
    7. explicit comboBoxDelegate(QObject *parent = 0);
    8. QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const;
    9. void setEditorData(QWidget * editor, const QModelIndex & index) const;
    10. void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
    11. void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const;
    12.  
    13.  
    14. signals:
    15.  
    16. public slots:
    17.  
    18. };
    To copy to clipboard, switch view to plain text mode 


    comboboxDelegate.cpp

    Qt Code:
    1. #include "comboboxdelegate.h"
    2. #include <connect.h>
    3. comboBoxDelegate::comboBoxDelegate(QObject *parent) :
    4. QStyledItemDelegate(parent)
    5. {
    6. }
    7.  
    8. QWidget *comboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    9. {
    10.  
    11. if(creeConnexion())
    12. {
    13. if(index.column() == 1 || index.column() == 0)
    14. {
    15. QComboBox *editor = new QComboBox(parent);
    16. editor->setEditable(true);
    17.  
    18. // Query data
    19. QSqlQuery *query = new QSqlQuery("select designation from Produit") ;
    20. while(query->next())
    21. editor->addItem(query->value(0).toString());
    22.  
    23. return editor;
    24. }
    25.  
    26. }else
    27. return QStyledItemDelegate::createEditor(parent,option,index);
    28.  
    29. }
    30.  
    31. void comboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    32. {
    33. QVariant value = index.model()->data(index,Qt::EditRole) ;
    34. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    35. comboBox->setCurrentIndex(comboBox->findText(value.toString()));
    36. }
    37.  
    38. void comboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    39. {
    40. QComboBox *comboBox = static_cast<QComboBox*>(editor);
    41. model->setData(index,comboBox->currentText(),Qt::EditRole);
    42. }
    43.  
    44. void comboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    45. {
    46. editor->setGeometry(option.rect);
    47. }
    To copy to clipboard, switch view to plain text mode 

    i wish that i'm clear now
    Last edited by advseo32; 3rd August 2013 at 18:48.

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    When you call createEditor() you call creeConnexion(), which always returns true. If the column is 0 or 1 you return an editor, otherwise your code exits the function with an undefined value. The view will likely crash the program when it tries to use the " editor" you returned. Only if creeConnexion() failed would a default editor be returned.

    This could have been discovered for yourself with 30 seconds of debugger time single-stepping the delegate code after discovering the crash.


    Other observations.

    Every time your view calls createEditor() you:
    • Allocate a new QSqlDatabase object on the heap, a memory leak.
    • Create a new Sqlite database Connection as the new default connection invalidating every existing connection. This will possibly cause crashes elsewhere in your program.
    • Check that the database opens but not that it has anything in it. Sqlite can "open" an empty or non-existent file: that is how it creates a new database.
    • Use a relative path to a database file, so there is a good chance that it does not exist where you think it does.
    • When creeConnexion() returns true (it almost always will) you create a QSqlQuery on the heap and create another memory leak.


    In setEditorData() and setModelData() you assume that qobject_cast() succeeded. This will crash when called on column that is not a QComboBox editor.

  10. #10
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    i have rewrited the code again but also it crashes when i return the default editor

    her is my code so far

    comboBoxDelegate.h

    Qt Code:
    1. #include <QStyledItemDelegate>
    2. #include <QComboBox>
    3. #include <QSqlDatabase>
    4. #include <QSqlQuery>
    5. class comboBoxDelegate : public QStyledItemDelegate
    6. {
    7. Q_OBJECT
    8. public:
    9. explicit comboBoxDelegate(QObject *parent = 0);
    10. QWidget * createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const;
    11. void setEditorData(QWidget * editor, const QModelIndex & index) const;
    12. void setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const;
    13. void updateEditorGeometry(QWidget * editor, const QStyleOptionViewItem & option, const QModelIndex & index) const;
    14.  
    15.  
    16. signals:
    17.  
    18. public slots:
    19. private:
    20. QSqlDatabase *m_db;
    21. QSqlQuery *m_query;
    22.  
    23. };
    To copy to clipboard, switch view to plain text mode 


    comboBoxDelegate.cpp


    Qt Code:
    1. #include "comboboxdelegate.h"
    2. #include <QMessageBox>
    3. comboBoxDelegate::comboBoxDelegate(QObject *parent) :
    4. QStyledItemDelegate(parent)
    5. {
    6. m_db = new QSqlDatabase;
    7. *m_db = QSqlDatabase::addDatabase("QSQLITE");
    8. m_db->setHostName("localhost");
    9. m_db->setDatabaseName("gestionstockDB.db");
    10. m_db->setPassword("");
    11. m_db->setUserName("");
    12. if(m_db->open())
    13. m_query = new QSqlQuery("select designation from Produit") ;
    14. }
    15.  
    16. QWidget *comboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    17. {
    18. if(index.column() == 1)
    19. {
    20. if(m_db->open())
    21. {
    22. QComboBox *editor = new QComboBox(parent);
    23. editor->setEditable(true);
    24. while(m_query->next())
    25. editor->addItem(m_query->value(0).toString());
    26. return editor;
    27. }else{
    28. QMessageBox::critical(parent,"Erreur Connexion ","Erreur, lors de la connexion au base de donnée");
    29. return QStyledItemDelegate::createEditor(parent,option,index);
    30. }
    31. }else
    32. return QStyledItemDelegate::createEditor(parent,option,index);
    33.  
    34. }
    35.  
    36. void comboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    37. {
    38. QVariant value = index.model()->data(index,Qt::EditRole) ;
    39. QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
    40. comboBox->setCurrentIndex(comboBox->findText(value.toString()));
    41. }
    42.  
    43. void comboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    44. {
    45. QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
    46. model->setData(index,comboBox->currentText(),Qt::EditRole);
    47. }
    48.  
    49. void comboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    50. {
    51. editor->setGeometry(option.rect);
    52. }
    To copy to clipboard, switch view to plain text mode 

  11. #11
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    Have you bothered to determine where it is crashing? Tell us which line it crashes on and which column you are trying to edit at the time. There are clues in my earlier post.

  12. #12
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    When i click on column number one, so , her enter her

    Qt Code:
    1. if(index.column() == 1)
    2. {
    3. if(m_db->open())
    4. {
    5. QComboBox *editor = new QComboBox(parent);
    6. editor->setEditable(true);
    7. while(m_query->next())
    8. editor->addItem(m_query->value(0).toString());
    9. return editor;
    10. }else{
    11. QMessageBox::critical(parent,"Erreur Connexion ","Erreur, lors de la connexion au base de donnée");
    12. return QStyledItemDelegate::createEditor(parent,option,index);
    13. }
    To copy to clipboard, switch view to plain text mode 

    because m_db->open() is always "true" , he never execute this line
    Qt Code:
    1. return QStyledItemDelegate::createEditor(parent,option,index);
    To copy to clipboard, switch view to plain text mode 

    but in other cases

    if(index.column() != 1)

    he execute this line

    Qt Code:
    1. return QStyledItemDelegate::createEditor(parent,option,index);
    To copy to clipboard, switch view to plain text mode 

    so, it crashes her

  13. #13
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    At what line does it crash and what column were you trying to edit at the time?
    Run the program in your debugger. Make it crash. Look at the stack back trace and find the line in your delegate code that is mentioned closest to the top of the stack. Look at the value of the variables at that time. I bet you will find a null pointer.

    If it is crashing when you edit a column other than 1 then I bet it is not crashing in createEditor() but setEditorData(). However, I cannot tell you certainly where it is crashing... only you can tell us.

  14. #14
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    Yes you're right it crashes on setEditorData()

    I tried to edit the column Zéro
    It's crashed on line 42
    the value of "value" variable is invalid because index is not valid also ; ----> line 40

    her is the code
    Qt Code:
    1. #include "comboboxdelegate.h"
    2. #include <QMessageBox>
    3. comboBoxDelegate::comboBoxDelegate(QObject *parent) :
    4. QStyledItemDelegate(parent)
    5. {
    6. m_db = new QSqlDatabase;
    7. *m_db = QSqlDatabase::addDatabase("QSQLITE");
    8. m_db->setHostName("localhost");
    9. m_db->setDatabaseName("gestionstockDB.db");
    10. m_db->setPassword("");
    11. m_db->setUserName("");
    12. if(m_db->open())
    13. m_query = new QSqlQuery("select designation from Produit") ;
    14. }
    15.  
    16. QWidget *comboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    17. { if(!index.isValid())
    18. return QStyledItemDelegate::createEditor(parent,option,index);
    19. if(index.column() == 1)
    20. {
    21.  
    22. if(m_db->open())
    23. {
    24. QComboBox *editor = new QComboBox(parent);
    25. editor->setEditable(true);
    26. while(m_query->next())
    27. editor->addItem(m_query->value(0).toString());
    28. return editor;
    29. }else{
    30. QMessageBox::critical(parent,"Erreur Connexion ","Erreur, lors de la connexion au base de donnée");
    31. return QStyledItemDelegate::createEditor(parent,option,index);
    32. }
    33. }else
    34. return QStyledItemDelegate::createEditor(parent,option,index);
    35.  
    36. }
    37.  
    38. void comboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    39. {
    40. QVariant value = index.model()->data(index,Qt::EditRole) ;
    41. QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
    42. comboBox->setCurrentIndex(comboBox->findText(value.toString()));
    43. }
    44.  
    45. void comboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    46. {
    47. QComboBox *comboBox = qobject_cast<QComboBox*>(editor);
    48. model->setData(index,comboBox->currentText(),Qt::EditRole);
    49. }
    50.  
    51. void comboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    52. {
    53. editor->setGeometry(option.rect);
    54. }
    To copy to clipboard, switch view to plain text mode 

  15. #15
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    it works for column 1 and show Qcombobox perfectly , but when i have clicks on column 0 ( != 1)

    It craches on line 35 in setEditorData() function , i can't figured out the problem happened their

    Qt Code:
    1. #include "comboboxdelegate.h"
    2. #include <QMessageBox>
    3. comboBoxDelegate::comboBoxDelegate(QObject *parent) :
    4. QStyledItemDelegate(parent)
    5. {
    6. m_db = new QSqlDatabase;
    7. *m_db = QSqlDatabase::addDatabase("QSQLITE");
    8. m_db->setHostName("localhost");
    9. m_db->setDatabaseName("gestionstockDB.db");
    10. m_db->setPassword("");
    11. m_db->setUserName("");
    12.  
    13.  
    14. }
    15.  
    16. QWidget *comboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    17. {
    18. if(index.column() != 1)
    19. return QStyledItemDelegate::createEditor(parent, option, index);
    20. QComboBox* editor = new QComboBox(parent);
    21. editor->setEditable(true);
    22. if(m_db->open())
    23. {
    24. QSqlQuery m_query("select designation from Produit") ;
    25. while(m_query.next())
    26. editor->addItem(m_query.value(0).toString());
    27. return editor;
    28. }
    29.  
    30. }
    31.  
    32. void comboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    33. {
    34. if(index.column() != 1)
    35. QStyledItemDelegate::setEditorData(editor, index);
    36. QComboBox *comboboxDelgate = qobject_cast<QComboBox*>(editor);
    37. int value = index.model()->data(index,Qt::EditRole).toUInt();
    38. comboboxDelgate->setCurrentIndex(value);
    39. }
    40.  
    41.  
    42. void comboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    43. {
    44. if(index.column() !=1)
    45. QStyledItemDelegate::setModelData(editor, model, index);
    46. QComboBox *comboboxDelgate = qobject_cast<QComboBox*>(editor);
    47. model->setData(index,comboboxDelgate->currentText(),Qt::EditRole);
    48. }
    49.  
    50.  
    51. void comboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    52. {
    53. editor->setGeometry(option.rect);
    54. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by advseo32; 4th August 2013 at 18:41.

  16. #16
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    You are telling me it crashes:
    Qt Code:
    1. void comboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    2. {
    3. if(index.column() != 1)
    4. QStyledItemDelegate::setEditorData(editor, index); // <<<<< here
    5. QComboBox *comboboxDelgate = qobject_cast<QComboBox*>(editor);
    6. int value = index.model()->data(index,Qt::EditRole).toUInt();
    7. comboboxDelgate->setCurrentIndex(value);
    8. }
    To copy to clipboard, switch view to plain text mode 
    I find that hard to believe. I could believe:
    Qt Code:
    1. void comboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    2. {
    3. if(index.column() != 1)
    4. QStyledItemDelegate::setEditorData(editor, index);
    5. QComboBox *comboboxDelgate = qobject_cast<QComboBox*>(editor);
    6. int value = index.model()->data(index,Qt::EditRole).toUInt();
    7. comboboxDelgate->setCurrentIndex(value); // <<<< crashing here
    8. }
    To copy to clipboard, switch view to plain text mode 
    especially if this:
    Qt Code:
    1. QWidget *comboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. if(index.column() != 1)
    4. return QStyledItemDelegate::createEditor(parent, option, index);
    5. QComboBox* editor = new QComboBox(parent);
    6. editor->setEditable(true);
    7. if(m_db->open()) // <<<< is false
    8. {
    9. QSqlQuery m_query("select designation from Produit") ;
    10. while(m_query.next())
    11. editor->addItem(m_query.value(0).toString());
    12. return editor;
    13. }
    14. // <<< what is returned now?
    15. }
    To copy to clipboard, switch view to plain text mode 


    You need to be more methodical. Put a break point in each function of your delegate and single step through each. Inspect each step and variable at each step and understand why things are happening the way they are.

  17. #17
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    Now it's working , thank's ChrisW67

    You're right in every point, it crashes at this line
    Qt Code:
    1. comboboxDelgate->setCurrentIndex(value);
    To copy to clipboard, switch view to plain text mode 
    another problem is don't check if(m_db->open == false )
    I wish you to inform me if my code is bad, is good, is very good, i need your advices


    her is the code

    Qt Code:
    1. #include "comboboxdelegate.h"
    2. #include <QMessageBox>
    3. comboBoxDelegate::comboBoxDelegate(QObject *parent) :
    4. QStyledItemDelegate(parent)
    5. {
    6. m_db = new QSqlDatabase;
    7. *m_db = QSqlDatabase::addDatabase("QSQLITE");
    8. m_db->setHostName("localhost");
    9. m_db->setDatabaseName("gestionstockDB.db");
    10. m_db->setPassword("");
    11. m_db->setUserName("");
    12.  
    13.  
    14. }
    15.  
    16. QWidget *comboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    17. {
    18. if(index.column() != 1)
    19. return QStyledItemDelegate::createEditor(parent, option, index);
    20. QComboBox* editor = new QComboBox(parent);
    21. editor->setEditable(true);
    22. if(m_db->open())
    23. {
    24. QSqlQuery m_query("select designation from Produit") ;
    25. while(m_query.next())
    26. editor->addItem(m_query.value(0).toString());
    27. return editor;
    28. }else
    29. return QStyledItemDelegate::createEditor(parent, option, index);
    30.  
    31. }
    32.  
    33. void comboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    34. {
    35. if(index.column() != 1)
    36. QStyledItemDelegate::setEditorData(editor, index);
    37. else{
    38. QComboBox *comboboxDelgate = qobject_cast<QComboBox*>(editor);
    39. int value = index.model()->data(index,Qt::EditRole).toUInt();
    40. comboboxDelgate->setCurrentIndex(value);
    41. }
    42. }
    43.  
    44.  
    45. void comboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
    46. {
    47. if(index.column() !=1)
    48. QStyledItemDelegate::setModelData(editor, model, index);
    49. else{
    50. QComboBox *comboboxDelgate = qobject_cast<QComboBox*>(editor);
    51. model->setData(index,comboboxDelgate->currentText(),Qt::EditRole);
    52. }
    53. }
    54.  
    55.  
    56. void comboBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
    57. {
    58. editor->setGeometry(option.rect);
    59. }
    To copy to clipboard, switch view to plain text mode 

  18. #18
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    Look at createEditor(): What type of editor is returned for column1 if the database open() fails?
    Once it has the editor, the view calls setEditorData() to load the editor.
    What pointer value will qobject_cast<QComboBox*>() return for the type of editor you created when the database open() failed?
    What happens when you use that value?

    In your last listing:
    Delete line 29.
    Remove "else" from line 28.
    Move line 27 after line 28.
    How does that change the behaviour?

  19. #19
    Join Date
    Jul 2013
    Posts
    54
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Qstyleditemdelegate probeme with Qcombobox

    Quote Originally Posted by ChrisW67 View Post
    Look at createEditor(): What type of editor is returned for column1 if the database open() fails?
    Once it has the editor, the view calls setEditorData() to load the editor.
    What pointer value will qobject_cast<QComboBox*>() return for the type of editor you created when the database open() failed?
    What happens when you use that value?

    In your last listing:
    Delete line 29.
    Remove "else" from line 28.
    Move line 27 after line 28.
    How does that change the behaviour?
    No behaviour changed, because m_db->open() == always true and never fails

    but now i have figured out what you mean,

    with old code when m_db->open() fails createEditor() return nothing
    so, qobject_cast<QComboBox*>() also return nothing, so the programme crashes

    her is your revised code


    Qt Code:
    1. QWidget *comboBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
    2. {
    3. if(index.column() != 1 )
    4. return QStyledItemDelegate::createEditor(parent, option, index);
    5. QComboBox* editor = new QComboBox(parent);
    6. editor->setEditable(true);
    7. if(m_db->open())
    8. {
    9. QSqlQuery m_query("select designation from Produit") ;
    10. while(m_query.next())
    11. editor->addItem(m_query.value(0).toString());
    12. }
    13. return editor;
    14.  
    15. }
    16.  
    17. void comboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    18. {
    19. if(index.column() != 1)
    20. QStyledItemDelegate::setEditorData(editor, index);
    21. else{
    22. QComboBox *comboboxDelgate = qobject_cast<QComboBox*>(editor);
    23. int value = index.model()->data(index,Qt::EditRole).toUInt();
    24. comboboxDelgate->setCurrentIndex(value);
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Do not paint selection in QStyledItemDelegate
    By jgirlich in forum Qt Programming
    Replies: 3
    Last Post: 4th March 2013, 10:33
  2. little problem with QStyledItemDelegate
    By Qiieha in forum Qt Programming
    Replies: 1
    Last Post: 26th November 2012, 09:21
  3. Replies: 1
    Last Post: 19th June 2012, 20:20
  4. QTreeView & QStyledItemDelegate
    By mentalmushroom in forum Qt Programming
    Replies: 0
    Last Post: 29th September 2011, 07:57
  5. Custom QStyledItemDelegate
    By Berryblue031 in forum Qt Programming
    Replies: 0
    Last Post: 2nd March 2011, 10:32

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
  •  
Qt is a trademark of The Qt Company.