Where and when QModelIndex created - qt5

Class QAbstractItemModel have a method QAbstractItemModel::createIndex() that creates QModelIndex.
When this object created for each QComboBox item for example?
In documentation we can find:
Note: Model indexes should be used immediately and then discarded. You should not rely on indexes to remain valid after calling model functions that change the structure of the model or delete items. If you need to keep a model index over time use a QPersistentModelIndex.
So it seems temp object, but who create it? Is it model, but i did not find any sign that model itself create index in source code, or it View (Widget) creates index before it reads data from model?
How QComboBox store values and indexes that readed from Model, or it reads it from every time it need to update data?
How QComboBox actually read data, is it just loop through from 0 to rowCount() and creates new index for each value then use data( const QModelIndex &index, int role ) to read?

Ok, after i wrote my own widget with model i have an answer - QModelIndex created by widget itself.
As example you can use my project on github:
https://github.com/vasiliyaltunin/articles.blog.altuninvv.ru/tree/master/qt5/Models/LampModel
When status of lamp updated
https://github.com/vasiliyaltunin/articles.blog.altuninvv.ru/blob/1068fdd88466b0e2dcf818af1e305e7d41896b19/qt5/Models/LampModel/qlampwidget.cpp#L344
I use model method data() to get status from model
this->setStatus(model->data(QModelIndex(),Qt::DisplayRole).toInt());
Because i have only one "cell" in model, i pass empty QModelIndex() to model.
So it widget itself create indexes based on row and column count returned by model.

Related

Pyomo: How to create new a model variable without declaring its name, and then store it in a dictionary?

I was wondering if there is a way to create a variable that attaches to the model without specifying its name. Or, how do I dynamically create variables with different names in pyomo?
Background:
I currently have a concrete model for a master problem and another concrete model for a subproblem. My goal is to solve a two stage optimization problem using a column and constraint generation algorithm under this master and subproblem framework.
I am trying to constrict a while loop to add new variables and constraints to the master problem. Specifically, I am trying to create a new variable for each iteration of a while loop. Then I'd like to add to the master problem some new constraints/cuts that involve the new variable in each iteration.
To do this, I was trying to construct a dictionary using a for loop. In the dictionary, each key corresponds to a newly created variable. I wrote something like this:
for i in I:
xname= 'x' + str(i)
new_x[xname] = model.add_component(xname, pyo.Var())
where I is the set of iteration counts used to create new variables and assign them to a dictionary key.
However, this doesn't work because model.add_component(xname, pyo.Var()) cannot be assigned to a key since it is a procedure that adds the new variable directly to the model. However, I can't use the standard syntax model.x = pyo.Var() to create the new variables either because it means I have to do it manually, defeating the purpose of a while loop.
I was wondering if there is a way to create a variable that attaches to the model without specifying its name. Or, how do I dynamically create different variables in pyomo?
Thank you!
Some first options:
You could use setattr(model, xnnam, pyo.Var) in your while loop instead;
You could create a set using 'I' elements, then the variable indexed
over I and solve the model for each 'i' only in your while loop.
Hope these make sense.

Yii: zii.widgets.CDetailView - declare attrs in model or just in arguments of the widget?

I have a model (let's call it M). This model has a relation (let's call it R) to an other model (X).
By default Gii generates the code which shows R as a numeric ID (primary key in the DB). I want to show it as a hyperlink.
I consider two ways to do it with zii.widgets.CDetailView:
provide extra arguments to $this->widget('zii.widgets.CDetailView');
define method getHyperlink() in my model class and refer to the property as ->hyperlink.
Which of these two ways is better?
The first way may require duplicate code (say in index.php with zii.widgets.CListView generated by Gii).
The second way requires attributeLabels() with new attribute hyperlink which would have the same title as an other (non-hyperlinked numeric) attribute. So I write the same title two times.
So, what of these two variants is better?
Generally, I would think that the second method using the model would be better due to being more DRY.
If you decide that you wanted to use the hyperlink inside of another view, then you would not have to redefine the logic with extra arguments to $this->widget('zii.widgets.CDetailView');

How to share one AJAX call between two stores?

I have two Sencha/ExtJS4 grids which use the exact same data (ie, same store.proxy.url), but each uses different filters, so each has its own separate store. The problem is I am making an unnecessary AJAX call to retrieve the extra copy to work with.
What is the recommended approach to make a single AJAX call and then share the data between two stores, for independent filtering?
potential solutions:
create two classes that extend the same store ?
use the same proxy instance ?
retrieve one store then cloning it ?
The Ext JS 4 framework seems to be built with the intention that each view receives its own store. As mentioned in other answers, your best option is to create a second store and copy all the records from one to the other.
function cloneStore(src, dest) {
var recs = src.getRange(); // returns array of records
dest.loadRecords(recs); // removes existing records before batch add
}
The exact implementation of that function may vary depending on how you need your data spread out. If each grid only needs a subset of the data to begin with, you can initialize a master store from your Ajax call, then create two sub-stores using filters directly on the store.data MixedCollection.
// Note: This function isn't exactly "good practice"
// Actual implementation may vary
function populateSubStores(master, storeA, storeB) {
var dataA = master.data.filter(/* filter criteria for store A */),
dataB = master.data.filter(/* filter criteria for store B */);
// dataA and dataB are MixedCollections of records
storeA.loadRecords(dataA.getRange());
storeB.loadRecords(dataB.getRange());
}
Or some variation thereof. This should be enough to get you started in the right direction.
If you're really gung-ho, you could create a new type of store that maintains separate MixedCollections representing filter states from different views, then return each filter state as a store with an identical interface to Ext.data.Store but with an implementation that operates on the "master" store's internal representation so that existing views can operate without overrides. But I don't recommend it.
You can create two instances of one store and then just copy data from one store to another using getRange() and add() methods. Creating two classes doesn't seem reasonable.

Interactive QSqlTableModel

Please could you give me an advice. I am using QSqlTableModel class to access the database table and QTableView to view it. What signal of what instance should I handle to know about user move the cursor in QTableView?
I want to update the content of TableView B after the cursor moved in QTableView A (Table B have foreign keys to table A in database)
May be somewhat from this http://doc.trolltech.com/latest/qabstractitemmodel.html?
Thanks.
Ivan, if you are talking about table cursor, you can reimplement QAbstractItemView::moveCursor method which is virtual.
If you're talking about mouse cursor, you can use QAbstractItemView::viewportEvent method to detect mouse move event. You need to set QWidget::setMouseTracking(true) to the viewport of your QTableView.
Hope that helps
Another way is using the selection model
Using a selection model
The standard
view classes provide default selection
models that can be used in most
applications. A selection model
belonging to one view can be obtained
using the view's selectionModel()
function, and shared between many
views with setSelectionModel(), so the
construction of new selection models
is generally not required.
If you have an shared selection model the views will be updated not matter which one changes. You can then react to it. The selection flags control if you want a cell, row or multiple selections.
See also working with selections :
//selection changes shall trigger a slot
QItemSelectionModel *selectionModel= treeView->selectionModel();
connect(selectionModel, SIGNAL(selectionChanged (const QItemSelection &, const QItemSelection &)),
this, SLOT(selectionChangedSlot(const QItemSelection &, const QItemSelection &)));
}

How to use GtkTreeView correctly

I am using a TreeView with a ListStore as model. When the user clicks on a row I want to take some action but not using the values in the cells, but using the data I created the row from...
Currently I have the TreeView, the TreeModel (ListStore) and my own data (which I ironically call model)..
So the Questions are:
Is it "right" to have a model - an object representation of the data I want to display and fill a ListStore with that data to display in a TreeView, or would it be better to implement an own version of TreeModel (wrapping my data-model) to display the data?
And also:
If someone double-clicks in a row I can get the RowActivated event (using C#/Gtk#) which provides a Path to the activated row. With that I can get a TreeIter and using that I can get the value of a cell. But what is the best practice to find the data object from which the row was constructed in the first place?\
(Somehow this question got me to the first one - by thinking would getting the data object more easy if I tried to implement my own TreeModel...)
It's quite awkward/difficult to implement TreeModel, so most people simply synch the data from their "real" model into a TreeStore or ListStore.
The columns in the store do not have to match the columns in the view in any way. For example, you can have a column that contains your real managed data objects.
When you add a cellrenderer to a TreeView (visual) column, you can add mappings between its properties and the columns of the store. For example, you could map one store column to the font of a text cellrenderer, and another store column to the text property of the same cellrenderer. Each time the cellrenderer is used to render a particular cell, the mappings will be used to retrieve the values from the store and apply them to the properties of the renderer before it renders.
Here's an example of a mapping:
treeView.AppendColumn ("Title", renderer, "text", 0, "editable", 4);
This maps store column 0 to the renderer's text GTK property and maps store column 4 to the editable property. For GTK property names you can check the GTK docs. Note that the example above uses a convenience method that adds a column, adds a renderer to it and add an arbitrary number of mapping via params. To add mappings directly to a column, for example a column with multiple renderers, pack the renderers into the column then use TreeViewColumn.AddAttribute or TreeViewColumn.SetAttributes.
You can also set up a custom data function that will be used instead of mappings. This allows you to set the properties of the renderer directly, given a TreeIter and the store - so, if all the data you want to display is trivially derived from your real data objects, you could even have your store only contain a single column of these objects, and use data funcs for all the view columns.
Here's an example of a data func that does exactly what the mapping example above does:
treeColumn.SetCellDataFunc (renderer, delegate (TreeViewColumn col,
CellRenderer cell, TreeModel model, TreeIter iter)
{
var textCell = (CellRendererText) cell;
textCell.Text = (string) model.GetValue (iter, 0);
textCell.Editable = (bool) model.GetValue (iter, 4);
});
Obviously data functions are much more powerful because they enable you not only to use properties of more complex GTK objects, but also to implement more complex display logic - for example, lazily processing derived values only when the cell is actually rendered.