QTableView, QSqlTableModel and programmaticaly insert inew row.



  • According Qt documentation:
    If you insert row programmatically using QSqlTableModel::insertRows(), the new rows will be marked with an asterisk (*) until they are submitted using submitAll() or automatically when the user moves to another record (assuming the edit strategy is QSqlTableModel::OnRowChange).

    My situation:
    I insert new row programmatically using QSqlTableModel::insertRowIntoTable(rec), so I not necessary using submitAll(). Edit strategy is OnManualSubmit. All works fine, but when I insert new row, in the QTableView the new rows are marked with an asterisk (). How I may the remove an asterisk () with out using submitAll() and reloading model???



  • Without any research, so I may be totally off:
    My first guess would be that the asterisk is created by the virtual headerData method. If that is the case, you can simply subclass QSqlTableModel, reimplement headerData(), and don't show the asterisk.



  • bq. Without any research, so I may be totally off:

    No you're not :D

    The asterisk is returned by the headerData() model data, see "headerData":http://qt.gitorious.org/qt/qt/blobs/de07df9001586cc18ae267591359541b7ea494a0/src/sql/models/qsqltablemodel.cpp#line459

    So one solution would be, as Andre said, to subclass QSqlTableModel to override the headerData function.



  • Well...Many thaks!

    @
    bool asterisc;

    QVariant SQLModel::headerData ( int section, Qt::Orientation orientation, int role ) const {
    if (orientation == Qt::Vertical) {
    if (asterisc && QSqlTableModel::headerData(section,Qt::Vertical) == "*") {
    return this->rowCount();
    } else return QSqlTableModel::headerData(section,orientation, role);
    } else return QSqlTableModel::headerData(section,orientation, role);
    }
    @



  • Slightly shorter:

    @
    bool asterisc;

    QVariant SQLModel::headerData ( int section, Qt::Orientation orientation, int role ) const {
    if (orientation == Qt::Vertical) {
    if (asterisc && QSqlTableModel::headerData(section,Qt::Vertical) == "*") {
    return this->rowCount();
    }
    }

    return QSqlTableModel::headerData(section,orientation, role);
    

    }
    @

    And you don't really need to check if you currently have an askterisk either, so you could return to this:
    @
    bool asterisc;

    QVariant SQLModel::headerData ( int section, Qt::Orientation orientation, int role ) const {
    if (asterisc && orientation == Qt::Vertical) {
    return this->rowCount();
    }

    return QSqlTableModel::headerData(section,orientation, role);
    

    }
    @



  • OK many thanks


Log in to reply
 

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