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. QTableWidget: Only numbers (delegate)
QtWS25 Last Chance

QTableWidget: Only numbers (delegate)

Scheduled Pinned Locked Moved General and Desktop
6 Posts 3 Posters 3.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.
  • I Offline
    I Offline
    itsmemax
    wrote on last edited by
    #1

    'm currently trying to let my QTableWidget only display numbers. I read that I'd need a QAbstractItemDelegate to do so, so I read through the documentation and found the createEditor void. Here's my code I'm currently using:

    @#include "tabledelegate.h"

    TableDelegate::TableDelegate(QObject *parent) : QStyledItemDelegate(parent)
    {
    }

    QWidget* TableDelegate::createEditor(QWidget* parent,const QStyleOptionViewItem &option,const QModelIndex &index) const
    {
    QLineEdit* editor = new QLineEdit(parent);
    QDoubleValidator* val = new QDoubleValidator(editor);
    val->setBottom(0);
    val->setNotation(QDoubleValidator::StandardNotation);
    editor->setValidator(val);
    return editor;
    }@

    And I'm trying to call the Delegate by doing this in the constructor of MainWindow:

    @ui->tableWidget->setItemDelegate(new TableDelegate(ui->tableWidget));@

    But it gives me this error:

    bq. no matching function for call to 'QTableWidget::setItemDelegate(TableDelegate*)' ui->tableWidget->setItemDelegate(new TableDelegate(ui->tableWidget)); ^

    Why?

    1 Reply Last reply
    0
    • I Offline
      I Offline
      itsmemax
      wrote on last edited by
      #2

      Okay, I just forgot to subclass QStyledItemDelegate in my headerfile. But now I receive a new error at:
      @TableDelegate::TableDelegate(QObject *parent) : QStyledItemDelegate(parent)@

      bq. undefined reference to `vtable for TableDelegate'

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        You need to rerun qmake before building or do a clean rebuild

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • I Offline
          I Offline
          itsmemax
          wrote on last edited by
          #4

          Yep, I also forgot to reimplement the three other functions which are needed:

          "In this case, the following virtual functions must be reimplemented:
          createEditor() returns the widget used to change data from the model and can be reimplemented to customize editing behavior.
          setEditorData() provides the widget with data to manipulate.
          updateEditorGeometry() ensures that the editor is displayed correctly with respect to the item view.
          setModelData() returns updated data to the model."

          After doing so and running a qmake, it works. Well, at least it compiles. Whenever I click on a cell in my QTableWidget now:
          !http://i.epvpimg.com/1ydWc.png(picture)!
          The validator works, but when I click enter/go onto another cell, the cell isn't filled. Also, this looks pretty dumb.

          /edit: Nevermind. The three other functions which need to be reimplemented have got a reason, I see. Got a quick look through the docs again and managed to do it. It works as it should now! Thanks anyways, SGaist.

          1 Reply Last reply
          0
          • JeroentjehomeJ Offline
            JeroentjehomeJ Offline
            Jeroentjehome
            wrote on last edited by
            #5

            When you select an other cell, the model you have will be called in the setData() function. There you will need to store the data into your model container to keep the new value. Otherwise the old value will be kept. This is handy for validation purposes.
            For the display of your delegate look into your updateEditorGeometry settings!

            Greetz, Jeroen

            1 Reply Last reply
            0
            • I Offline
              I Offline
              itsmemax
              wrote on last edited by
              #6

              I've edited my above post already, managed to do it myself. Thanks though!

              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