Skip to content
  • 0 Votes
    2 Posts
    54 Views
    jeremy_kJ

    See QAbstractItemModel - Subclassing. To add a row to an existing model, QAbstractItemModel::beginInsertRows() and endInsertRows() must be called. dataChanged() is used to announce changes to existing cells.

  • 0 Votes
    2 Posts
    76 Views
    C

    Your class implementation of, for example, QAbstractItemModel::setData() should emit the dataChanged() signal. It is all fairly well described in subclassing.

  • 0 Votes
    3 Posts
    170 Views
    S

    @jdent said in How can I extract the underlying value from QVariant?:

    Do I need to do a switch with types? is QVariant::metaType the way to find the kind of value inside the QVariant? Can you show an example?

    This would be one way to go. I have never had a need for this, but I'll give it a try how you could solve this with a switch statement:

    switch(myVariant.metaType().id()) { case QMetaType::fromType<int>().id(): // handle int break; case QMetaType::fromType<QString>().id(): // handle QString break; default: // maybe a message/log that this case is unhandled break; }

    (I have never used QMetaType this extensively. I hope that calling id() is the smartest choice to distinguish these cases.)

    std::variant also has a solution with the overload pattern (using lambdas) using std::visit. It might be possible to use the overload pattern with std::visit on QVariants as well.

  • 0 Votes
    4 Posts
    186 Views
    JonBJ

    As @mpergand says for your current situation: within your program make sure you call insertRows(), then any attached views will be notified and refresh accordingly. Depending on your Qt model type that should already be the case (e.g. for the QSql... models). If you claim to be doing that yet not seeing the update, make sure your two views are sharing the same model instance. If you have two separate model instances both open on the same underlying SQL database that is no use: Qt won't recognise that to propagate the signal notification from one instance to another.

    @jdent said in How do I refresh a model so it takes new rows added after its creation?:

    @jdent The new locations could be added directly to the Database by means of a client application - how do I refresh the locations displayed?

    Assuming you mean some other application, no matter what/how, updates the database you are using for your model, be aware that Qt offers no way of detecting this or acting on it. That would require (a) the native database/driver having a way of "notifying" other client applications such as yours that this has happened which most do not provide and (b) Qt having a way of receiving such a "notification" and acting on it/passing it onto you, which it does not. You would have to write your own code outside of Qt to handle this if it were possible (but probably not because of (a)).

    The only way you would see such a change is (a) if you were in charge of the other application such that it could send a message to your application via a socket or shared memory/semaphore or similar or (b) you will simply see the new data the next time you (happen to) fill the model from the database. To that end if you had to know you would have to set up e.g. a QTimer to keep re-querying the database in case it had changed, which obviously is not very efficient.

  • 0 Votes
    6 Posts
    339 Views
    S

    I solved it. It turns out, I uses same listdata to both models, I need to make new listdata, and pass it to the second model. So models don't share same list data. problem solved.

  • 0 Votes
    4 Posts
    741 Views
    ODБOïO

    @T_e_d_d_y nice,

    Actually you don't even need the Connections object

    model:GameBoardModel{ onPlacementChanged : {} }
  • 0 Votes
    9 Posts
    1k Views
    SGaistS

    Did you saw the third argument of the signal ? The one you are currently not using in the code you provided. It's the one containing the custom roles.

  • 0 Votes
    2 Posts
    708 Views
    IntruderExcluderI

    You can get StackView instance from Profile.qml quite easy, using StackView attached properties. Define new property at your StackView:

    StackView { id: home property var modeldata: null // Define StackView property initialItem: Pane { ... ListView { ... delegate: SwipeDelegate { ... swipe.onCompleted: { if (swipe.position > 0) { home.modeldata = model; // Set StackView property home.pop(); home.push("Profile.qml"); } ... } ... } } } }

    And then at Profile.qml you can easily get attached StackView property:

    Pane { id: profile ... Button { ... text: "Delete" onClicked: { let modeldata = profile.StackView.view.modeldata; // Accessing StackView property via attached properties if (modeldata !== null) { user_model.removeRow(modledata.index); } ... } } }

    In this case you do not need workaround with helper row. Also if you need some objects like your helper - better way is to use QtObject rather than visual 'invisible' object.

  • 0 Votes
    2 Posts
    225 Views
    JonBJ

    @Ahti
    Perhaps not relevant, but can't help noticing: your removeRows and its call to beginRemoveRows() correctly allows for multiple rows, but your call to response = model->removeRow(first, QModelIndex()); only allows for one row. That's works for your del_row(), but not in general. I don't suppose fixing that changes behaviour?

  • 0 Votes
    2 Posts
    333 Views
    sierdzioS

    @Ahti said in Inconsistency between model and view after removing a row from a table:

    JsonObject u_data;
    u_data.insert("id", model->record(index.row()).value(0).toInt());
    u_data.insert("name", model->record(index.row()).value(1).toString());
    if (role == Qt::DisplayRole)
    return u_data;

    That's inefficient. If you only support DisplayRole, then you can move that QJsonObject directly into that if statement.

    if (role == Qt::DisplayRole) { QJsonObject u_data; u_data.insert("id", model->record(index.row()).value(0).toInt()); u_data.insert("name", model->record(index.row()).value(1).toString()); return u_data; }

    Second, you should use the parent here, even if it is a flat list:

    beginRemoveRows(QModelIndex(), first, last);
    response = model->removeRow(first, QModelIndex());

    beginRemoveRows(parent, first, last); response = model->removeRow(first, parent);

    But why you get this strange behavior when removing - I don't know. Perhaps it has something to do with the fact that you have two models here - UseModel is a list model and your model variable is a QSqlTableModel (which inherits from the same base class as list model). Perhaps these calls get duplicated, although it is unlikely.

    Try adding some qDebug() calls into removeRows(), or run it through a debugger, to see what is happening.

  • 0 Votes
    3 Posts
    2k Views
    l1q1d56L

    Hi @Joel-Bodenmann , thank you, I got the point about the item delegate but I see two issues:

    implement a drag and drop listview because I ended up with empty lines (on the list view) and fields on the widget mapper:
    https://s32.postimg.org/47yyrq7id/Untitled.png implement a delegate that paint a checkbox with custom pixmap, the input with number and the input with text.
  • 0 Votes
    2 Posts
    1k Views
    mrjjM

    @tokafr said:

    Mydelegate *delegate = new Mydelegate;

    hi, i might miss something but why dont you just create another instance?

    Mydelegate *delegate2= new Mydelegate;
    view2 -> setItemDelegate(delegate2);

    Normally a delegate (instance) is not shared between views as its not intended.

  • 0 Votes
    2 Posts
    629 Views
    SGaistS

    Hi,

    It depends on your implementation. Generally speaking, you trigger the whatever is needed to grab the data over the network and once you have it you populate your model and if done correctly, this will trigger the machinery that will make all view aware that new data are available and they should update.

    Hope it helps

  • 0 Votes
    9 Posts
    6k Views
    S

    @Kofr I am stuck into a similar problem, Can you by any chance provide the working code where the TreeView(with parent-child relation) is editable too? Considering that this post was really long back, but it will be a huge help if the code (completed with the qml and C++ too) is provided! Thanks in advance!

  • 0 Votes
    2 Posts
    1k Views
    SGaistS

    Hi,

    IIRC it should already be done. What do you have in your delegate ?

  • 0 Votes
    8 Posts
    2k Views
    L

    Do you have an example of this?