How to detect doubleClick in QTableView - qt

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!

Related

How do I Insert an item at top of QTreeView

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.

Which Class has to be reimplemented for custom Selection in QTableWidget?

QTableWidget consists of several Selection Modi, which are selectable with the method
setSelectionMode (QAbstractItemView::SelectionMode mode)
None of the given modi fits the type of interactive selection i want to have. I want to implement a selection mode where when a user clicks in one table cell, and then shift+clicks in another table cell, the resulting selection is not a sum of columns and rows between these two, but starts from the first clicked cell, goes in read direction along row by row, ending by the second click. I hope i made clear what i want to do.
Do i need to overwrite the QItemSelectionModel or the QTableWidget?
Where are the user clicks for selection are processed?
You need to create a descendant of QItemSelectionModel and reimplement select.
virtual void select(const QModelIndex & index, QItemSelectionModel::SelectionFlags command)
virtual void select(const QItemSelection & selection, QItemSelectionModel::SelectionFlags command)
When reimplementing select you can call QItemSelectionModel::select with different arguments to achieve the needed result.
Then assign an instance of the selection model to QTableWidget with setItemSelectionModel.

QT: QTableView read cell content

I'm writing a small program with QT creator (QT 5.2.1) under Windows 7 (32 bit) and I'm having problems reading the informations stored in a TableView. My application has 3 elements, a TableView to store text data, a TextBrowser to show info and a buttom.
I modified the TableView properties: when the user selects with the mouse a cell, the full row is selected and multiple row selection is not allowed.
The user select a row and when the buttom is pressed, I would like to read the content of a specific TableView cell and show it in a TextBrowser. In particular, I would like to know the row index of the selected row and read the content of the cell with that row index and a specific column index (example 2).
The pseudo-code is this:
void my_program::on_pushButton_clicked()
{
ui->textBrowser->append("button pressed");
QItemSelectionModel *select = ui->tableView->selectionModel();
int index_row = select->selectedRows();
int index_column = 2;
char cell_data[30] = ??[index_row][index_column]
ui->textBrowser->append(cell_data);
}
The main problem is that select->selectedRows() returns a QModelIndex that is a collection of indexs and I do not know how to convert it to int (since multiple selection are not allowed, it should have only one element).
I would be glad if someone can suggest me a way to proceed.
Thanks
Francesco
edit:
Hi Bogdan, thanks a lot!! I succeed to read the cell content by using
ui->textBrowser->append(ui->tableView->model()->data(ui->tableView->model()->index(2,5)).toString());
this give me the content of the cell in position 2,5.
not sure if this is the best way or not but it works !!.
Can you be a bit more precise about how to iterate the QModeIndexList ? thanks :)
selectedRows() returns QModelIndexList, thus you need to iterate over it and call QModelIndex::data() to get stored data.

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.

QTreeView: Setting all selected cells to the user-entered value at once

I have a QTreeView which has the ExtendedSelection attribute (users may select more than one non-contiguous range of cells).
I would like my app to allow them to select these multiple cells and then enter a value and have all the cells take that value.
My issue is that I don't know how to get the full range of cells passed to my model. Right now the index that is passed to the setData method is only the active cell, not the full range of selected cells.
In the past, I have had my view store the currently selected range in the model every time it changes, and then use that to control which cells to modify. This seems kind of hacky and I wonder whether anyone has a better and more elegant solution.
I am using PyQt by the way, though I suspect this applies to QT by itself.
Thanks!
I believe you can use selectionModel method of the QAbstractItemView class to iterate through selected indexes and change values of the corresponding cells. Below is a small example:
foreach (QModelIndex index, ui->treeView->selectionModel()->selectedIndexes())
{
qDebug() << "Changing index " << index.row();
ui->treeView->model()->setData(index, "new data");
}
hope this helps, regards

Resources