Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How add overwriteMode in QTableWidget.



  • I need overwriteMode in QTableWidget. This function contains in QPlainTextEdit. How to add this in QTableWidget?


  • Moderators

    @Anton-Shelenkov
    Editor widgets in itemviews are mostly QLineEdit widgets, which do not have such a property.

    You can try to create a QPlainTextEdit widget as editor by subclassing QStyledItemDelegate:

    QWidget* MyDelegate::createEditor(QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index) const
    {
          QPlainTextEdit* editor = new QPlainTextEdit( parent );
               editor->setGeometry( option.rect );
               editor->setPlainText( index.data().toString() );
               editor->setOverwriteMode( true );
          return editor;
    }
    
    void MyDelegate::setEditorData(QWidget * editor, const QModelIndex & index) const
    {
         if( QPlainTextEdit* edit = qobject_cast<QPlainTextEdit*>(editor) )
         {
                edit ->setPlainText( index.data().toString() );
         }
    }
    
    void MyDelegate::setModelData(QWidget * editor, QAbstractItemModel * model, const QModelIndex & index) const
    {
         if( QPlainTextEdit* edit = qobject_cast<QPlainTextEdit*>(editor) )
         {
                model->setData( index, QVariant::fromValue<QString>(edit->toPlainText()) );
         }
    }
    

    (untested)



  • @raven-worx Thanks, your solution work, but in qtableWidget adding PlainText Edits and it work incorrectly because it contains some lines,how to correct this problem?


  • Lifetime Qt Champion

    @Anton-Shelenkov
    Hi
    Can you explain a bit more what is wrong with MyDelegate ?
    In what way does it work incorrectly and what do you mean with "some lines" ?

    To work correctly, what should it do ?



  • @mrjj Default qtableWidgetItem have only one row.
    In you solution in every QTableWidgetItem added QPlainTextEdit and when i press enter in TableWidgetItem is added new row and is added scrolbar. I want save tableWidgetItem behavior like lineEdit or default item.


  • Moderators

    @Anton-Shelenkov
    try adding setMaximumBlockCount(1) to the created QPlainTextEdit editor


Log in to reply