Hi there
PyQt is not available as tag, but this should be a simple question anyway:

  • drag & drop: from QListWidget to QListwidget
  • pushButton action: inserts chosen items from second QListWidget into QTableWidget
    But: when i run my code - most of the time the chosen items are inserted twice into my QTableWidget,
    sometimes the code runs like expected - and i can't figure out why.
    Of course i tried a "print(num)", and the added really occurs twice, for example:
    (0) ListEdit
    (0) ListEdit
    (1) TextEdit
    (1) TextEdit
    ... a.s.o.

Here's the snippet:

    vheader = self.tableWidget.verticalHeader()
    r_count = self.tableWidget.rowCount()
    c_count = self.tableWidget.columnCount()
    for num in range(self.listWidget_3.count()):
            name = self.listWidget_3.item(num).text()
            if name == 'LineEdit':
                this = r_count
                that = (c_count -1)
                line_comboBox = QComboBox()
                new_line = QLineEdit()
                self.tableWidget.insertRow(this)
                self.tableWidget.setCellWidget(this, that -1, line_comboBox)
                self.tableWidget.setCellWidget(this, that, new_line)
                vheader.setSectionResizeMode(this, QHeaderView.ResizeToContents)
            elif ......:

Err.. working on PyCharm. PyQt 5.13, Python 3.7.4 - if you'd need any further information: let me know.

Dani AI

Generated

Good catch, @MillaS — that stray use of a fixed row-count elsewhere is exactly the sort of subtle bug that produces intermittent double inserts. was also on the right track: when the same insertion routine can be triggered more than once (duplicate signal connections, overlapping drop/mouse handlers, or re-entrant signals) you’ll see duplicated rows even if a single loop looks correct.

Why this happens (brief): inserting rows while relying on a previously captured index, or letting the insert routine run twice, will either place new rows at the wrong position or perform the same insert twice. Common real-world culprits are (a) connecting the same slot twice, (b) invoking insert logic from both a drop handler and a mouse/release handler, and (c) iterating the source widget while the target is changing without first taking a snapshot.

Practical fixes you can apply immediately:

  • Take a snapshot of the items to insert (copy their texts/IDs into a list) and iterate that snapshot.
  • Compute the insertion index at the moment of insertion (query rowCount or append) rather than reusing a saved value.
  • Prevent re-entrancy by blocking signals during bulk updates (QSignalBlocker / blockSignals) or by using a simple guard flag.
  • Check your connects so the insertion slot is only connected once, and verify dropEvent/mouse handlers aren’t both calling the same code.
  • Optionally keep a small “seen” set of item IDs during the batch insert to skip accidental duplicates.

A short pattern to block signals during a batch update:

from PyQt5.QtCore import QSignalBlocker

with QSignalBlocker(tableWidget):
    # perform repeated inserts safely here (compute rowCount for each insert)
    ...

Summary: snapshot the source, compute the insertion index at each insert, and temporarily block signals — that combination eliminates the usual causes of double inserts. Congrats on tracking the issue down,

Recommended Answers

All 3 Replies

..my bad - i'm sorry. In this part there's no mistake, but other "name"s had, compared to this 'line 11.' a

self.tableWidget.insertRow(r_count)

what of course doesn't work.
Thanks for your efforts.

Since "occurs twice" appears to be the cause and what you need to investigate, where is the code that sets up or handles the drag, drop and mouse events?

PS. The code supplied doesn't appear to be causing this.

commented: Thank you! +0

:
Thank you, already solved - my bad: made a second rowcount on the other items.. events? ..in another class?! (..they will be, cause this's just been a quick workaround)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.