Scroll QTableWidget to specific column - python

I have a bunch of data in a QTableWidget and I would like to be able to scroll to a particular column. I'm currently using scrollToItem(self.item(0, col)). However, this hardcodes the row to 0. It causes problems if a user is looking at row 100 and scrolls to a specific column since it loses their vertical place in the table.
Is there a way to find what row the user is currently viewing inside of the QScrollArea that the QTableWidget provides? If so, I could easily replace that defaulted row with the correct one.
Maybe there is another way to achieve this result with something like .ensureWidgetVisible()? However, I'm not sure how to get the correct widget that I would want to scroll to or make visible.

One way to do this is to get the row-index of the first visible row, and then use that to find the first visible item in the column you want to scoll to:
def scrollToColumn(self, column=0):
visible = self.itemAt(0, 0)
if visible is not None:
self.scrollToItem(self.item(visible.row(), column))
Since the row of the item you're scrolling to is already visible, the view should not scroll itself vertically (unless the verticalScrollMode is ScrollPerPixel and the item at point (0, 0) is not fully visible).

Related

Python tkinter: table with variable row height possible?

For my sqlite-based search Python script (a kind of two-language translation dictionary, i.e. translation memory), I've decided to use tkinter and show results in a treeview.
Everything works OK, except for the fact that for longer strings, text gets truncated: table rows are always 1 line high. If I change row height, it then applies to the whole treeview, which is not what I want.
Judging from Brian Oakley's answer here:
How to change height of only one row in a Treeview?
it seems that having variable row heights in a treeview is not possible.
So, my question is whether it is possible (using tkinter) to have a table with e.g. 3-4 columns, in which I can vary row height, depending on number of lines of wrapped text?
Perhaps something using listboxes or labels?
This is what I have now with treeview:
And this is what I would like to have (note different row heights in the table - this was done in Perl/Tk):
So, is there any way to have such table with variable row heights, using Python and tkinter?

Tkinter - Return column number of selected treeview

How do I get the number of the column that is selected in a tkinter treeview?
Right now I'm using tree.focus() but it returns something like I001 or I00A and I have no idea how to convert that to a number.
I can't index the tree because there are multiple items with the same name in it, and I want to know the exact column the user clicks on.
What I expect is to click the first item and get the integer 0 back, etc.
Thanks, please ask questions if I was confusing...
Here are docs for Treeview.
You have used the term "column" repeatedly, but when you say "multiple items with the same name" and refer to "first item" it sounds a lot like you're talking about rows.
If you want the column, you'll need to capture the click event using treeview.bind("<Button-1>", callback) or a variant of that. You would then use treeview.identify_column to get the column index based on the event's x location (keep in mind, per the docs, that if your columns are rearranged you may need to do some extra work). Here are two links if you need information on events.
If you were actually talking about rows, you can use treeview.selection() to get a list of iids of selected items, and then feed them into treeview.index() to get the 0-index of the row that you were talking about.

How to get values of selected range with right click in wx.grid in wxpython?

I can get value of a single selected cell with right click in wx.grid this way, when i right click on a cell, it prints value of that cell:
self.Bind(wx.grid.EVT_GRID_CELL_RIGHT_CLICK , self.OnSelectCell, self.mygrid)
def OnSelectCell(self, event):
row = event.GetRow()
column = event.GetCol()
print self.mygrid.GetCellValue(row,column)
event.Skip()
But I couldn't find out how to get all values in a selected range with right click, how can I do this?
It sounds like getting selected cells in the grid is actually more complicated than I thought. Fortunately, someone has already written up some code explaining it all here:
http://ginstrom.com/scribbles/2008/09/07/getting-the-selected-cells-from-a-wxpython-grid/
There are 3 ways of selecting cells in a grid, so there are 3 different ways of acquiring the selection.

QTableWidget how to have a cell selected for editing from code and/or with single click

Two part question:
I have a 10x10 QTableWidget with QTableWidgetItem in each cell.
For some reason, clicking on a cell is not sufficient to edit it, I need to double-click the cell to enter it.
Is there a way to change this behavior to single click
Is there a way to have 2nd cell in 1st row selected and ready for editing by default when window is created?
You can modify the editing behaviour in various ways with setEditTriggers.
For single-click, try:
table.setEditTriggers(QAbstractItemView.CurrentChanged)
The current edited cell can be set with editItem:
table.editItem(table.item(0, 1))
To select a cell without editing it, use setCurrentCell:
table.setCurrentCell(0, 1)
You can also use setCurrentCell.
table.setCurrentCell(0,1)
QTableWidget.setCurrentCell (self, int row, int column)

PyQt Sort List of Radio Buttons

I am trying to dynamically create a list of radio buttons that represents the open COM ports on my computer. Creating and displaying the list the first time is easy enough since I can just sort the ports to be in numerical order and then add their corresponding radio button to my vertical layout.
However, if the user inserts a new device which creates a new COM port, I have to find some way to add the new button in the correct place since it might not be in the right numerical order. So far, the only way I have been able to do this is to just get rid of all the buttons and then re-add them after sorting the list since addWidget doesn't let me specify where to add the widget. This method seems really inefficient, and I am assuming there is a simpler way, but I just have not found it yet.
Instead of using addWidget(), determine the index in the list of buttons to place the new one, and use QBoxLayout.insertWidget(index, widget) to insert it there:
newButton = QRadioButton(...)
newText = newButton.text()
index = 0
for button in get_buttons():
if button.text() >= newText:
break
index += 1
layout.insertWidget(index, newButton)

Categories

Resources