Track movable tabs in QTabWidget - c++

I have a question regarding QTabWidget: It has a property called movable, it allow one to mix tab positions. When a tab position is moved, its index is updated. Here is my problem. Lets say I have 4 tabs, each representing some object Foo but with different parameters. That means its important to me that I know exacly which object is at which position. When I close the tab I would like to destroy the proper Foo object so I need to keep track of him. When I move a tab it changes its index, so lets say I have the object I am interested in within tab 0 and I move it one place behind, that way it is now with index 1 not 0. How can I know that? How to get notified if indexes changes? I have a list of that objects as separate property. That way if one closes the tab I can destroy and then remove an item from the list with corresponding index (but only If i wont move the tabs, because then they change indexes).
I havent posted any code examples because this is a very general problem, but if it is needed I can do it of course. I would aprichiate all help regarding this!

For the things that you have mentioned you don't need to track tabs movement. When a widget's index matters (for example, if you have index of a tab to close and want to know which widget currently lies at that index), use QTabWidget::widget to convert index to widget and QTabWidget::indexOf to convert widget to index.
Tracking tab movements is rarely needed. You can get QTabBar objects with QTabWidget::tabBar and use its QTabBar::tabMoved signal.

Related

PYQT4 List item contents are disappearing when inserting it back into the list

I have a pyqt4 setup with a custom widget as the item of the listwidget. I have two buttons in that custom widget to move it up or down the list by taking it and inserting it either 1 up or 1 down.
When it is inserted the item is still highlighted but the contents are gone.
Here is what is moving the item.
def ChangeInit(self, row, direction):
item = self.initiativeList.takeItem(row)
self.initiativeList.insertItem(row + direction, item)
row = the row the item is in
direction = 1 or -1 depending on which button is pressed
Any ideas why the item appears to be moved but the contents of it not being moved with it or at least not visible?
Let me know if you need more info.
The documentation of QListWidget.addItem states:
Warning: A QListWidgetItem can only be added to a QListWidget once. Adding the same QListWidgetItem multiple times to a QListWidget will result in undefined behavior.
Although this can be interpreted in more than one way (i.e. adding one widget multiple times simultaneously, or adding it sequentially like you do) I sugest that you create a completely new QListWidgetItem object and insert that in the list, just to be sure. Otherwise I don't know if Qt will handle the underlying indices correctly.
P.S. next time I would add the general PyQt label unless it is a problem specific to PyQt4. This might get you some more views (you now only have two after 23 hours).

QTableView re-focus the view to specific column

I have a QTableView with 80+ columns displayed in it. The subclass i have created for QTableView allows has functionality for the standard table stuff i wanted e.g order by columns move columns and rows hide column and rows etc.
However the problem i have is the view's focus. lets say you have all the data in the table, you scroll all the way to the right so you are looking at columns 70-80 (assuming 10 columns fit on the screen at once) if i click the header of row 80 (to order by column 80) the table reorders (as expected) however it also jumps the view to the last focused cell (which as far as i can tell is the cell that was last clicked)
What i want to do is not necessarily refocus on the column that was clicked because this might still change the view what im looking for is to just keep the view's focus exactly as it is instead of having it jump back to where the last clicked cell was?
Is there some flag im missing for focus policy or will i need to get the current view and set the view back to that view after mouse clicks that reorder the table and if so how could this be done.
Im aware i have provided no code for this question but it doesnt seem there is any needed since its not a bug im looking to fix more of a feature im unaware of, if you want to see any just comment
EDIT:
Im using a QSortFilterPorxyModel, this seems to get set once (when i call this->setSortingEnabled(true); after the initial call to this on contruction my table model never calls sort again. I have a slot linked to the header clicked signal, and i set the scroll bars to scrollTo() to the index clicked, but i think the sort is happening after this so it does nothing, any idea of what signal are emitted after sorting so i can catch them and then set the view back maybe?
thanks
Before you do your sort, store the current values for where the scroll bars are with
int vPos = yourQTableView->verticalScrollBar()->sliderPosition();
int hPos = yourQTableView->horizontalScrollBar()->sliderPosition();
then after the sort, set it back
yourQTableView->verticalScrollBar()->setSliderPosition(vPos);
yourQTableView->horizontalScrollBar()->setSliderPosition(hPos);
The signals you are looking for are signals of QSortFilterProxyModel inherited from QAbstractItemModel:
layoutAboutToBeChanged()
layoutChanged()
I couldn't reproduce your symptoms by creating a QTableWidget, filling it up with random stuff, and then sorting a particular column. The selected cell stays selected, but does not become visible if it has scrolled off screen.
So the question is, what is causing the behavior you're seeing. It sounds as though scrollTo() is being called by some other function. Since that's a virtual function, I would override it with a pass-through function and see when it's getting called.

wxCheckListBox Filtering function

I'm currently trying to apply a filter to the wxCheckListBox (for a search of specific elements). So far i have no idea how to do it. The Problem with it is, that I don't want to have a copy of the Control and always delete the unnecessary items from the copy, and as soon as the search changes it has to be copied again from the original and delete the items again. I was wondering if there is a way to simply hide some items and not the entire control
You can't hide the items in a wxListBox or wxCheckListBox. To have this sort of dynamic control over the items appearing in the control you need to use wxListCtrl in virtual mode.
However it's not usually really a problem to delete some items from a wxListBox and then insert them back (or, even simpler, store all the items, delete some of them from the control and then, to revert, clear the control and restore all the initially stored items).

How to make a wide virtual Tree-View Control with only two levels fast?

I basically want to have the same virtual performance I can get with a List-View control. With a List-View control you can set an ItemCount and in the LVN_GETDISPINFO notification you then fill in the information for the items once they are scrolled visible.
Now, the virtual functionality the Tree-View provides is good for very deep trees, so you would only add items once a node expands (via TVN_ITEMEXPANDING), and TVN_GETDISPINFO can be used for filling in item information once the item is scrolled visible. But what to do if you have an "always expanded" two-level tree (just for design matters) where TVN_ITEMEXPANDING wouldn't be of any use and only want to add the items once they would be visible. The problem is, there's no such thing as SetItemCount() or similar to already resize the tree.
In my case, the filling of item information (text, image, selected image) isn't the expensive part, but the inserting of items (all at one level) is.
One option would be to only insert the items which would be visible plus one invisible one, once the invisible one gets visible (detected in TVN_GETDISPINFO), I'd insert a few more and so on. But then the scrollbar would always get smaller the more I scroll down, I think that's weird.
Are there any other ideas to achieve what I want except from drawing my own control?
The whole tree would just look like this, pretty much a list, it's just that I like the tree-look.
RootNode
|
|--Item 1
|--Item 2
|--Item 3
|--Item 4
|--Item 5
|--Item 6
|--Item 7
...
|__Item 1000
As stated in many other posts, the really expensive part about the Tree-View control is using InsertItem and DeleteItem. A quick way to improve performance for those operations is making use of SetRedraw. Not only does it hide the flickering but does really speed things up, since the drawing seems to be expensive - even though TVN_GETDISPINFO is used.
Also, it's faster to rename existing items and change their data instead of deleting and adding new ones. So when I have a big list and know that the next update will contain about the same amount +/- a couple ones, I go iterate through the items, rename them, change their lparams and sync (i.e. remove/add) the remaining ones in accordance to the new data. Depending on the size of the list, making those extra calculations can have a huge performance improvement.
The Win32 TreeView control does not support the kind of virtual mode you are looking for. So you will need a custom control.

User interface for reordering a list items

I have a list of items as a part of a web application. The question is how user can manipulate the order of items in the list (not the list sort order). The typical way is to use arrow buttons to move items up or down. The other way is the drag-and-drop.
But are there any other ways for a user interface for list reordering?
There are two other sorting methods (besides those you mentioned) I've seen which work pretty well.
Click To Move
The method used for ordering items in the Gallery web photo album works pretty well for ordering photos, and it should work just as well for any set that can be represented as a sorted group of clickable elements:
Present your list of items as clickable elements.
Clicking an element "selects" it, it is highlighted to indicate it's selected.
Clicking another item moves the selected item to a position just before the clicked item.
Repeat until all items are in the desired order.
A dummy item is shown at the end of the list for moving items to the end.
This is slightly easier to use than drag-n-drop as it requires less dexterity, and you don't have to hold down the mouse button while you figure out where you want to "drop" the item.
The method could easily be extended to allow selection of multiple items (via shift-click or similar) which could then be placed in a new position in the same way.
Provide Order Numbers
Used by Netflix and some internal apps I've worked with. This works best if your users have a concrete idea of exactly what the numeric order should be (used when working with lists of instruction steps in our internal app).
Present your list of items one per line.
Provide a text entry box next to each item where the order number is displayed, starting with 1.
The user changes the order numbers in the text fields as desired.
If multiple items are given the same order number, they are placed next to each other.
Provide a button to "apply" the sort in JavaScript so that the user doesn't have to submit the entire page to see the re-arranged list. This makes it easy to work in increments.
Edit: A couple of additional thoughts on Drag-and-Drop. You might have used these before or not, but there are a few things that can make drag-and-drop more forgiving and easier to use:
Highlight the area where the item will appear when dropped. For example, show a prominent horizontal line between the two existing items where the item will be inserted if it is dropped.
Ghost the draggable item as it is dragged so that it's obvious what's being moved, rather than using a generic "dragging" cursor. This works best if the items being dragged are still legible if shown on top of one another with transparency.
Make sure the target areas where the draggable can be dropped are sufficiently large. Larger areas can be helpful for people who have trouble with the required coordination.
We've found that drag and drop can be counter intuitive for non-technical people. We have explored the Up Down Arrow which works but can also be cumbersome as you need to keep clicking up and down and it results in a lot of traffic.
Another paradigm we've explored is the Move button so each item in a list has a Move Item button when you click it new buttons are added before and after each item in the list to let you move the item to any location.
This works well when each item in the list takes a lot of space, if each list item is only a single row it can result in a cluttered interface. In our case each item was half a dozen lines of text or more. We also have add item here button before / after each item to allow insertion.
Survey Monkey uses this paradigm as well and inspired some of what we do.
Some thoughts - Very much on the ideas rather than implementation end though...
1 - Provide both up and down arrows and drag and drop, and monitor which is more popular, which type of users use which etc, then tailor from there once you have some data
2 - Add a "random" button which generates the order randomly - could be useless, could be fun depending on your app
3 - Add a "display order" field by the side of each item and allow the user to manipulate it (but make sure that you have some code to auto update the rest of the numbers when one changes) personally I think this could be very confusing, but for some users it might work
4 - Instead of drag and drop in place, have users drag to a new list
5 - For a very simple version, have a "favourite" check box, and then have the list just show the favourites first, (in alphabetical order or something)
6 - Have groups - you assign a group number to an item, all the group ones appear first, followed by group 2 etc
Hope this random rambling has been useful, if i think of anything more I'll come back...
1) A variation of Click to Move would involve having a separate target list, where the user selects the slot into which their item will move, then clicks on the original item to move it.
For example, in the following diagram, the user has already put 'E' at the head of the reordered list and has selected slot three for their next choice. Their next step would be to choose which item from the old list goes into slot three on the new list. (The row of asterisks is a feeble attempt to show that slot three is highlighted or selected.)
old new
----- -----
| A | | E |
| D | | |
| C | |*****|
| | | |
| B | | |
----- -----
Clicking on an item in the new list selects it and highlights its original slot in the old list, which is now a target. Clicking on the item a second time returns it to that original slot.
The new list should also display indicators of some sort to show that it has selectable regions, perhaps unobtrusive (low-opacity) numbered buttons in its slots or some other informative affordance.
2) Another approach would be to allow users to draw lines between the original and desired positions.
Whatever method is chosen, the process need not be chatty: there's no reason this couldn't all be done client side (with the option to save and commit changes to the server).
You can experiment with drag-and-drop using the examples of jQuery UI Sortables.
To make it obvious for the non-technical or new users you could use visual cues such as handles or arrows and maybe a tooltip on hover to suggest dragging the element.
You could even provide an animated gif demonstrating the gesture.
As soon as a user learns how to do this I think it's the easiest method of ordering a list.
Another way is to provide a small text input next to every item, so the user can enter a numerical ordering themselves; then they click a button to reorder it all at once. (I've only seen this used on sites that store the order of items, such as Netflix queue or Livejournal links.)
Sorting, by clicking on headers is very popular. Perhaps only considered as a reordering of the view of the actual list, though.
Implement a copy/paste style function? This would mean you can take an item out of the list, and then select another item, and click "paste" or CTRL+V. This is quite intuitive and would allow large lists to be manipulated easily.
You could implement multi-selection easily to move a large block of adjacent items.
Network traffic would be low (only one or two requests).
You need to make sure the "paste" is consistent. I.e. pasting always inserts above the selected item.
Take a look at Checkvist for more inspiration.
You could also use arrow keys to move up and down.
I actually think the iphone / itouch does this really well when moving application icons.
If you haven't seen it look here: http://www.youtube.com/watch?v=qnXoGnUU6uI
The 'shaking' icons are a very good visual cue that something is moveable/draggable.
So I would suggest this approach with drag an drop. Clicking and holding on an item could put it in 'moveable-mode' and this would be indicated by it shaking (or some easier to code visual cue). Then drag and drop would work in the normal way.
Implementing this in javascript is of course the challenge...
Also another thing to think about - most people make the mistake of conflating usability with learnability. Think who the users of this app are (will they use it regularly and be taught how to use it, or are they public web users who may use it infrequently and not be taught how to use it) - it might give you a different answer to what the solution should be.
To me, performing a drag and drop of list elements in place (i.e. within the list itself) is the best approach.
Not only you can give to your users the immediate idea of what their list will look like (the list is reorganizing before their eyes), but it's also very easy for them to understand the moving mechanism.
And this is the briefest way to have your users ordering the list in the shortest number of moves.
By the way, foreseeing that a list could be longer than few elements, alongside the DnD method you could provide an asynchronous way to order the list: give the user the ability to attribute an ordering number to each entry, and then click on the "Order" button.
Handling in a smart and correct way user's input, this could result in a speed up for longer lists editing.
I was thinking of "Move selected to here":
Let all items be selectable by checkboxes
Let all items have a button or icon meaning "Move selected to here"
When "Move selected to here" is clicked, all selected items are moved to this item in existing order
The edge-cases here are when the items should be moved to either end of the list. One way to solve that, is to move all selected items before the target, and reserve a special button/icon at the end to move the selection there.
I've found the following to be the quickest way to allow specifying item order regardless of list size:
1) If user wants to set the order of list/grid items, they click a "Reorder" button.
2) This opens the reordering dialog which can be used with any list or grid.
3) In the dialog, all of the items are shown in their current order in a list on the left. There is an empty list on the right.
4) The user clicks the items in the left list in the desired order. When an item is clicked, it is removed from the left list and placed in the right list in the next position. In a worst case reordering where every item needs a new order, this allows ordering a list of N items with N clicks.
5) The user can then apply the new order or cancel. Applying the order results in the "display order" field in the data being set to the final order of the items in the right list. You can decide if the "Apply" button is only enabled when the left list is empty.
6) Also available in the ordering dialog are the following controls:
a) A button to move all of the items from the left list to the right list maintaining whatever order they are in
b) A button to start over by reloading the left list in its original order and clearing the right list.
c) A button to sort the right list alphabetically (or by date or numerically depending on what the key field is)
d) Drag and drop capability in the right list to manually drag items into order.
e) An index number column in the right list which, if edited, moves the item to that position.
This provides the best of all worlds. If you have a huge list where only a few items need to be moved, move all items over, then drag the few items where desired or enter the desired index. If you have a small-ish list that needs to be completely reordered, just click the items in the desired order. And so on.
I've used this approach for many years and it has been very effective.
You can show 'Up' and 'Delete' buttons just the way Google does for SearchWiki. Most of the people have at least some experience with it now. Most people bother only with 'Upping' their choice. If they do not like a thing, and want to downvote it, removing from the list with help of the abovementioned 'Delete' button will be easier for them
You could show an overlay when hovering over an element. This overlay shows you 4 arrows (n/e/s/w) and u can click and move the element accordingly.
If you are trying to oder items across a grid like facebook's and picasaweb's photo grouping features, then that is about the only way to handle that
if you had 3 columns each with a list of items, clicking on any of the items would move the item either to the left or right, middle column could show option for left or right. you could still allow for dragging and dropping or sorting using the typical functionality for that.