Important: Please read the Qt Code of Conduct -

Determining whether QLineEdit or QTextedit is used when string is edited in TableView

  • Hi,
    I have a QItemEditorfactory set p the following way:

    QWidget *myDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
        QItemEditorFactory *factory = new QItemEditorFactory;
        QItemEditorCreatorBase *lineEditor = new QStandardItemEditorCreator<QLineEdit>();
        factory->registerEditor (QVariant::String, lineEditor);

    Is there a way to determine whether the string in the tableview to be handled by QLineEdit or QTextEdit?

    Thank you.

  • Qt Champions 2019

    @gabor53 How do you want to "determine" that? What are the criteria to decide? Can you explain better what you want to achieve?

  • @jsulm
    I have fields where only short text is displayed like name or color. In these cases LineEdit is enough. I have bigger fields with several lines of text (like description) where TextEdit works better. My problem is that technically they are both string, just one of them usually longer than the other.

  • Qt Champions 2019

    @gabor53 Then you need to check the length of the string and if it exceeds a specific length then you use QTextEdit else QLineEdit.
    But actually it would be better to not to use the string length as it can change. Do you have any other information?

  • @jsulm
    I can use the column title. I've just realized, that there are text fields where QComboBox would be the best as I can populate those from the database. I think the best way to determine the editor is using the column title, but I was told that was not the best idea.

Log in to reply