-1

I have a QTableView with a QAbstractview with only one column. I assigned the double click event to the QTableView which calls a function that prints the selected value. The strange thing is that when there is only one record in the table it works fine. When there are more records the function is always called twice.

Thats my code:

class ViewWindow(QWidget):

def __init__(self):
    super(ViewWindow, self).__init__()
    loader = QUiLoader()
    file = QFile(abspath("ui/view.ui"))
    file.open(QFile.ReadOnly)
    self.view_screen = loader.load(file, self)
    file.close()
    self.initUI()

def initUI(self):

    self.model = QStandardItemModel(parent=self)
    self.model.setColumnCount(1)
    self.model.setRowCount(len(d))
    for i in range(0, len(d)):
        self.model.setItem(i, 0, QStandardItem(str(d[i]['file'+str(i+1)])))

    self.view_screen.tbl_attachments.setModel(self.model)

    self.view_screen.tbl_attachments.setAlternatingRowColors(True)
    self.view_screen.tbl_attachments.verticalHeader().setVisible(False)
    self.view_screen.tbl_attachments.horizontalHeader().setVisible(False)
    self.view_screen.tbl_attachments.setSelectionMode(QAbstractItemView.SingleSelection)
    self.view_screen.tbl_attachments.setEditTriggers(QAbstractItemView.NoEditTriggers)
    self.view_screen.tbl_attachments.setShowGrid(False)
    self.view_screen.tbl_attachments.setFocusPolicy(Qt.NoFocus)
    self.view_screen.tbl_attachments.setColumnWidth(0, 680)
    self.view_screen.tbl_attachments.doubleClicked.connect(self.open_attachment)


def open_attachment(self):
        print (self.view_screen.tbl_attachments.currentIndex().data())

Anyone knows the issue?

Thanks alot & best regards

EDIT:

I figured out something: when I add the elements manually it works. Adding it from the json seems to be the problem. This is how I do it:

    urllib.urlcleanup()
    f = urllib.urlopen("https://kose.kutschera.co.at/view_attachments_client.php?aussendung_id=22")
    s = f.read()
    f.close()
    d = json.loads(s)

    self.model = QStandardItemModel(parent=self)
    self.model.setColumnCount(1)
    self.model.setRowCount(len(d))
    for i in range(0, len(d)):
        attach = str(d[i]['file'+str(i+1)])
        self.model.setItem(i, 0, QStandardItem(attach))

Any ideas? - Thanks

dhirczy87
  • 33
  • 1
  • 7

1 Answers1

0

I've found the problem. I connected the print function within a function I am calling several times. So it gets connected several times ...

Thanks for your help guys.

dhirczy87
  • 33
  • 1
  • 7