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 to set limits on the text entered in any cell of a QTableView?
Forum Update on Monday, May 27th 2025

How to set limits on the text entered in any cell of a QTableView?

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 2.8k Views
  • 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.
  • M Offline
    M Offline
    Mezzo
    wrote on 9 Apr 2011, 15:03 last edited by
    #1

    Hello,

    I'm trying to do a crossword game using QT. I'm using QTableView for this and It should be that the user can enter one and only one letter in any of the editable cells in the QTableView. The problem is that I cannot seem to figure out how to put limits or restrictions on what the user can enter in each editable cell. How to do it? I have looked at QT documentation, searched online, looked in key/mouse events in QT but still I did not get how to do it. Any help well be appreciated. Thank you.

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on 9 Apr 2011, 15:15 last edited by
      #2

      Hi Mezzo,

      the trick you need is named: QStyledItemDelegate :-)

      The delegate handles the input editors (and also painting) for cells of a view.
      You create the editors there, so you can create a line edit with input restrictions :-)

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 9 Apr 2011, 15:31 last edited by
        #3

        I would indeed subclass QStyledItemDelegate, and reimplement the createEditor method to something like this:

        @
        QWidget* OneLetterDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const
        {
        //let QStyledItemDelegate do the hard work and create an editor
        QWidget* widget = QStyledItemDelegate::createEditor(parent, option, index);

        //If the editor is a a QLineEdit, we apply our limits:
        QLineEdit* lineEdit = qobject_cast<QLineEdit*>(widget);
        if (lineEdit) {
             lineEdit->setInputmask("a");
        }
        
        return widget;
        

        }
        @

        1 Reply Last reply
        0

        1/3

        9 Apr 2011, 15:03

        • Login

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