Important: Please read the Qt Code of Conduct -

Editing data in QStyledItemDelegate using QTextEditor

  • Hi,
    I use the following code to write the modified text back to the model and save the changes to the db:

    QTextEdit *tedit = qobject_cast<QTextEdit*>(editor);
                QModelIndex updateIndex(index.model ()->index(index.row (),0,index.parent ()));
                QString fixID;
                QVariant v( (Qt::DisplayRole));
                fixID = v.toString ();
                qDebug() << "FixID: " << fixID;
                QModelIndex updateIndex2(index.model ()->index(index.row (),6,index.parent ()));
                QString descItem;
                QVariant desc( (Qt::DisplayRole));
                descItem = desc.toString ();
                qDebug() << "descItem: " << descItem;
                model->setData (index,tedit->setText(descItem));
                QVariant de( (Qt::DisplayRole));
                QString fixedDescr;
                fixedDescr = de.toString ();
                fixquery.prepare ("UPDATE Items SET Color = :Color WHERE ID = :fixID ");
                fixquery.bindValue (":Color",fixedDescr);
                fixquery.bindValue (":fixID",fixID);
                fixquery.exec ();

    The model->setData (index,tedit->setText(descItem)); line generates an error message:
    C:\Programming\Projects\Folkfriends_1_0\mydelegate.cpp:194: error: invalid use of void expression
    model->setData (index,tedit->setText(descItem));
    How can I fix that line to make it work? Thank you for your help.

  • Lifetime Qt Champion

    The model->setData (index,tedit->setText(descItem));
    You give it :
    But that is a function call and it returns void.

    So something like this
    QString str = tedit->toPlainText();
    model->setData (index,str);

  • For anyone who got here googling: the above is not a nice example of how to use a delegate

Log in to reply