Results 1 to 2 of 2

Thread: [PyQt4] Drag and drop rows with QTableView and QSqlTableModel

  1. #1
    Join Date
    Sep 2015
    Posts
    4
    Qt products
    Platforms
    Windows

    Default [PyQt4] Drag and drop rows with QTableView and QSqlTableModel

    Hi I am trying to drag and drop single rows internally by clicking on any item in the row. I can't seem to get this to work. I can make them sortable by making the HEADERS movable, but then I must click on the header to initiate the drag which is not desirable. Here is a working example:

    Qt Code:
    1. import os, sys
    2. from PyQt4 import QtCore, QtGui, QtSql
    3.  
    4. def makeDB():
    5. import sqlite3
    6. db = sqlite3.connect("db.db")
    7. db.execute("create table if not exists table1 (value text, data text)")
    8.  
    9. query = "insert into table1 (value, data) values (?, ?)"
    10.  
    11. valueSet = (("day","today"),("time","noon"),("food","cheese"))
    12. for values in valueSet:
    13. db.execute(query, values)
    14. db.commit()
    15.  
    16. class TestApp(QtGui.QDialog):
    17. def __init__(self, model, parent = None):
    18. super(TestApp, self).__init__(parent)
    19. self.model = model
    20.  
    21. table = QtGui.QTableView()
    22. table.setModel(self.model)
    23. table.setSortingEnabled(True)
    24. table.setDropIndicatorShown(True)
    25. table.setAcceptDrops(True)
    26. table.setDragEnabled(True)
    27. table.setSelectionMode(QtGui.QTableView.SingleSelection)
    28. table.setSelectionBehavior(QtGui.QTableView.SelectRows)
    29. table.setDragDropMode(QtGui.QAbstractItemView.InternalMove)
    30. '''table.horizontalHeader().setMovable(True)
    31. table.horizontalHeader().setDragEnabled(True)
    32. table.horizontalHeader().setDragDropMode(QtGui.QAbstractItemView.InternalMove)
    33. table.verticalHeader().setMovable(True)
    34. table.verticalHeader().setDragEnabled(True)
    35. table.verticalHeader().setDragDropMode(QtGui.QAbstractItemView.InternalMove)
    36. '''
    37. layout = QtGui.QVBoxLayout(self)
    38. layout.addWidget(table)
    39.  
    40.  
    41. class myModel(QtSql.QSqlTableModel):
    42. def __init__(self, parent = None):
    43. super(myModel, self).__init__(parent)
    44. self.setEditStrategy(QtSql.QSqlTableModel.OnFieldChange)
    45.  
    46. self.setTable("table1")
    47. self.select()
    48.  
    49. if __name__ == "__main__":
    50. if not os.path.exists("db.db"):
    51. makeDB()
    52.  
    53. myDb = QtSql.QSqlDatabase.addDatabase("QSQLITE")
    54. myDb.setDatabaseName("db.db")
    55. model = myModel()
    56.  
    57. app = QtGui.QApplication(sys.argv)
    58. dl = TestApp(model)
    59. dl.exec_()
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Sep 2015
    Posts
    4
    Qt products
    Platforms
    Windows

    Default Re: [PyQt4] Drag and drop rows with QTableView and QSqlTableModel

    Adding the follow to the model subclass allows the drag to start, but I cannot drop them:

    Qt Code:
    1. def flags(self, index):
    2. return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsDragEnabled | QtCore.Qt.ItemIsSelectable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsDropEnabled
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 24th March 2014, 17:07
  2. QTableView Drag and Drop
    By alenyashka in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2012, 15:06
  3. Drag & Drop rows in a QTableWidget
    By vycke in forum Qt Programming
    Replies: 7
    Last Post: 19th January 2012, 01:01
  4. Replies: 2
    Last Post: 13th October 2010, 22:51
  5. Drag & Drop QListView: reorder QSqlTableModel
    By Earthnail in forum Qt Programming
    Replies: 0
    Last Post: 24th March 2010, 19:51

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.