32bitfloat
32bitfloat

Reputation: 771

PySide QTableWidget: working directly with rows instead of items?

I have a table with 3 columns and some rows. I would like to

To show a selection of the full row by clicking a single cell in it, I set selectionBehabior in QT Designer to SelectRows. Working.

Problem is, I have only managed to work with the single items inside a row. To show the same tooltip on every three cells I need to set it to every cell. My Code:

for entry in entries:
    #col 1
    item = QtGui.QTableWidgetItem(entry['published'])
    item.setToolTip(entry['subtitle'])
    self.tableFeedEntries.setItem(i, 0, item)
    #col 2
    item = QtGui.QTableWidgetItem(entry['author_name'])
    item.setToolTip(entry['subtitle'])
    self.tableFeedEntries.setItem(i, 1, item)
    #col 3
    item = QtGui.QTableWidgetItem(entry['title'])
    item.setToolTip(entry['subtitle'])
    self.tableFeedEntries.setItem(i, 2, item)   

    i += 1

is accordingly bloated.
I'd rather use setToolTip() and setData() once for each row, but I don't know how. Well, while these 2 things were some kind of nice-to-have, I'm facing a real problem when handling the selection event: by checking if the current selected row is not equal the previous one.

#connection
self.tableFeedEntries.currentItemChanged.connect(self.__show_feed_content)

#method
def __show_feed_content(self, current, previous):
    if current is not previous:
        data = current.data(QtCore.Qt.UserRole)
        content = self.__db.get_entry_content(data['id'])
        #etc.

This also checks the items, not the row. So if one clicks on another cell in a already selected row, the same process will run again.

How to set data and eventhandlers to a row?

Upvotes: 0

Views: 655

Answers (1)

Plouff
Plouff

Reputation: 3470

You could implement the data method of your model:

def data(self, index, role):
    if role == Qt.ToolTipRole:
        row = index.row()
        return [your tooltip]

The doc is here:

http://pyside.github.io/docs/pyside/PySide/QtCore/QAbstractItemModel.html#PySide.QtCore.PySide.QtCore.QAbstractItemModel.data

Upvotes: 1

Related Questions