синхронные QTableView и DragDrop

Убил день на поиск драгдропа из QTableView
нашел!
ключевое слово: model.flags

#!/usr/bin/env python
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtSql import *

def createConnection():
    db = QSqlDatabase.addDatabase("QSQLITE")
    db.setDatabaseName(":memory:")
    if not db.open():
        QMessageBox.critical(0, qApp.tr("Cannot open database"),
                qApp.tr("Unable to establish a database connection.\n"
                              "This example needs SQLite support. Please read "
                              "the Qt SQL driver documentation for information "
                              "how to build it.\n\nClick Cancel to exit."),
                QMessageBox.Cancle, QMessageBox.NoButton)
        return False
    query = QSqlQuery()
    query.exec_("create table person(id int primary key, "
                "firstname varchar(20), lastname varchar(20))")
    query.exec_("insert into person values(101, 'Danny', 'Young')")
    query.exec_("insert into person values(102, 'Christine', 'Holand')")
    query.exec_("insert into person values(103, 'Lars', 'Gordon')")
    query.exec_("insert into person values(104, 'Roberto', 'Robitaille')")
    query.exec_("insert into person values(105, 'Maria', 'Papadopoulos')")
    query.exec_("insert into person values(106, 'Ma', 'Papa')")
    return True

def createView( title, model ):
    return view

class Window(QWidget):

    def __init__(self, parent = None):
        QWidget.__init__(self, parent)

        model = QSqlTableModel(self)
        model.setTable("person")
        model.setEditStrategy(QSqlTableModel.OnManualSubmit)
        model.select()
        model.setSupportedDragActions(Qt.ActionMask)
#!!!!!!!!!!!!!!
        def setFlags(index):
            return Qt.ItemIsDragEnabled|Qt.ItemIsDropEnabled|Qt.ItemIsSelectable|Qt.ItemIsEnabled
        model.flags=setFlags
#!!!!!!!!!!!!!!
        model.setHeaderData(0, Qt.Horizontal, QVariant(QObject.tr(model, "ID")))
        model.setHeaderData(1, Qt.Horizontal, QVariant(QObject.tr(model, "First name")))
        model.setHeaderData(2, Qt.Horizontal, QVariant(QObject.tr(model, "Last name")))

        view1 = QTableView(self)
        view1.setModel(model)
        view1.setViewportMargins (-100, -100, -100, -100)
        view1.verticalScrollBar().hide()
        view1.horizontalScrollBar().hide()
        view1.setSelectionBehavior(QAbstractItemView.SelectRows)
        view1.setContextMenuPolicy(Qt.ActionsContextMenu)

        def delrow():
            view1.model().removeRow(view1.currentIndex().row())
            view1.model().submitAll()

        a=QAction("delete", view1)
        a.triggered.connect(delrow)
        view1.addAction(a)

        def listtbl():
            c=QSqlQuery("select * from person")
            c.last()
            print "Total number records: ",  c.at()+1

        b=QAction("check", view1)
        b.triggered.connect(listtbl)
        view1.addAction(b)


        view2 = QTableView(self)
        view2.setModel(model)
        view2.setSelectionBehavior(QAbstractItemView.SelectRows)
        view1.selectRow(0)
        view2.selectRow(0)
        layout = QHBoxLayout()
        layout.addWidget(view1)
        layout.addWidget(view2)
        frame=QGroupBox("123--------------------------")

        layout.addWidget(frame)
#!!!!!!!!!!!!!!
        view2.setDragEnabled(True)
        frame.setAcceptDrops (True)
#!!!!!!!!!!!!!!
        self.setLayout(layout)
        layout.setMargin( 1)
        layout.setSpacing(0)
        ############################################
        def selectionChanged1():
            view2.selectRow(selectionModel1.currentIndex().row())
        def selectionChanged2():
            view1.selectRow(selectionModel2.currentIndex().row())

        selectionModel1 = QItemSelectionModel(model, view1)
        view1.setSelectionModel(selectionModel1)
        self.connect(selectionModel1, SIGNAL("currentChanged(const QModelIndex &, const QModelIndex &)"),selectionChanged1)

        selectionModel2 = QItemSelectionModel(model, view2)
        view2.setSelectionModel(selectionModel2)
        self.connect(selectionModel2, SIGNAL("currentChanged(const QModelIndex &, const QModelIndex &)"),selectionChanged2)

        view2.connect(view1.verticalScrollBar(), SIGNAL("valueChanged(int)"),view2.verticalScrollBar(), SLOT("setValue(int)"))
        view2.connect(view2.verticalScrollBar(), SIGNAL("valueChanged(int)"),view1.verticalScrollBar(), SLOT("setValue(int)"))
        ############################################

if __name__ == "__main__":
    app = QApplication(sys.argv)
    if not createConnection():
        sys.exit(1)

    window = Window()
    window.show()
    sys.exit(app.exec_())
Запись опубликована Python