Create Row dynamically in QListWidget - qt

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.

Related

How to distinguish selected row from current row in TableView in 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.

How to only allow certain items to be multiple selected in a QTableWidget?

I have a QTableWidget and want items to be selectable depending on what's already selected (only if they have the same content in second column).
I know how to make items selectable or not. But everything should remain selectable so the user can select a single item to start the selection fresh. Only adding to a selection by keeping shift or ctrl pressed should allow/select only suitable items.
Where can I hook into to make adding to a selection only accept suitable rows?
Note: I'm using PySide but I can work from C++ or PyQt code just fine.
You could connect to the QTableWidget.itemSelectionChanged signal and modify the Qt.ItemIsSelectable flags of all the other items in the table based off the selection.
from itertools import product
table = QtGui.QTableWidget()
table.itemSelectionChanged.connect(self.on_itemSelectionChanged)
#QtCore.pyqtSlot()
def on_itemSelectionChanged(self):
sel_items = self.table.selectedItems()
# get all items in table
items = []
for r, c in product(range(self.table.rowCount()), range(self.table.columnCount())):
items.append(self.table.item(r, c)
# Loop through all the items in the table and
# set the selection flag based of already selected items.
for item in items:
if can_be_selected:
item.setFlags(item.flags() | QtCore.Qt.ItemIsSelectable)
else:
item.setFlags(item.flags() & ~QtCore.Qt.ItemIsSelectable)

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.

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!

How to get this QTableWidget to display items?

I have a QTableWidget and I can't get anything to show up in it.
The following appears in the constructor of the main window:
ui->tableWidget->setItem(0,0,new QTableWidgetItem("Item1"));
ui->tableWidget->setItem(0,1,new QTableWidgetItem("Item2"));
ui->tableWidget->setItem(0,2,new QTableWidgetItem("Item3"));
When I run the application, the table widget shows up, but the items do not.
I tried adding ui->tableWidget->insertRow(0); before the above code, but it didn't work.
Aha! I figured out what was going on... I needed to tell the control the number of rows it should have:
ui->tableWidget->setRowCount(2);
Example code:
//this will give the present number of rows available.
int insertRow = ui->tableWidget->rowCount();
//insert the row at the bottom of the table widget - using.
ui->tableWidget->insertRow(insertRow);
//After a new row is inserted we can add the table widget items as required.
ui->tableWidget->setItem(insertRow,0,new QTableWidgetItem("Item1"));
ui->tableWidget->setItem(insertRow,1,new QTableWidgetItem("Item2"));
ui->tableWidget->setItem(insertRow,2,new QTableWidgetItem("Item3"));

Resources