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. QTableView - some cells and column headers not editable
QtWS25 Last Chance

QTableView - some cells and column headers not editable

Scheduled Pinned Locked Moved General and Desktop
19 Posts 4 Posters 12.4k 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.
  • P Offline
    P Offline
    Psychonaut
    wrote on last edited by
    #1

    Hello,
    I'll try it here, because in another forum I didn't get any helpful posts.
    Okay - let's start.

    The problem is, like mentioned above, that some cells are not editable. Even the column Headers are not clickable.
    I tried to subclass from QSqlRelationalTableModel and re-implement the flag function.
    Here is the code:

    mytablemodel.h
    @#ifndef MYTABLEMODEL_H
    #define MYTABLEMODEL_H

    #include <QtSql>

    class MyTableModel: public QSqlRelationalTableModel
    {
    public:
    MyTableModel(QObject *parent = 0);
    Qt::ItemFlags flags(const QModelIndex &index) const;
    };
    #endif@

    mytablemodel.cpp
    @#include "mytablemodel.h"

    Qt::ItemFlags MyTableModel::flags(const QModelIndex &index) const
    {
    return QAbstractItemModel::flags(index) | Qt::ItemIsEditable;
    }

    MyTableModel::MyTableModel(QObject *parent) : QSqlRelationalTableModel(parent)
    {
    }@

    It compiles fine, but it doesn't solve my problem. My Database is SQLite.

    Now the code for assigning the view etc.

    @model = new QSqlRelationalTableModel( tableView );
    model->setTable("Protokoll");
    proxy = new proxymodel(tableView);

    // ------

    model->setRelation(12,QSqlRelation("Status", "ID", "Status"));

    model->setEditStrategy(QSqlTableModel::OnFieldChange);
    tableView->setSelectionMode(QTableView::SingleSelection);
    tableView->setSelectionBehavior(QTableView::SelectRows);

    tableView->setEditTriggers(QAbstractItemView::DoubleClicked);
    proxy->setSourceModel(model);

    tableView->setModel(proxy);
    tableView->hideColumn(0);
    tableView->setItemDelegate(new QSqlRelationalDelegate(this));

    tableView->setSortingEnabled(true);
    model->select();@

    Thanks!

    Edit:
    I have following views and models:
    @- QTableView

    • QSqlRelationalTableMode
    • Subclassed QSortFilterProxyModel
    • QSqlRelationalDelegate@
    1 Reply Last reply
    0
    • Y Offline
      Y Offline
      yshurik
      wrote on last edited by
      #2

      In code you posted above there is no instantiation of MyTableModel, so then no flags() can be called

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        What is more:
        I doubt line 5 of mytablemodel.cpp compiles. It should IMHO be
        @
        return QSqlRelationalTableModel::flags(index) | Qt::ItemIsEditable;
        @

        Also, while this hack may make the cells appear editable for the user, I think it does nothing to solve the underlying issue. There probably is a good reason this cell is not editable, and it has to do with your database structure I would guess. Even if you make the cell editable in the view, that does not mean that your edits will actually be processed correctly by the underlying QSqlRelationTableModel.

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Psychonaut
          wrote on last edited by
          #4

          Hello,
          thanks for your help!
          I identified that only in the first row some cells are not editable! In any other row the cells are editable. If I delete the first row, the second row will become first and then it's not more editable.
          But the header problem couldn't be solved - it's still not clickable.

          Edit:
          @Quote: In code you posted above there is no instantiation of MyTableModel, so then no flags() can be called@

          Sorry, I forgot to post the line

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            [quote author="Psychonaut" date="1304930577"]Hello,
            thanks for your help!
            I identified that only in the first row some cells are not editable! In any other row the cells are editable. If I delete the first row, the second row will become first and then it's not more editable.[/quote]
            Hmmm... That is strange. I would expect complete columns not to be editable, not just the first row. Could you create a minimal but demo project (downloadable from somewhere) that demonstrates the issue?

            [quote]
            But the header problem couldn't be solved - it's still not clickable.[/quote]
            What do you want happening when the header is clicked?

            1 Reply Last reply
            0
            • P Offline
              P Offline
              Psychonaut
              wrote on last edited by
              #6

              bq. What do you want happening when the header is clicked?

              The column should sort (what works for those headers, whose cells are completely editable)

              Is there a way to force the selection behavior? So I can click or sort the column?

              1 Reply Last reply
              0
              • P Offline
                P Offline
                Psychonaut
                wrote on last edited by
                #7

                Could following be a solution?
                I just create a dummy row, which is completely empty and grayed out. Additionally it can't be sorted. How can I implement it? Maybe with subclassing? But where do I start?

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  andre
                  wrote on last edited by
                  #8

                  Sounds like a work-around hack, and not a solution. I would start to try and find out why your cells are not editable in the first place. Use a debugger to step into the code, and find out what happens...

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    Psychonaut
                    wrote on last edited by
                    #9

                    Hello,
                    I cant locate the error. What do you mean by "use a debugger to step into the code"? How can I watch the changes made in tableview using a debugger?
                    Don't know what to do now. It's very strange that only the first row is affected.

                    Edit: Which part of the code may be interesting regarding the changes in tableview?

                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      andre
                      wrote on last edited by
                      #10

                      Just set a breakpoint in the model you use at one or more appropriate points (the data() method comes to mind), and run using a debugger (F5 when using QtCreator). It might also help if you create a minimum but compilable example that shows the problem and that you offer for download, so others can try to re-produce the problem.

                      1 Reply Last reply
                      0
                      • P Offline
                        P Offline
                        Psychonaut
                        wrote on last edited by
                        #11

                        Where can I upload the minimized project?

                        1 Reply Last reply
                        0
                        • A Offline
                          A Offline
                          andre
                          wrote on last edited by
                          #12

                          Where ever you like. I usually use my public Dropbox folder when I upload stuff for this forum, but you may also use any other service you fancy. Qt DevNet does not provide an uploading service itself.

                          1 Reply Last reply
                          0
                          • P Offline
                            P Offline
                            Psychonaut
                            wrote on last edited by
                            #13

                            I created a minimized project:
                            http://www.sendspace.com/file/sgb6mk

                            Edit: Just put it in /Qt/Sql example folder

                            1 Reply Last reply
                            0
                            • P Offline
                              P Offline
                              Psychonaut
                              wrote on last edited by
                              #14

                              After creating a completely new database with sqlite3 command line tool with new tables etc. using sqlitebrowser the shock: same error! I don't know why this problem occurs?!
                              Edit: I created a new project and tried also instead of using QSqlRelationalTableModel to use just TableModel but there's no difference.

                              1 Reply Last reply
                              0
                              • A Offline
                                A Offline
                                andre
                                wrote on last edited by
                                #15

                                OK, I managed to build your example, and confirm your issue. I'll try to see if I can find what is going wrong.

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  andre
                                  wrote on last edited by
                                  #16

                                  I am completely at a loss. Even if I further simply your sample (use a QSqlQueryModel instead of a QSqlRelationalTableModel), I get the same behavior you described. Very, very strange indeed. I would have expected the flags() implementation to be the problem, but it seems that it isn't even implemented until the QAbstractItemModel.

                                  I tried to see what flags are returned by the model for each index, and that looks fine to me. According to this code, every index should be enabled and selectable:
                                  @

                                  void Window::getModelIndex()
                                  {
                                  int rows = tableView->model()->rowCount();
                                  int columns = tableView->model()->columnCount();
                                  for (int row(0); row<rows; ++row) {
                                  for (int col(0); col<columns; ++col) {
                                  QModelIndex idx = tableView->model()->index(row, col);
                                  qDebug() << "r:" << row << "c:" << col << "valid:" << idx.isValid() << "flags:" << idx.flags();
                                  }
                                  }
                                  }
                                  @

                                  Why those cells refuse to be selected anyway is beyond me.

                                  I think it is time to file a bug report.

                                  1 Reply Last reply
                                  0
                                  • P Offline
                                    P Offline
                                    Psychonaut
                                    wrote on last edited by
                                    #17

                                    What is the result of the bug report? How long will it take to fix that problem?

                                    1 Reply Last reply
                                    0
                                    • G Offline
                                      G Offline
                                      giesbert
                                      wrote on last edited by
                                      #18

                                      it depends on the problem complexity etc...
                                      There is no time frame for such things.

                                      If you need fast responses and fixes, you need a commercial license and support. Otherwise you could also fix it and create a merge request on www.gitorious.org

                                      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 last edited by
                                        #19

                                        [quote author="Psychonaut" date="1305785662"]What is the result of the bug report? How long will it take to fix that problem?[/quote]

                                        That depends on how important is it deemed to be (by the maintainer of that piece of code), other priorities, and how complex the issue is, as well as how well documented and easy to reproduce the issue is. That is: there is no fixed time frame. Some bugs are never solved, some the same day.

                                        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