Форум сайта python.su
Можно ли зафиксировать, например, 1 и 2 колонку
чтобы при прокрутке вправо они остались на экране
Офлайн
по простому нельзя, оказывается
а по сложному - так:
#!/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.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.setSelectionBehavior(QAbstractItemView.SelectRows)
view2 = QTableView(self)
view2.setModel(model)
view2.setSelectionBehavior(QAbstractItemView.SelectRows)
view1.selectRow(0)
view2.selectRow(0)
layout = QHBoxLayout()
layout.addWidget(view1)
layout.addWidget(view2)
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_())
Офлайн