Important: Please read the Qt Code of Conduct -

QDataWidgetMapper, QTextEdit storing plain text

  • OK, so my next question... I have a QTextEdit mapped to a database field via QDataWidgetMapper. Works wonderfully but I want it to store the plain text instead of HTML. Is there a magic trick to make that happen?

    Thanks in advance.

  • Lifetime Qt Champion

    @cmulcahy said:
    What about using a QPlainTextEdit instead ?

  • @mrjj I like the simple solutions best! Unfortunately not an option because I'm showing syntax highlighting of the contents. I want the syntax highlighter to do its job for display but have the DB store the plaintext.

    This is a snippet manager. I want to use the snippets as plaintext in another context but allow the user to view and edit it with syntax highlighting.

  • Lifetime Qt Champion

    Me too but might been too easy :)
    so its soemthing like

    Im wondering how you get the HTML back on next view?
    You insert ( highlight) at runtime so its fine to throw the HTML away?

    Have you tried with
    void QDataWidgetMapper::addMapping(QWidget *widget, int section, const QByteArray &propertyName)
    and try to see if it will accept
    plainText : QString
    property ?
    else you could subclass it and make such property.

    There is also
    void QDataWidgetMapper::setItemDelegate(QAbstractItemDelegate *delegate)

    "Sets the item delegate to delegate. The delegate will be used to write data from the model into the widget and from the widget to the model"

    Sounds like could be used.

  • void QDataWidgetMapper::addMapping(QWidget *widget, int section, const QByteArray &propertyName)

    That was it. No matter how many times I stared at this definition on the documentation, it never occurred to me that propertyName should be wrapped in quotes.

    mapper->addMapping(ui->scriptEditor, scriptmodel->fieldIndex("script"), "plainText");

  • Lifetime Qt Champion

    Nah, a QString would have said it more.
    or as in old times const char * -ish :)

Log in to reply