QTableView - some cells and column headers not editable



  • 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@


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



  • 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.



  • 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



  • [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?



  • 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?



  • 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?



  • 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...



  • 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?



  • 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.



  • Where can I upload the minimized project?



  • 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.



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

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



  • 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.



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



  • 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.



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



  • 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



  • [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.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.