Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How add overwriteMode in QTableWidget.
Qt 6.11 is out! See what's new in the release blog

How add overwriteMode in QTableWidget.

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anton Shelenkov
    wrote on last edited by Anton Shelenkov
    #1

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

    raven-worxR 1 Reply Last reply
    0
    • A Anton Shelenkov

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

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @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)

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      A 1 Reply Last reply
      4
      • raven-worxR raven-worx

        @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)

        A Offline
        A Offline
        Anton Shelenkov
        wrote on last edited by
        #3

        @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?

        mrjjM 1 Reply Last reply
        0
        • A Anton Shelenkov

          @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?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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 ?

          A 1 Reply Last reply
          0
          • mrjjM mrjj

            @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 ?

            A Offline
            A Offline
            Anton Shelenkov
            wrote on last edited by
            #5

            @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.

            raven-worxR 1 Reply Last reply
            0
            • A Anton Shelenkov

              @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.

              raven-worxR Offline
              raven-worxR Offline
              raven-worx
              Moderators
              wrote on last edited by
              #6

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

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved