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. Maximum editable length qtablewidget item
Qt 6.11 is out! See what's new in the release blog

Maximum editable length qtablewidget item

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.2k Views 1 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
    an-01
    wrote on last edited by
    #1

    Hi,

    I have a tablewidget and I want to set the maximum length for editing of the qtablewidgetitems. However, this maximum length varies per tablewidgetitem. I read a lot of documentation and examples and it seems I have to create my own delegate.

    I found this example http://www.qtcentre.org/archive/index.php/t-36272.html, so I created the stringdelegate class. I try to use the delegate with setItemDelegate, so right now I can change the maximum length during editing for the entire table using (the fields are added in a loop varying over y)

    newField = new QTableWidgetItem(tr("%1").arg(pow((float)y, (float)0)));
    newField->setText("text");
    ui.tableWidget->setItem(y,0, newField);
    newField->tableWidget()->setItemDelegate(new stringDelegate(fieldlength[y],newField->tableWidget()));

    So I end up with a table with a maximum editable length based on the value in fieldlength.end().

    Any ideas how to get the maximum length varying for different items?

    1 Reply Last reply
    0
    • A Offline
      A Offline
      alex_malyu
      wrote on last edited by
      #2

      QModelIndex &index gives you information for which cell createEditor is called.

      So you can adjust max length to the number you want.
      Or if it is column dependent you can set different delegates on different column.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        an-01
        wrote on last edited by
        #3

        I do understand what you mean, however I still don't really understand how to apply it.

        So do I change the implementation of createEditor?

        QWidget* stringDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &/option/,const QModelIndex &index)
        const{
        QLineEdit *line = new QLineEdit(parent);
        line->setMaxLength(length);
        line->setFrame(false);
        connect(line, SIGNAL(editingFinished()),this, SLOT(commitAndCloseEditor()));
        return line;
        }

        or do I change it in the main?

        1 Reply Last reply
        0
        • A Offline
          A Offline
          alex_malyu
          wrote on last edited by
          #4

          change length in createEditor depending on row , column

          1 Reply Last reply
          0

          • Login

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