Gtk.Treeview deselect row via signals and code - python

I'm using PyGObject but I think this is a question that could be adapted to all GTK, so if someone know how to do it using C or anything should work in python also.
I have two treeview, Active and Inactive, I load data from a Sqlite database and I can swap and drag & drop items from one to other.
This is just an aestetic thing, if I click on one item on one treeview I want that a previous selected item on the other be deselected.
It appears that nobody had to do something similar because I didn't found anything about it on the net.

At the risk of being too basic (perhaps I misunderstand the problem), to manipulate treeview selections, you use the GtkTreeSelection object returned from GtkTreeView.get_selection. You can attach to signals on this object, change the current selection,etc.

To turn off selection in the other view, you can get its selection mode property and set to GTK_SELECTION_NONE. To turn it back on upon clicking, my thought was that you could catch a grab-focus signal, set the selection mode to single in that view, and set the selection mode to none in the other view:
(connect view-1 'grab-focus
(lambda args
(set-mode (gtk-tree-view-get-selection view-1) "GTK_SELECTION_SINGLE")
(set-mode (gtk-tree-view-get-selection view-2) "GTK_SELECTION_NONE")))
(That code is using the guile-gnome wrapper but the concept should be the same in any language binding.) A problem with this approach is that now in order to make a selection you must click the tree view twice - once to grab the focus, and again to make the selection.

Related

What is the difference between a Primitive, Control and Complex Control widget?

Qt supports custom paint events for drawing of widgets through QStyle.
However when using the QStyle you need to specify what type of element you're trying to draw,
i.e, if you want a control element you need to paint it by using QStyle.drawControl(CE_Example, style).
My question is, how is someone who is relatively new meant to understand which method to correctly call, or find which element to paint when I want to perform the actions myself, say for example I want to draw my SpinButton with purple up and down arrows. I tried to google and see if I could find an explanation but could not, so I was wondering if this is common terminology or if it was some jargon used by the Qt community.
Any advice for correctly traversing documentation or anything like that appreciated, maybe I have an incorrect approach.
I eventually found my answer traversing https://doc.qt.io/qt-5/qstyle.html#details
Primitive Element: A primitive element is a common GUI element, such as a checkbox indicator or button bevel.
Control Element: A control element is a part of a widget that performs some action or displays information to the user.
Complex Control: Complex controls have different behavior depending upon where the user clicks on them or which keys are pressed.

How to add a "remove button" at the right side of the hovering (or each) item of a wx.ComboBox popup

What I want is a mean to remove an item from the combobox without having to add a separate remove button somewhere else. So I want a remove button to appear at the right side on a combobox dropdown item when I hover over my mouse pointer on it. And, it is also OK if all the items have remove button at the right side and do not need hovering.
The images bellow will illustrate what I am saying [...please ignore my mspaint skils]
[combobox with remove button for hovering item]
https://i.imgur.com/kIMtF3G.jpg
[combobox with remove button for each item]
https://i.imgur.com/iyG23vG.jpg
[NOTE: Sorry, I cannot post images directly because it needs at least 10 reputation to post images.]
[I am new to python and wxpython. So please ignore my ignorance if any. And for the same reason any simple code sample will be greatly helpful.]
Regards.
The wx.ComboBox does not have this feature. The wxPython GUI toolkit uses the target platform's native widgets. If those widgets don't support doing it, then neither does wxPython.
However, wxPython does have custom widgets or you could create your own widget to do this sort of thing.
I also think you could use a context-menu for this task. You would need to right-click to make it work. Another method would be to bind to a mouse event and try to figure out where in the widget you are, but I think that method would be error prone.

How to tell when a QTableWidgetItem is being modified

I'm writing an app in Python with the PySide library. I have a QTableWidget that gets updated about every second. The thing is, I want to be able to change the data manually, and I thought that if I could find out whether or not the user is changing the data in the cell, then I could just prevent the program from updating this cell. Otherwise I get "kicked out" of the cell at each update.
Is this a good idea? Should I try something else and why?
How can I achieve my goal?
Many thanks
EDIT :
I know there exists an itemChanged signal, but what I'd really like to know is if there is a way to tell when the user is writing a new value in the cell, in order not to kick them out while editing.
In Qt Document:
void QTableWidget::itemChanged(QTableWidgetItem * item)
This signal is emitted whenever the data of item has changed.
Hope this will help you.
Edit:
QTableWidget uses a default itemdelegate(QItemDelegate instance) which has createEditor method and closeEditor signal.
You can reimplement createEditor which means edit starts, and connect the signal closeEditor which means the edit ends.
This may be the correct way.
Generally, you would handle this situation with the use of QItemDelegates, which allow you to control what cells are editable by the user, what types of controls they are given to edit the cells, and you can catch the data they input and validate or manipulate it before saving it to the model.
QItemDelegates only control edits being made using the view interface. If the table is being updated programmatically, the changes won't be sent to the QItemDelegate.
Here is an example of a QItemDelegate for a QTableWidget

GNAT GPS plug-in: Get selected line in Locations

I'm doing a python plug-in to GNAT GPS IDE.
It uses the Locations window to shows a messages to the user, therefore highlighting a file line, like errors do. I use GPS.Locations.parse() to add such information, the (somewhat hazy) documentation is here.
The user then could select the line from the code or the Locations window to perform an action.
Get the line from the opened file is a piece of cake.
But one thing that still eludes me, is how I can know which line from the Locations window the user is selecting.
I tried the GPS.MDI.get("Locations") which lead me to a maze of children windows objects. I got lost.
So how can I retrieve the text from the currently selected line in Locations?
Edit
Apparently there MessageContext that would give me what I want, but I get an error message saying there's not such class, even when the window has focus. So I recon that the class wasn't instantiated.
AttributeError: 'module' object has no attribute 'MessageContext'
I only saw this thread now. You should call GPS.current_context(), which will return an instance of GPS.Context or one of its children classes. In this case, you can likely use GPS.current_context().file()
It seems like what you want is to create an entry in the contextual menu in the Locations window. Take a look at the gps_utils.interactive hook. It wraps a python function into a GPS action. This action can then be bound to a specific contextual menu, a keybinding, a menu, a toolbar button,... You can also associate a filter with it, so that the contextual menu also shows up when the action applies (and to implement this filter, you use the above GPS.current_context().file()

Reordering items in a QTreeWidget with Drag and Drop in PyQt

I have a QTreeWidget with what I thought all the proper settings setup in order to be able to reorder items by dragging them around. It can work, at times, but more often than not I drag an item into another one and it either disappears or becomes a child.
Is there anyway to prevent this from happening so you don't lose items you're trying to reorder? I figured you could achieve this within Qt Designer. I have dragDrop mode set to InternalMove and defaultDropAction set to MoveAction, but I'm not even certain of both of those are what I need to be adjusting.
Thanks in advance!
You can add flags to individual tree widget items that control drag and drop behaviour (amongst other things). For instance, to prevent an item being a drop target, try something like this:
item = QTreeWidgetItem(parent)
item.setFlags(items.flags() & ~Qt.ItemIsDropEnabled)
For more details, see: Qt.ItemFlags

Categories

Resources