Results 1 to 3 of 3

Thread: QItemDelegate painting widget in cell 0,0 regardless of selected cell

  1. #1
    Join Date
    Dec 2010
    Location
    UK
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Post QItemDelegate painting widget in cell 0,0 regardless of selected cell

    My first post so apologies if I have fractured any rules!!

    I have created a custom delegate from QItemDelegate, i have reimplemented the virtual functions to createEditor() to create the widget, setEditorData() and set the setModalData().

    It all works well for cell 0,0 but when trying to edit other cells, 0,1 for example, the editing widget is painted into cell 0,0 Once the edit (completed in cell 0,0) is complete the correct value is entered into cell 0,1.

    If someone can explain why the widget is always painted to cell 0,0 or provide a link to something that explains my problem I would be very grateful.

    QWidget *tDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &/*index*/) const
    {

    QComboBox *editor = new QComboBox(parent);
    QStringList tempUnits;
    tempUnits << "Celcius" << "Fahrenhiet" << "Rankin" << "Delisle" << "Newton" << "Reaumur" << "Romer" << "Kelvin";

    for (int i=0; i<tempUnits.size();i++)
    {
    editor->addItem(tempUnits.at(i));

    }

    editor->setCurrentIndex(0);
    return editor;


    }

    Qt Code:
    1. void tDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
    2. {
    3.  
    4.  
    5. QString value = index.model()->data(index,Qt::EditRole).toString();
    6. QComboBox *combo = qobject_cast<QComboBox*>(editor);
    7. Q_ASSERT(combo);
    8. combo->setCurrentIndex(combo->findText(value));
    9.  
    10.  
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: QItemDelegate painting widget in cell 0,0 regardless of selected cell

    Have you implemented an override for QStyledItemDelegate::updateEditorGeometry() ? It need not be complicated, but the default does nothing and might be leading to the symptoms you describe.

    Also, you can construct the combo box from the QStringList directly and dispense with the explicit loop.

    BTW: You have spelt "Celsius" and "Fahrenheit" incorrectly.

  3. #3
    Join Date
    Dec 2010
    Location
    UK
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Thumbs up Re: QItemDelegate painting widget in cell 0,0 regardless of selected cell

    Chris,

    Your programming skills are far better than my spelling!!!

    I had indeed created the updateEditorGeometry() method but not populated it, deleting said method has fixed the problem.

    Thanks very much for your assistance, much appreciated.

Similar Threads

  1. Border around selected cell in qtablewidget...
    By pyqt123 in forum Qt Programming
    Replies: 4
    Last Post: 16th July 2012, 13:55
  2. Replies: 1
    Last Post: 21st April 2011, 09:14
  3. Many cell widget in QTableWidget
    By Benarez in forum Qt Programming
    Replies: 3
    Last Post: 21st January 2010, 07:02
  4. Replies: 1
    Last Post: 7th December 2009, 18:56
  5. Problem with painting QTable cell
    By codebehind in forum Newbie
    Replies: 9
    Last Post: 28th April 2007, 19:26

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.