alfanugraha
alfanugraha

Reputation: 13

PyQt paste function TypeError when activating QShortcut

I've tried PyQt paste function from this code successfully. Then I've modified it like this, in init function:

def __init__(self):
  ....
  self.initShortcuts()
  ....

This is snippet code for initShortcuts function with Ctrl+V shortcut key and connect to handle-paste-from-clipboard-to-QTableWidget function:

def initShortcuts(self):
  self.shortcutPaste = QShortcut(QKeySequence(Qt.CTRL + Qt.Key_V), self)
  self.shortcutPaste.setContext(Qt.WidgetShortcut)
  self.shortcutPaste.activated.connect(self.__handlePaste())

def __handlePaste(self):
  clipboard = QtGui.QApplication.instance().clipboard().text()

  rows = clipboard.split('\n')
  numRows = len(rows) - 1
  cols = rows[0].split('\t')
  numCols = len(cols)

  for row in xrange(numRows):
    columns = rows[row].split('\t')
    for col in xrange(numCols):
      item = QTableWidgetItem(u"%s" % columns[col])
      self.tblTransMatrix.setItem(row, col, item)
  ...

But it gives me the following error:

TypeError: connect() slot argument should be a callable or a signal, not 'NoneType'

Upvotes: 1

Views: 180

Answers (1)

ekhumoro
ekhumoro

Reputation: 120768

You are attempting to pass the return value of a method to connect, when you should be passing the callable itself (i.e. without parentheses):

    self.shortcutPaste.activated.connect(self.__handlePaste)

Upvotes: 0

Related Questions