How can I remove an item with text "something" from a QStandardItemModel that filled with QStandardItem items and shown in a QListView in pyqt.
I made my QStandardItemModel like code shown below:
item = QtGui.QStandardItem("something")
QStandardItemModel.appendRow(item)
You will first need to find the items with the matching text, and then remove them from the model:
model = listview.model()
for item in model.findItems('something'):
model.removeRow(item.row())
I believe the solution given only works when findItems returns a single item.
Otherwise the first call to removeRow will invalidate the next items...
Related
I had implement TreeView following this tutorial, But I had implement with Qt Quick Control (does not has .ui file). tutorial here. With load text and show on TreeView, it work fine. But I had another problem:
I know that a row is a TreeItem and the model is TreeModel extend from QAbstractItemModel.
- My TreeView has four columns and I set data for TreeItem with its itemData, each itemData has 4 element.
On Value Column (column 2) I want to add dynamic component (QComboBox and TextField), it depend on value of TreeItem the TreeModel. The figure below.
I set data for TreeItem and TreeModel from .cpp file, and set model for TreeView in .qml file.
I have a objectA with 8 element (name, value, data to assign component for column 2, current index of Combobox if have Combobox, data1,...) with second element maybe QStringList or QString. My TreeItem get data from objectA with TreeItem (name,value,data1,data2).
I have 3 problems need to be solve:
First problem, I had delegate the column 2 to all ComboBox or all TextField ok, But I cannot mix them together. When the value of TreeItem is a QStringList, I want delegate for Combobox, and when it is QString I want delegate for TextField. But I cannot access to the return data, I dont know that I need to define it on .cpp file or .qml file. I know that "styleData.value" is the value for a cell data, access from .qml file. But I cant to know data Type of "styleData.value" .
Second problem, Another problem that I dont know how to set current index for Combobox on load data. For example: the value(second element) of TreeItem is: [item1, item2, item3] and the current Index is at item 2.
Third problem, when data is loaded, I change data for ComboBox. How to get current Data of the TreeItem, current Index of Combobox.
Sorry for long question, and a lot of problems.
Please help me if you know any my problem.
Thank in advance.
In my app I'd like to insert an item at the top of a QTreeView.
What I have so far will insert an item just above the currently selected item. The code (nicked, I think, from the EditableTreeviewDemo):
QModelIndex index = this->selectionModel()->currentIndex();
QAbstractItemModel *model = this->model();
if (!model->insertRow(index.row(), index.parent()))
return;
I guess what I need is the index to the current first row? How do I get this?
As a side question, what happens to the current index when a row is inserted? Does it continue to point to the same item, or the same row?
Well first you have to know that insertRow is a function from QAbstractItemModel and it will call insertRows (with an s). This function must be redefined in your model subclass if you want to allow insertion of data in your model.
http://doc.qt.io/qt-5/qabstractitemmodel.html#insertRows
Also consider that any parent of a topmost index is a invalid QModelIndex. Then the call to do would be :
model->insertRow(0, QModelIndex());
And because this is the default value for the second parameter, simply call :
model->insertRow(0);
Then in your redefinition of insertRows simply check the validity of you parent index to ensure you news underlying data is created where you want it to be.
For you question, inserting data in the model won't affect the current and selected items.
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.
I have a simple list in Flex that is populated every N seconds by a dataprovider. My goal is to avoid scrolling the list after the dataprovider has been changed.
So, before I populate the list, I save the selectedIndex, and once the dataProvider is filled, I call:
list.selectedIndex = index;
list.scrollToIndex(index);
Trouble is that this moves the selected item of the list to the top.
The solution would be to get the index of the first element displayed in the list: but I have no idea on how to get that. Any clue?
Perhaps something like: list.getIndexFirstVisibleElement()
You haven't mentioned if you're using Flex 4, but if you are you might want to look into ensureIndexIsVisible.
You can find an example here: http://blog.flexexamples.com/2010/05/12/scrolling-to-a-specific-index-in-a-spark-list-control-in-flex-4/.
i'm new for flex...
we can the length by using getnumber but
i want to get all item values in the list and to display the item values in the alert box one by one... is it possibile?
thank's
Can't you get them directly from your list's DataProvider (which might be an ArrayCollection or a XML object?)