How to efficiently handle multiple qspinboxes each changing a separate class variable - qt

I have a class with many private variables which I need to be able to modify with qspinboxes. I can do it by having a seperate slot for each variable connected to its particular spinbox but the code is getting lengthy and repetitive.
I really want to have a single slot which takes the address of the variable to change and its new value. I think I could somehow use qsignalmapper for this but I can't figure out how. Can anyone help? -preferably with an example as I am a novice QT programmer.
I am using Qt4 with C++ and Fedora 14

What you most likely want to do is create a custom subclass of the spin box that can also keep track of 1 variable to modify. Create your custom subclass instead of the spin box, and for each spin box, pass the variable that it is supposed to be able to modify. Inside your custom class, you have one slot, which modifies the variable it knows about.
There are variations on this idea, but it is a simple way to reduce all of that repetition.

Trust me, you'll want to slog it out and just have a bunch of repeated connect() lines, and member access functions. It's not that bad. QSignalMapper is meant for parameterless signals, and to relate them with integer ID's or pointers to QObjects. That's not your scenario.

Related

Best way to list files in a QFileSystemModel()?

I'm beginning Qt/pySide programming and am trying to implement a simple QListView with QFileSystemModel as the model. I have this working and in addition have defined a name filter on the model. I'd like to get a list of all files in the QListView (or rather the underlying model).
The following code appears to do this, but is incredibly ugly and cannot possibly be the correct way. Help!
model = myQListView.model()
idx = model.index(model.rootPath())
for i in range(0, model.rowCount(idx)):
child = idx.child(i, idx.column())
print model.fileName(child)
That is the correct way of working. The whole idea of the QAbstractItemModel abstraction is to provide a unified API for accessing arbitrary and possibly dynamic data which happen to fit into a list, table or tree presentations. Because this API has to accomodate everything from a simple dummy list of a few strings to the contents of an address book, including the rich contact details, it is inherently complex. Depending on what you want to achieve, using a one-purpose tool might be better in your specific situation.
By the way, the QFileSystemModel is very dynamic in nature (the directory enumeration happens on a separate thread). You won't get meaningful data until the directoryLoaded signal is emited, you have to wait for it. If you are simply looking for a list of files to use in your code, using Python's native facilities might be easier.

Trying to use a TreeMap in a subclass entitiy

I have found help for subclassing of a mapped superclass, and I have found help on how to map a TreeMap. However, I can not find anything that covers using a TreeMap in a subclass at all. Here is the situation: I am adding a function to a long-existing application. That application has mapped entities, a certain set of which, form a hierarchy. There is a , for instance that establishes the base class. Then there are many many MANY subclasses that use the elements to map the individual subclasses. The subclasses all use elements to reference the additional table that holds the attributes of the subclass. This has worked for several years and life has seemingly been good.
Now along comes my work effort and I need to use a TreeMap in my new subclass entity (extending the same base class as all of the others) so the first thing that I do is attempt to map it the same way other subclasses do, with a inside a .
Making the story short, I got an error when I started the app and Hibernate began mapping. Researching this error led me to review the DTD for hibernate mapping files and, guess what? According to my interpretation, one can not configure a Map of any kind inside a .
That same research exposed me to the which CAN contain a Map. So, off I go configuring a inside the base class mapping.
When I did this, I got bizarre errors referencing symbols that I can't even find in the code, in mappings.... anywhere! More research and I find a reference in the Hibernate reference manual, Chapter 9, that says that Hibernate does not support mixing of and elements. So that can't be done either.
My question is: is there a solution that I have missed along the way? From the sound of things it seems that you can NOT configure a map in a subclass if you are using because the inside it won't take a map.... and, while a Map can be configured in a , I doubt that I can gather up much support around here for wading through the current mapping files and re-mapping ALL entities as entities.
Has anybody got any ideas? If I am not looking at this right, believe me, I am willing to accept that and learn!

Qt Design Promoted Qpushbutton parameterised constructor

I've been learning about subclassing and widget promotion in Qt Designer and I've promoted some QPushButtons. My promoted class represents the number keys on a calculator, and takes a parameter in the constructor. The parameter given is the number of the button and it is passed in as an int.
Is there a way to tell Qt Designer how to construct each button? The constructor needs the numbers 0-9 passed to it. If I manually modify the generated code it works; however, each time it generates the form, I have to manually edit it.
There is no simple way to achieve this while keeping your argument in the constructor I'm afraid (read this page in the docs to how you might go about it).
I think your best solution is to keep the constructor mirroring that of QPushButton (i.e. just passing a parent QWidget) and set your custom data by calling a function afterwards.

Why create new classes in R?

I know that you can create new classes in R, but why would you want to? I've thought of two reasons:
You can use the is. function to test whether an object belongs to a particular class (classifications of objects)
To only allow certain classes of entries into slots of an object (e.g., only a string for the surnmane and only a number for a zip code in the person class).
I haven't thought of situations where these benefits couldn't be achieved fairly easily by other means or when they'd really be useful.
I hope that this isn't too open ended and more concrete examples how one might use defining classes would be great. Thanks for any thoughts.
Its called Object-Oriented programming. Look it up, but in short:
Objects encapsulate behaviour - eg the behaviour of the 'print' method for a class is specific to that class. You can then keep the code for that method on that class separate from other code. You then only have to tell your users to "print" the thing - which is something they already do - and they get a nicely custom printed version of your thing, without having to use a special print function, like "printMyThing(thing)".
Objects inherit behaviour from their parent classes - eg the 'formula' method for the glm class falls back to the formula method for the lm class (not sure if this is true, but its just for illustration.
In short, its a Good Thing.

Updating a QListView when objects change externally

I've a simple question regarding the update of a QTreeView (or any subclass of QAbstractItemView) when a model object changes externally. Let's say that a list shows a subclass of QAbstractItemModel, and an item of that model gets changed outside of the list window, and we would like to update the list with the change. What is the usual strategy to achieve something like this ? I've looked at the Qt documentation of QAbstractItemModel and there is a signal named 'dataChanged' that is (or should be) emited when data from the model changes. But since this signal (as all QAbstractItemModel functions/signals/slots) work with a QModelIndex, which is not persistent as the documentation clearly says, am i supposed to store somehow a mapping of my data to QPersistentModelIndex(es), so when my data change i will be able to find the corresponding QPersistenModelIndex and use that as argument to the various QAbstractItemModel functions ? Is that what QPersistentModelIndex(es) are used for ? Or am i missing something ?
Thank you.
ps: I guess i could just reload the QTreeView, but then i wouldn't know which items were expanded or which were selected. Is there an strategy to overcome this problem and just reload the list ?
QTreeView already handles the case in which the underlying model's data changed (i.e. the model has emitted the dataChanged() signal). That means you don't need to do any additional work on the view.
If you're implementing your own model (a derived class of QAbstractItemView), and you're making a change to the contents of the model, you simply need to emit the dataChanged() signal when your change is complete. The signal/slot mechanism will automatically inform the view using that signal.

Resources