Is there a nice way to map a QStandardItemModel to many QLineEdits - c++

I have a single value that needs to appear on several parts of my UI.
Where I have a list of values that is duplicated - for instance QComboboxes, I create a single QStandardItemModel and set that model on the combos.
I'd like to do something similar for this single value. Great, I thought, I'll use QDataWidgetMapper, but it turns out that QDataWidgetMapper doesn't allow one to many mappings, i.e. I can only map one widget to each column in my table. I'd like to map many.
I can think of a few roll-my-own ways around this but if there's an easy way to do it that is built in, I'd appreciate hearing it. I'm on Qt 4.7 fwiw.

You could create a QDataWidgetMapper for each widget.
Alternatively, make one widget be the 'master value' and connect its valueChanged() signal (or whatever you want to call it) to the corresponding setValue() slot of all the 'slave' widgets.

Related

QListWidget sharing the same model

If I understood the concept of the Qt models correct, then I can have multiple views, sharing the same model, so that when data is updated in the model, all views using it, will also update their view accordingly right?
Now I have multiple widgets in my application, which should have individual selections, but they should operate on the same underlying data. So when a row is added in one of the panels, the others should be able to display this new row as well.
Since the QListWidget provides all the features that I need, there would be no point in writing my own model and use it with a QListView. But I realized that QListWidget doesn't allow me to change the model, because the setModel()method is made private.
So is there some way that I can achieve this, without the need of writing a full model on my own? I'm rather new to Qt so maybe there is a ready made general purpose model, that I can use? But so far I haven't found one.
You are trying to use QListWidget to set your own model which is not possible.
You are better off using the MVC pattern the QT imposes. You can refer here on how to use the MVC pattern. This way would be more manageable and correct. Also you can make of the QStandardItemModel or the more general QAbstractListModel or QStringListModel model for lists. You can refer here for more details
But to answer your question yes they is a hack you can use.
Create a QListWidget and treat that widget as your model.
For other views create a QListView and set the model that is returned by the QListWidget
.
For e.g. refer
QAbstractItemModel* model = listWidget->model();
listView->setModel(model);
listView_2->setModel(model);
Then you can use the listWidget as your model. Any operation (add/delete) performed on the listWidget will also affect the listView and listView_2.
I am still of the opinion that instead of the hack creating your own model would be better and more correct.

Keeping a QLayout sorted

The problem:
I'm creating a class in C++ that contains a QVBoxLayout of buttons that are exclusively checked (clicked). Each button represents a Client object that contains more data than just the title of the button. The new class "SelectionLayout" will emit a signal when any of the buttons are checked and will return an id that can be associated (by the parent class) to the Client object for that button. I want to be able to add and remove buttons from this layout while keeping it sorted by button title. It seems that there is no Qt class or method for inserting a properly sorted widget into a layout, so I have come up with my own solution. I'm a big proponent of doing things the "Standard" way if possible so please let me know if there is a better solution to this problem, or if there is an error in the solution I created. I've googled for quite some time and I'm either missing something very basic, or this is a unique enough problem that everyone is creating their own solution.
My Solution:
Exclusive checking (only one button can be checked at a time) is provided by QButtonGroup, sorting however, is not. So I need an additional data structure to keep it sorted, a Map of (QString, int) seems to make sense here, because it will keep the sorting AND I can associate the checked button to an integer id that will be passed to the parent class. I also need the QVBoxLayout to stay sorted along with the map, which will take some logic of grabbing the index out of the map after inserting and relaying that to the Layout.
The parent class must be able to map the id of each button it creates to the Client object, so that we can properly handle the signal emitted by the SelectionLayout class. I can do this with a QHash (Client*, int) because we don't care about sorting.
All of this seems pretty complicated (Four data structures to hold and keep everything sorted) and I'm hoping there is an easier way.
Thanks in advance for any help, let me know if my explanation isn't adequate.

how to create ms-access like continuous subforms (widgets) in Qt?

I'm thinking of porting my access application to Qt. I am interested to learn how to do continouos subforms, sub custom widgets for presenting/editing/inserting data from recordset in a verically scrollable non datagrid fashion. Meaning I could put button, label, combo, lineEdit... whatever, for every record.
I like QTableView and delegates. I just don't know if it could be modified to fully emulate access subform.
Sidequestion(maybe the same answer)... how do they DO those continuous forms in access under the hood.
thanks
... not real application data in that example recordset
Qt MVC is probably the best/easiest answer for your question ( http://qt-project.org/doc/qt-4.8/model-view-programming.html ), and with QTableView you should be able to achieve what you want.
Another solution could be: if you have a fix set of column items in every row you could simply design a QWidget with the contents of the row and paste your items (rows) into a QVerticalLayout.
Although I would sugget to try with MVC because that's the preferred way and in this case you could even port it to use QML UI if you want (while you can use the same data classes for the 'backend'). QML is definitely the best approach for (even slightly) animated UIs, and it's mature enough to use it already (it's part of Qt 4.8 and will be the 'star' of Qt 5).

Using Qt Model/View with non-table like data and non-table/list UI?

I've been reading about Qt's Model/View framework. I find it really helpful on working with tabled data like tables from a database. My question is: will it be useful for non-table data like property list or just some bunch of data of various types? If so, how should I approach it?
The goal is to come up with an editor for some property list like data. The list is constructed at runtime and the elements are of various types (numbers, strings, booleans, and file paths, to name a few). Each element is basically a name-value pair. The name, type, and restrains (limits for example) for each element are defined at compile time. They will be assembled at runtime into different lists depending on user input. And the list of elements can change during the edit session.
The UI will most likely be combination of various pre-designed widgets assembled according to user input. They may not be list or table views.
Some pointer to design pattern or examples are also much appreciated. Thanks.
I don't see a problem with MVC framework in QT for doing that.
Basically the difference between a standard table display and this is that you create a list dynamically akin to a map of:
QMap<QString, QVariant> property_map;
You can do a:
QList<std::pair<QString, QVariant>> property_list;
which you can then use to display in a table the property. The best way would probably be:
struct {
QString prop_name;
int prop_type;
QVariant prop_value;
};
QVariant basically will provide you with a single abstraction class for data storage and it is actually what's being returned by the data() function inside the QAbstractItemModel which you might be reimplementing.
So basically you will take a property list and boil it down to the same table like data as the database.
AMENDED
If you have a Widget that you want to have this widget populated with other predefined widgets you are quite likely to have multiple problems unless widgets are of same or well defined size.
What you can do is in you Display widget define a layout like: QGridLayout or other possible layouts and then add your other widgets to it using some set of parameters, which could be done but can be somewhat of a pain.
The other approach that you may take is to place all property widgets up front on the display UI and simply turn the ones you need on and the rest off, but this only applicable if you have a well defined limited number of pre-designed widgets.
I've been using Model/View framework for quite some time now and I usually implement my own models with a backend based on Qt containers (vectors, list, etc). Even if data eventually comes from a database, working with (e.g.) a vector of database ids can dramatically improve performance (and sometimes is the only way you can do).
This trivial example from Qt docs (see "Creating a Custom Model) is the point where I started and shows how to use a QStringList as a backend for a custom model.
Once defined your model you can define your custom Views, which will draw arranged widgets based on the content of the model underneath.
When the model change, your view will change accordingly rearranging widgets when necessary.
Leveraging QVariant capabilities you should be able to render the proper widget for every datatype (e.g. a QSpinBox for a float a QComboBox for a QStringList, and so on...)

Show a download list with qt4

What is the best way to show a list of downloads using QT4?
1 QListWidget
2 QTreeWidget
3 QTableWidget
Thanks
It depends on how much functionality you want. If you just want to simply list the filenames, use a QListWidget. If you want to list other things like progress, filesize, etc. then you may want to use a QTableWidget. If you want to be able to group downloads under different categories, you can use the QTreeWidget, which will also allow for multiple table cells per row like the QTableWidget.
If you want something fancier like the firefox downloader, you may have to create an item delegate to handle the drawing yourself.
Use the model-view-controller widgets.
QListView
QTableView
QTreeView