How to distinguish selected row from current row in TableView in JavaFX? - javafx

If display simple TableView sample, then select first row, then press Ctrl and then Down Arrow button two times, we will see the following picture:
I.e. first row remains selected, while third row get cursor.
How to know this row?

This is the focused row. You can access that row via the focusModel property:
int rowIndex = tableView.getFocusModel().getFocusedIndex();
The TableView.TableViewFocusModel class used for focusModel also supports some additional features like accessing the item for the focused row.

Related

Last row inserted in QTableWidget looks different

I have a QtableWidget. When an "Add" button is clicked, I added a new row to the table. The cells each row contain some custom widgets.
void addRow() {
tableWidget->insertRow(tableWidget->rowCount());
int itemRow = tableWidget->rowCount() - 1;
// create custom widgets for each column
tableWidget->setCellWidget(itemrow, columnNumber, customWidget);
}
Everything works fine. The only problem is that the last row that is seems to have slightly different size/margin/padding etc. So the widgets in each column are slightly misaligned compared to the previous rows.
When another row is added, what was previously the last row now appears fine. Its always the last row added, that "looks" slightly different.
Is there something I need to do after adding the row?

how to Change the text color of one column on datagrid when user enters data on other column

I had a very simple problem that, I had a spark datagrid of 3 columns in which 2 columns will be with data and the third column is an empty column with text input as itemeditor. So what ever the data user enters in the cells of third column should always verify with the first column and the appropriate first column value or text should turn into red. This is because user should not enter the duplicate value.So,somebody please help me. Thanks!
I am using Advanced datagrid instead of spark, in Advanced datagrid, you can do this by using the styleFunction="myStyleFunction" of a data grid or a single column.
code sample:
public function myStyleFunc(data:Object, col:AdvancedDataGridColumn):Object
{
// all rows for invalid selection combinations
if ((data["isRequired"]) && !(data["isSelected"]))
return {color:0xFF0000, fontWeight:"bold", backgroundColor:0xf2d6d7};
// Return null if the selection is valid
return null;
}
For coloring in datagrid column for best practice use itemrenderer there you can easily show color for background and foreground.
In case of text color in datagrid use this setStyle. e.g.
labelDisplay.setStyle("color",0xFFFFFF);
labelDisplay is the id of your Label inside itemrenderer.

Create Row dynamically in QListWidget

Hi,
I want to create a custome widget. Please help by giving some idea.
My intention is to make a Listwidget which shows some Information and when clicking particulat row it have to show the details coresponding to that row by creating a new area(Row or text edit) immediately below the selected row and pull the other rows below the selected row to down after the created new area.
In attachment when I click row Nancy (consider it as row) her details is coming below the selected row. Please help on this
What you want to use is the QTreeView. It is capable of showing rows with text and icons.
You need to define the QStandardItemModel for the table, this will give it column definition.
Below are three relevant functions from a class I wrote for a similar app. Note that I did not need to use any images in my rows so you'll have to figure that part out yourself.
def drawPeopleListBox(self):
header = ["Picture","First Name","Last Name","Title","Address", "City", "Region"]
self.model = QStandardItemModel(0, len(header), self)
for i in range(len(header):
self.model.setHeaderData(i, Qt.Horizontal, self.selectionDict[i+1].upper())
self.itemList = QTreeView()
self.itemList.setRootIsDecorated(False)
self.itemList.setAlternatingRowColors(True)
self.itemList.setSortingEnabled(False)
self.itemList.setModel(self.model)
self.itemList.NoEditTriggers=True
#self.itemList.clicked[QModelIndex].connect(self.onRowClick)
self.itemList.clicked.connect(self.onRowClick)
self.itemList.setCursor(Qt.PointingHandCursor)
self.itemList.setColumnWidth(0,70)
self.itemList.setColumnWidth(1,140)
self.itemList.setColumnWidth(2,70)
self.itemList.setColumnWidth(3,180)
self.itemList.setColumnWidth(4,100)
self.itemList.setColumnWidth(5,100)
self.itemList.setColumnWidth(6,100)
self.populateList(self.userDataList)
def populateList(self, userDataList):
row=[]
for user in userDataList:
for attrib in user:
row.append(QStandardItem(attrib))
for item in row:
item.setEditable(False)
self.model.appendRow(row)
def onRowClick(self, index):
print index.row()
'''
Here you need to resize the clicked row height. Also resise the image.
Or insert another row that matches your design requirement.
self.model.insertRow(rowNumber, listOfQStandardItems)
'''
self.repaint()
You may want to try using a custom delegate to view the rows. I believe the delegates know if they are selected. Using this, you would draw the unselected rows normally, and draw more information for the selected rows. The problem with this is that I don't know if you can resize the widget on selection. If not, the QTreeView solution should still work.

Flex Datagrid insert row below current row

my application needs to allow users to insert rows below the current datagrid row. My solution is to to add a row to the dataproviders collection. This works, but the row does not appear beneath the current row the user clicked on.
The Datagrid has a default sort order (date ASC), which re-orders the data...so this seems to affect the position of the row in the grid.
Any ideas how to fix this?
Two possible answers:
1. define your own sort function that sorts according to item order in dataprovider (i.e. it does nothing), and assign it to the sortFunction property
2. simply comment out the sorting of the data inside the component.

How to detect doubleClick in QTableView

I'm using PyQt to create a GUI application. In a view inherited from QTableView, need to detect the row the user has selected when they double click a row. The table has sorting, but no editing.
How do I do it?
Note - tried the doubleClicked(int) signal. It is emitted by mouse buttons, not by data cells, so it was never fired. :(
Ian
I dont understand.
The doubleClicked signal of the QTableView has the signature
void doubleClicked ( const QModelIndex & index )
If you connect that signal you should obtain the correct QModelIndex.
No need to use SIGNALs anymore:
self.your_table.doubleClicked.connect(your_function)
"doubleClicked" being inherited from QAbstractItemView.
Once you have the modelIndex, (from Frank's comment above) you can use it to find which cell was double clicked.
def slotDoubleClicked(self, mi):
row = mi.row()
column = mi.column()
You then can use these row and col values to access the table with table.setItem(row, column, newdata) or other table method
Like #regomodo said, you can simply connect your function to the double click via:
self.your_table.doubleClicked.connect(your_function)
Then, if you want to know on which row the user double clicked, you can use the following code:
for idx in self.your_table.selectionModel().selectedIndexes():
row_number = idx.row()
column_number = idx.column()
It will return an integer corresponding to the row or the column number.
There will always only be a single value as the double click remove the previous selection.
If you link your function to a push button or another signal, you can receive a list containing multiple elements selected by the user.
For example, you can easily retrieve a list of all selected rows using this code:
rows = []
for idx in self.your_table.selectionModel().selectedIndexes():
rows.append(idx.row())
rows = list(set(rows))
This will return a list of all selected rows (The set function will also remove any duplicates).
Cheers!

Resources