I'm displaying the query from an QSqlModel in a table view. e.G.
SELECT id, name FROM person;
I don't want to display id in the view, however I need the id when the user clicks on a row in that view.
Is there a model in Qt, which allows me to display only specified columns in views but also providing access to invisible ones?
I used to do it by hiding the columns e.g.:
ui->myview->setColumnHidden( 0, true );
But probably there is a better solution.
Since the OP amended the question to exclude using QTableView the only alternative that would seem to work is the creation of a new class derived from QSortFilterProxyModel. One can override the filterAcceptsColumn method
Documentation on the class QSortFilterProxyModel can be found here
QSortFilterProxyModel can be used for sorting items, filtering out items, or both. The model transforms the structure of a source model by mapping the model indexes it supplies to new indexes, corresponding to different locations, for views to use. This approach allows a given source model to be restructured as far as views are concerned without requiring any transformations on the underlying data, and without duplicating the data in memory.
You'd also have to override MapToSource and SourceToMap . There is a good example of usage in this StackOverflow's answer
Related
I am (still) working with Vaadin 7.6.4 and im trying to write my own custom FieldGroup.CommitHandler
I am trying to retrieve the currently added item (grid row) from the the commitEvent itself.
While debugging I can see that all the Data is available in a property called itemId if I use the following trainwreck: commitEvent.getFieldBinder().getItemDataSource() inside the Debug-Inspector, however it is private.
The itemId is the exact bean-entity i want to access. I only managed to access single fields of the entity with the following trainwreck: commitEvent.getFieldBinder().getField(propertyId).getValue(). However this is cumbersome and does not give me the entity as a whole (and to be precise does not retrieve the information from the entity, but rather from the displaying elements of the grid).
Is there a way to access the currently edited entity (which is the datasource for the edited grid row), without declaring the entire grid as a global field and call the grid.getSelected()-method?
Also, is there a data-wise difference between post- and preCommit?
Since you are apparently using BeanItemContainer based on your comment of itemId, You could try something like the following:
BeanItem item = (BeanItem) commitEvent.getFieldBinder().getItemDataSource();
MyBean bean = (MyBean) item.getBean();
getBean() in this case returns itemId.
There are two tables in SQLite. For a simple example, person and address.
How to map data of the current man from both tables to form widgets? How should we synchronize the current index of the two tables, if we use two QDataWidgetMapper's?
Is there another approach to display related data on the form, including insertion?
I would be very grateful for your help and examples.
If I got you right, you want to join two SQL tables (say person and address). If this is the case, you would need a QSqlQueryModel.
Note that QDataWidgetMapper is used for sharing a single model among multiple widgets. However in this case, it seems you want to show data from two tables in one widget.
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT name, city FROM person INNER JOIN address ON persion.id=address.id");
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("City"));
Now, you can use this 2D model wherever you need. Also you can use a QDataWidgetMapper with this model in order to share its data with multiple widgets.
I've a problem with my Qt/interview application. I use QTreeView to display tree data. I implemented my own model based on QAbstractItemModel.
I get a following error prior to application crash. It happens often after I add new record.
Could You explain to me what is the meaning of this error. What is a QPersistentModelIndex ?
I'm not using QPersistentModelIndex in my code.
ASSERT failure in QPersistentModelIndex::~QPersistentModelIndex: "persistent model indexes corrupted"
Thanks.
QPersistentModelIndexes are (row, column, parent) references to items that are automatically updated when the referenced items are moved inside the model, unlike regular QModelIndex. For instance, if you insert one row, all existing persistent indexes positioned below the insertion point will have their row property incremented by one.
You may not use them directly, but QTreeView does, to keep track of expanded items and selected items, for example.
And for these persistent indexes to be updated, you have to call the functions QAbstractitemModel::beginInsertRows() and endInsertRows() around the actual row insertion(s) when you add new records.
See the end of the section about subclassing model classes for details: http://doc.trolltech.com/latest/qabstractitemmodel.html#subclassing
I found this method QAbstractItemModel::persistentIndexList and I'm
wondering what indexes it should return. All of them ?
Should this method return all nodes currently visible in the TreeView ?
That method returns only the indexes for which a QPersistentIndexModel was created and is still in scope (as a local variable, a class member, or in a QList<QPersistentIndexModel> for example).
Expanded or selected nodes are not necessarily currently visible, so you can't (and shouldn't anyway) assume anything about what these persistent indexes are used for.
You just have to keep them updated, and you only need to use persistentIndexList for big changes in the model, like sorting (see QTreeWidget internal model : QTreeModel::ensureSorted(link)), for smaller incremental changes you have all the beginXxxRows/beginXxxColumns and endXxxRows/endXxxColumns methods.
It seems I am not capable of filtering more then one column at once. I think it's common usage when using filters, maybe I'm missing something.
For example, i have 4 columns in my QTableView, let's say column X (string), Y (int), Z (string), Q (string). I wish to filter by filter_1 column X and filter by filter_2 column Z. Is it possible to set QSortFilterProxyModel filter for more then one column (dynamically), but not all (re implementing filterAcceptsRow).
Thanks
You can use setFilterRegExp(), setFilterWildcard(), or setFilterFixedString() methods to set a filter. An example from QT doc is :
proxyModel->setFilterRegExp(QRegExp(".png", Qt::CaseInsensitive,
QRegExp::FixedString));
proxyModel->setFilterKeyColumn(1);
If these methods are not sufficient, according to Qt documentation customizing proxy models is designed to be used via inheritance.
For advanced users,
QSortFilterProxyModel can be
subclassed, providing a mechanism that
enables custom filters to be
implemented.
In this case you need to sublass and override filterAcceptsRow() method.
I really don't know if this makes sense but this is what I trying to do:
I'm doing my game's editor in QT. Currently I'm using a QStandardItemModel to store all my scene items. These items have names, position, Textures(vector of Texture which is a custom class), Animations (vector of Animation), etc.
I find it useful to have one item for row cause I can add or remove these items easily besides having them in a single place so changing this model should affect the entire app.
Now, I'm trying to do specific views for say the "Textures" of a certain item. This QTableView should show the texture's name, path etc. So, basically how can I grab the vector of Textures in the general model and fill another view without doing another model?
You will want to use a QSortFilterProxy Model. Set one up like this.
QTableView *tableView = new QTableView;
MyItemModel *sourceModel = new MyItemModel(this);
QSortFilterProxyModel *proxyModel = new QSortFilterProxyModel(this);
proxyModel->setSourceModel(sourceModel);
proxyModel->setFilterKeyColumn(column_#_to_filter_by);
proxyModel->setFilterRegExp(a_regex_that_matches_the_item_you_want_to_display);
tableView->setModel(proxyModel);
You should be able to use one model and different proxies to setup different views.