How to sort View items - python

Setting an setSortingEnabled() to True makes it possible to click the column header name to sort the view's items:
tableView=QTableView()
tableView.setSortingEnabled(True)
But even while the attribute has been set the view will display the items unsorted.
In order to sort the items the header must be clicked.
Question: How to make view go ahead and to sort its items before the header is clicked.
So the view is sorted straight from the start.

You can use QHeaderView.setSortIndicator(logicalIndex, order)
For your example, this would mean calling tableView.horizontalHeader().setSortIndicator(0, Qt.AscendingOrder) to sort the first column in ascending order.
Note that you are passing in logicalIndex which may not correspond to the visualIndex if the columns have been reordered. QHeaderView provides methods for translating between the two if you need it (but I think it is unlikely you will need it).

To sort QTableView() without clicking on its header (assuming the tableView.setSortingEnabled(True) was set) use:
tableView.sortByColumn(0, Qt.AscendingOrder)

Related

Find an item by its position in a grid tkinter

In tkinter, is there a way for me to reference a widget within a grid by its row and column, in the same way that you would be able to reference an item within a list (or list of lists) by knowing its position in the list?
You can call the .grid_slaves(row, column) method on the parent widget; this will return a list (possibly empty) of the widgets in that cell.
You could also iterate over all of the child widgets (.grid_slaves() with no parameters, or .winfo_children()) and call .grid_info() on each one. This returns a dictionary with 'row' and 'column' keys, along with various other grid parameters.
Actually, I realised that I could solve my own problem in a much simpler way, by literally making a list of lists, with each sub-list containing all of the widgets for a single row, and therefore I can refer to each item through it's row and column.

Selecting all items individually in a list

I was wondering if it is possible to re-select each and every item in the rsList?
I am citing a simple example below but I am looking at hundreds of items in the scene and hence below are the simplest form of coding I am able to come up with base on my limited knowledge of Python
rsList = cmds.ls(type='resShdrSrf')
# Output: [u'pCube1_GenShdr', u'pPlane1_GenShdr', u'pSphere1_GenShdr']
I tried using the following cmds.select but it is taking my last selection (in memory) - pSphere1_GenShdr into account while forgetting the other 2 even though all three items are seen selected in the UI.
Tried using a list and append, but it also does not seems to be working and the selection remains the same...
list = []
for item in rsList:
list.append(item)
cmds.select(items)
#cmds.select(list)
As such, will it be possible for me to perform a cmds.select on each of the item individually?
if your trying to just select each item:
import pymel.core as pm
for i in pm.ls(sl=True):
i.select()
but this should have no effect in your rendering
I think for mine, it is a special case in which I would need to add in mm.eval("autoUpdateAttrEd;") for the first creation of my shader before I can duplicate.
Apparently I need this command in order to get it to work

Custom ordering of fields inside Formalchemy Fieldset

We have a pretty long table where a row should be rendered using FormAlchemy.
The requirement is that the 'title' column should be displayed first and all other fields should follow in alphabetical order. Is there a straight forward way to move and sort fields in FormAlchemy. I need a generic solution here....is touching the FieldSet._render_fields OrderedDict appropriate?
Passing the include paramter to the configure function of the fieldset will allow you to set the order. Are you looking to set it programatically? If not you can do
fieldset.configure(include=[fieldset.title, fieldset.a_field, fieldset.b_field, fieldset.c_field])

Fastest way to handle a submit button with a variable in it?

I've got a form with a bunch of items. Beside each item is a "Remove" button. When one of buttons is pressed, I need to know which one so that I know which item to remove.
The way I have it now, each button is named remove-# where # is the index of the item. In order to grab the # though I have to loop over every POST value, check if the key starts with remove- and if it does, parse out the integer.
Is there any method that would be faster than O(n) where n is the number of post vars?
Not that n is so large that this would become an expensive operation, but mostly because I'm curious.
Also, before I get any answers that suggest I use JavaScript and put an onclick event on each button to move the index into a separate hidden input.... I'm trying to do this without JavaScript.
An option would be to put each remove button in a separate form, with a hidden value with a constant name and the value indicating which item to remove.
If the POST values are stored in a dict you could turn this around and instead of looking for something that looks like a remove-# you generate all remove-# and try to use them to index the dict. That would limit the search to O(m) where m is the number of elements that can be removed.
This really is the domain of Javascript. However adding onclick isn't a great way to do this. Use HTML5 data attributes and unobtrusive listeners.. For example, in JQuery/HTML5
<input type='button' class='remove-button' name='remove' data-remove-attr='5'>
$('.remove-button').live("click", function() {
alert($(this).attr("data-remove-attr")); // Alerts 5.
});
Despite the data- being an HTML5 thing, it is backwards compatible with old IE, etc as they always allowed arbitrary dom attributes.
Edit: Note this is O(1) if as your action you did $('#remove-'+id).hide();

Referring to objects inside a list without using references or indices

I'm using python for my shopping cart class which has a list of items. When a customer wants to edit an item, I need to pass the JavaScript front-end some way to refer to the item so that it can call AJAX methods to manipulate it.
Basically, I need a simple way to point to a particular item that isn't its index, and isn't a reference to the object itself.
I can't use an index, because another item in the list might be added or removed while the identifier is "held" by the front end. If I were to pass the index forward, if an item got deleted from the list then that index wouldn't point to the right object.
One solution seems to be to use UUIDs, but that seems particularly heavyweight for a very small list. What's the simplest/best way to do this?
Instead of using a list, why not use a dictionary and use small integers as the keys? Adding and removing items from the dictionary will not change the indices into the dictionary. You will want to keep one value in the dictionary that lets you know what the next assigned index will be.
A UUID seems perfect for this. Why don't you want to do that?
Do the items have any sort of product_id? Can the shopping cart have more than one of the same product_id, or does it store a quantity? What I'm getting at is: If product_id's in the cart are unique, you can just use that.

Categories