QTableView doesn't show newly inserted records from QSqlTableModel



  • Hi.

    I am new here, and though I used qt for first time in my colleges times (around 2000 or so), I haven't really gotten back to it until new, for one or another reason.

    I am not entirely unfamiliar with it, and, even more than a decade after that, it's not beeing too difficult for me to get (back) the hang of it.

    I have a little question today. I have one method that's called from a slot when you click a given button at a given dialog. The method is this:

    [code]
    bool presupuesto::saveBudget(int row)
    {
    QSqlRecord record;

    if(row == -1)
    {
        //record = models.budgets->record();
        row = models.budgets->rowCount();
        models.budgets->insertRow(row);
    }
    
    record = models.budgets->record(row);
    
    // value(0) not set, 'cause it's an autoincrement field
    record.setValue(1, getCliente());
    record.setValue(2, getDireccion());
    record.setValue(3, getTelefono());
    record.setValue(4, getLocalidad());
    record.setValue(5, QDateTime::currentDateTime().toString());
    record.setValue(6, getPlanta());
    
    if(!(models.budgets->setRecord(row, record))) // && models.budgets->submitAll()))
    {
        qDebug() << Q_FUNC_INFO << "setRecord() failed";
        qDebug() << Q_FUNC_INFO << models.budgets->lastError();
        return false;
    }
    else
    {
        qDebug() << Q_FUNC_INFO << QString("record %1 submitted to the database").arg(row);
        models.budgets->select();
        emit newBudgetSaved();
        return true;
    }
    

    }
    [/code]

    The row parameter has a default value of -1, which allows me to use this same function to update a given record or to save a new one by calling this from a slot without arguments.

    As you can see I go trying here and there many things. However, I can't still figure out how to get the qtableview updated when the record is stored into the model.

    I know it is storing correctly in the DB because the next time I fire up the program I can see it listed in the QTableView that's attached to the model (or vice-versa, whatever is the correct wording for it).

    As you see, I have also tried to emit a given signal, and then connect that to the update() slot of the QTableView, and many others that sounded a bell while looking around :P

    Still, the QTableView won't refresh correctly. Or maybe it's something I need to do to the model... I know for sure that the records are being stored in the DB, that's all I can say for sure.

    Remember, it's been quite a long time, and I never ever touched the model/view stuff before (nor QSqlDatabase either, by the way ;) ). So be easy one me.

    Any input is much appreciated.

    Thank you!



  • What usually is forgotten is that when you add/remove data to the model you have to signal that to the view via: beginInsertRows () and endInsertRows or something like it.
    Between these calls you are "free" to add data to the model.
    When done, you also need to tell the view to update a certain amount of rows (if in view) with update();
    Hope this gives a bit of help. Don't want to give away all the fun, so no code examples ;-)
    Happy coding!!
    And welcome to Qt-Project!!



  • Thank you, Jeroen.

    I wouldn't like it any other way hehe. I suspect your hint will come in handy.

    The QTableView is in another class, which is responsible for handling all the ui and the "big picture". The class "presupuesto" where this function lives is a separate class which does some basic stuff with the data structures I use in the program.

    So, to sum up, I guess that I will need to pass a pointer to the QTableView unless I want to expose it as public, correct? So that I can call those methods you named from there...



  • There's something in this whole model/view thing that I am not getting right.

    Maybe it's my old mental model or something. Anyway... it seems those methods you mention are protected, so they can't be used from this class. What's would be the right procedure around this?

    Maybe you no longer need that and my problem is elsewhere?



  • The insertRows etc are still in the docs of Qt5.1
    "Model/View doc":http://qt-project.org/doc/qt-5.0/qtwidgets/model-view-programming.html
    There is a signal:
    @emit emit dataChanged(index, index);@
    in every model that is automatic linked to any connected view. The view will then "update" his view according the new datamodel information (rowCount/columnCount etc).
    Just putting a class into public domain is not an aswer, just a bad fix ;-)
    Because I don't know your desired output or software buildup it's hard to understand what you want to achieve.
    Does your "presupuesto" class hold the datamodel class? How is the datamodel connected to your view?
    There has to be a connection somewhere between your data and the view.



  • Hello again and thanks, Jeroen.

    I was more like talking aloud to myself.

    I am using 4.8/.7, developing in Linux a program that will run in win(x86). I still haven't get the time to get my hands on qt5 since I have had a whole history of bad experiences using the latest {lib,backend,cms} on day-0 in the past.

    Right now I am using what Gentoo (which is my distro) considers stable.

    Changing (back) topic, for some reason now it's working, at least most of the time. I think this is due to some strange interaction between my "presupuesto" ("budget", if you are interested) class, a QDataWidgetMapper (tied to a series of QLineEdits/QLabels, the QTableView and the QSqlTableModel.

    But, I was more interested in why are there so many people suggesting the usage of those methods you named above around the internet, when they are not usable at all (since they are protected and as far as I remember they just cannot be used).

    Anyway, just for the record, the "presupuestos" class purpose is to store the current budgets, along with the models needed to represent the current budget form, the budgets table, the articles assigned to each budgets and some other related info.

    It's not the main class, but it does contain the models we speak of (I can provide all the code, just trying to avoid polishing the forum more than strictly needed).

    The main class is called "kooker", and it manages the UI, the QDataWidgetMapper and the QTableViews and that stuff. It instantiates "presupuestos", but also some other classes that handle DBs, downloads, article tables, etc.

    For now I will just polish this a bit and see if I can reproduce back the problem. Maybe it was just some silly issue.

    I suspect that I could radically simplify "presupuestos" if I was able to use all the potential for the data widget mapper, but I am new to this stuff and it's being quite a learning curve, so I still need to rely a lot on my c++/ancient qt-fu ;)

    Thank you :)


Log in to reply
 

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