Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QTableWidget Drop event get cell

[SOLVED] QTableWidget Drop event get cell

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fatuo
    wrote on last edited by
    #1

    I have implemented a Drop event for a QTableWidget, and i have 2 problems:

    1 - The detection of the row fails when there is only one row, source.rowAT(position.y()) gives me -1 , if there is more than one row, at the same position (first row) get the correct number (1).

    2- The text drop fails, when i drop a file it works correctly (the PATH is saved)

    The code:
    @
    self.ZipTable.installEventFilter(self)
    .........

    def eventFilter(self, source, event):
    if (event.type() == QEvent.DragEnter):
    if event.mimeData().hasUrls or event.mimeData().hasUrls is not None or event.mimeData().text() != '':
    event.accept()
    return True
    else:
    event.ignore()
    return False

    if (event.type() == QEvent.Drop):
    if event.mimeData().hasUrls:
    for url in event.mimeData().urls():
    item = QTableWidgetItem()
    item.setText(str(url.toLocalFile()))

     position = event.pos()
     row = source.rowAt(position.y())
     column = source.columnAt(position.x())
     print row,column
    
     if row <= 0 or column <= 0 :
      self.AddRow(source, icon = eliminar_icono)
      source.setItem(source.rowCount()-1,column, item)
     else:
      source.setItem(row-1,column, item)
    

    elif event.mimeData().text() != '':
    item = QTableWidgetItem()
    item.setText(event.mimeData().text())

    position = event.pos()
    row = source.rowAt(position.y())
    column = source.columnAt(position.x())
    print row,column
    
    if row <= 0 or column <= 0 :
     self.AddRow(source, icon = eliminar_icono)
     source.setItem(source.rowCount()-1,column, item)
    else:
     source.setItem(row-1,column, item)
    

    return True
    else:
    return False
    @

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      The rowAt() and columnAt() expect a point in table content coordinates. The event delivers a point in widget coordinates.
      To get the right point you need to subtract the size of the table headers from the event pos.

      1 Reply Last reply
      0
      • F Offline
        F Offline
        Fatuo
        wrote on last edited by
        #3

        Solved.

        New code:
        @
        self.ZipTable.installEventFilter(self)
        .........

        def eventFilter(self, source, event):
        if (event.type() == QEvent.DragEnter):
        if event.mimeData().hasUrls or event.mimeData().hasUrls is not None or event.mimeData().text() != '':
        event.accept()
        return True
        else:
        event.ignore()
        return False

        if (event.type() == QEvent.Drop):
        if event.mimeData().hasUrls:
        for url in event.mimeData().urls():
        item = QTableWidgetItem()
        item.setText(str(url.toLocalFile()))

         position = event.pos()
         row = source.rowAt(position.y())
         column = source.columnAt(position.x())
         print row,column
        
         if row <= 0 or column <= 0 :
          self.AddRow(source, icon = eliminar_icono)
          source.setItem(source.rowCount()-1,column, item)
         else:
          source.setItem(row-1,column, item)
        

        elif event.mimeData().text() != '':
        item = QTableWidgetItem()
        item.setText(event.mimeData().text())

        position = event.pos()
        
        # 30 is the row Height
        row = source.rowAt(position.y() - 30)
        
        column = source.columnAt(position.x())
        print row,column
        
        if row <= 0 or column <= 0 :
         self.AddRow(source, icon = eliminar_icono)
         source.setItem(source.rowCount()-1,column, item)
        else:
         source.setItem(row,column, item)
        

        return True
        else:
        return False
        @

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Aww, don't hardcode the height. Headers can be resized or hidden entirely. Their size depends on contents, font size, dpi settings and system style applied. 30 is not an universal value. For example on my computer the default is different.

          What you should do is check if there is a header at all with horizontalHeader(). Then check if the header is not hidden with isVisible(). Then take the height with height().

          Apart from that you should apply the same logic to columns. The x coordinate should be adjusted too.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved