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. Direction needed (tables)
Qt 6.11 is out! See what's new in the release blog

Direction needed (tables)

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

    The cells in QTableWidget are by default editable. QTableWidget's signals notify you about changed items.

    Check out the protected functions of QTableWidget. There are functions which allow you get an item from QModelIndex and vice verse. Also check out the signals of QAbstractItemView, grandparent of QTableWidget, which provide lower-level notifications about the cells. Those protected functions allow you get the item if you are using QAbtractItemView's signals. Hence QTableWidget is expected to be inherited from if you need to.

    By the way, even if you used a QTableView with a custom model, you wouldn't have a cell represented as a QObject-based object. Notifications about data in these widgets are sent from the view or a widget derived from that view.

    1 Reply Last reply
    0
    • A Offline
      A Offline
      applefier
      wrote on last edited by
      #7

      ckakman,

      That is what I have been doing - reading on abstract models and tables - but it seemed like overkill for something so small and simple as my table is (or at least it seems simple) so I decided to ask first.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        ckakman
        wrote on last edited by
        #8

        Have you checked how QTableWidget behaves by default? It allows navigation with arrow keys or the tab key, editing a cell, multi-cell selection with Shift or Ctrl, saving the entered data with Enter, ending editing without saving data to cell with Escape. It is already there.

        You just need to connect to a couple of signals if you get notified when the user edits a cell.

        Apparently I am not seeing what functionality is missing in QTableWidget that you need.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          ckakman
          wrote on last edited by
          #9

          If your Qt and Qt Creator installed, it would take < 1 minute to start playing with QTableWidget:

          • Create a Widget project.
          • Double click the ui file to open it in Design mode.
          • Drop a table widget.
          • Edit the row and column count in the Property viewer.
          • Click Run.
          1 Reply Last reply
          0
          • A Offline
            A Offline
            applefier
            wrote on last edited by
            #10

            The fact that QTableWidget provides many things I need for my table - selectable, movable, insertable rows, navigation by Tab press and many others - is why I chose it for the base class. The only thing I had to do is specify additional key presses and system reactions to them, which was a simple and quick work but it didn't work.

            I apologize if I am not being clear enough, that is probably due to me being new to Qt.
            To reiterate, my problem is that my table has to know these things:

            • position of the active cell

            • if the active cell received a key press, and which key press (enter, tab, escape...)

            and I couldn't do it using event filters. What I am trying to figure out is another way to achieve this in the most simple and natural way.

            Thanks for your patience.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              ckakman
              wrote on last edited by
              #11

              [quote author="applefier" date="1421485441"]

              The idea is to have a table that does not require any mouse action or pressing a separate button to add, edit, verify or sync the entries with the actual data, add a new row at the end of the table, or navigate the table. Everything should be doable by key presses while working in the table and the action triggered by these presses (verify the data, sync the data, go to next cell, add a row etc) would also depend on whether the active cell contains new data or the existing data is being edited.
              [/quote]

              All of these can be done with QTableWidget without deriving from it and without using any of the base classes' members and signals.

              I have already mentioned the default navigation and editing capabilities of QTableWidget. You can validate data in a slot connected to itemChanged(). You can programmatically start editing a cell with editItem(), change the row/column count setRow/ColumnCount(), etc.

              1 Reply Last reply
              0
              • C Offline
                C Offline
                ckakman
                wrote on last edited by
                #12

                If default navigation features are not enough, you can combine these two:
                @
                void QWidget::​keyPressEvent(QKeyEvent * event)
                QList<QTableWidgetItem *> QTableWidget::​selectedItems() const
                @

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  ckakman
                  wrote on last edited by
                  #13

                  [quote author="ckakman" date="1421488880"]If default navigation features are not enough, you can combine these two:
                  @
                  void QWidget::​keyPressEvent(QKeyEvent * event)
                  QList<QTableWidgetItem *> QTableWidget::​selectedItems() const
                  @[/quote]

                  I forgot to say this: keyPressedEvent() is a protected function hence requires a QWidget subclass to reimplement it. If you just want to receive the keyPressedEvent() for the table, and not a parent of it, then you need to derive from QTableWidget.

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    applefier
                    wrote on last edited by
                    #14

                    Thanks ckakman, if what you say is true - that I can do everything I need using QTableWidget - that is great. I will go check out its documentation again.

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      ckakman
                      wrote on last edited by
                      #15

                      [quote author="applefier" date="1421490324"]Thanks ckakman, if what you say is true - that I can do everything I need using QTableWidget - that is great. I will go check out its documentation again.[/quote]

                      :) Don't take my word for it but I am pretty sure what I'm saying is true.

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

                        Yep, ckakman is right.

                        One thing you have to keep in mind if your derive from QTableWidget is to not forget to call the base class implementations of the methods you reimplement otherwise you'll loose some of the functionalities provided

                        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
                        • A Offline
                          A Offline
                          applefier
                          wrote on last edited by
                          #17

                          Thanks SGaist,

                          hearing good news from one source was great, hearing it from two is fantastic. Thanks for the reminder too, I will keep that in mind.

                          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