本文整理汇总了Python中PyQt5.QtWidgets.QTableView.setSelectionModel方法的典型用法代码示例。如果您正苦于以下问题:Python QTableView.setSelectionModel方法的具体用法?Python QTableView.setSelectionModel怎么用?Python QTableView.setSelectionModel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtWidgets.QTableView
的用法示例。
在下文中一共展示了QTableView.setSelectionModel方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from PyQt5.QtWidgets import QTableView [as 别名]
# 或者: from PyQt5.QtWidgets.QTableView import setSelectionModel [as 别名]
def main(args):
app = QApplication(args)
page = QSplitter()
data = Model(1000, 10, page)
selections = QItemSelectionModel(data)
table = QTableView()
table.setModel(data)
table.setSelectionModel(selections)
table.horizontalHeader().setSectionsMovable(True)
table.verticalHeader().setSectionsMovable(True)
# Set StaticContents to enable minimal repaints on resizes.
table.viewport().setAttribute(Qt.WA_StaticContents)
page.addWidget(table)
tree = QTreeView()
tree.setModel(data)
tree.setSelectionModel(selections)
tree.setUniformRowHeights(True)
tree.header().setStretchLastSection(False)
tree.viewport().setAttribute(Qt.WA_StaticContents)
# Disable the focus rect to get minimal repaints when scrolling on Mac.
tree.setAttribute(Qt.WA_MacShowFocusRect, False)
page.addWidget(tree)
list = QListView()
list.setModel(data)
list.setSelectionModel(selections)
list.setViewMode(QListView.IconMode)
list.setSelectionMode(QAbstractItemView.ExtendedSelection)
list.setAlternatingRowColors(False)
list.viewport().setAttribute(Qt.WA_StaticContents)
list.setAttribute(Qt.WA_MacShowFocusRect, False)
page.addWidget(list)
page.setWindowIcon(QIcon(images_dir + '/interview.png'))
page.setWindowTitle("Interview")
page.show()
return app.exec_()
示例2: setupViews
# 需要导入模块: from PyQt5.QtWidgets import QTableView [as 别名]
# 或者: from PyQt5.QtWidgets.QTableView import setSelectionModel [as 别名]
def setupViews(self):
splitter = QSplitter()
table = QTableView()
self.pieChart = PieView()
splitter.addWidget(table)
splitter.addWidget(self.pieChart)
splitter.setStretchFactor(0, 0)
splitter.setStretchFactor(1, 1)
table.setModel(self.model)
self.pieChart.setModel(self.model)
self.selectionModel = QItemSelectionModel(self.model)
table.setSelectionModel(self.selectionModel)
self.pieChart.setSelectionModel(self.selectionModel)
table.horizontalHeader().setStretchLastSection(True)
self.setCentralWidget(splitter)
示例3: MainWindow
# 需要导入模块: from PyQt5.QtWidgets import QTableView [as 别名]
# 或者: from PyQt5.QtWidgets.QTableView import setSelectionModel [as 别名]
class MainWindow(QMainWindow):
def __init__(self,system):
super(MainWindow, self).__init__()
self.system = system
fileMenu = QMenu("&Menu", self)
dbinitAction = fileMenu.addAction("사용자 정보 초기화")
quitAction = fileMenu.addAction("E&xit")
quitAction.setShortcut("Ctrl+Q")
self.menuBar().addMenu(fileMenu)
self.statusBar()
dbinitAction.triggered.connect(self.ClickAction_dbinit)
quitAction.triggered.connect(QApplication.instance().quit)
self.setupModel()
self.setupViews()
self.readDB()
self.setWindowTitle("PyStudy 학습진도창")
self.resize(870, 550)
def setupModel(self):
self.model = QStandardItemModel(3, 2, self)
self.model.setHeaderData(0, Qt.Horizontal, "목록")
self.model.setHeaderData(1, Qt.Horizontal, "읽기여부")
self.model2 = QStandardItemModel(3, 2, self)
self.model2.setHeaderData(0, Qt.Horizontal, "학습여부")
self.model2.setHeaderData(1, Qt.Horizontal, "개수")
def setupViews(self):
splitter = QSplitter()
self.table = QTableView()
self.pieChart = PieView()
splitter.addWidget(self.pieChart)
splitter.addWidget(self.table)
splitter.setStretchFactor(0, 0)
splitter.setStretchFactor(1, 0)
self.table.setModel(self.model)
self.pieChart.setModel(self.model2)
self.selectionModel = QItemSelectionModel(self.model2)
self.table.setSelectionModel(self.selectionModel)
#self.pieChart.setSelectionModel(self.selectionModel)
#table.setColumnWidth(0,100)
self.setCentralWidget(splitter)
self.table.doubleClicked.connect(self.ClickAction_table)
def readDB(self):
con = sqlite3.connect("mystudy.db")
cur = con.cursor()
cur.execute("select subject, readcheck from study;")
self.model.removeRows(0, self.model.rowCount(QModelIndex()),
QModelIndex())
self.model2.removeRows(0, self.model2.rowCount(QModelIndex()),
QModelIndex())
row = 0
for line in cur:
if line[1] ==1:
result = "○"
else:
result = "X"
self.model.insertRows(row, 1, QModelIndex())
self.model.setData(self.model.index(row, 0, QModelIndex()), line[0])
self.model.setData(self.model.index(row, 1, QModelIndex()),result)
self.model.setData(self.model.index(row, 1, QModelIndex()), QVariant(Qt.AlignCenter),Qt.TextAlignmentRole)
row += 1
cur.execute("select count() from study ;")
for line in cur:
self.studyTotal =line[0]
cur.execute("select count() from study where readcheck=1;")
for line in cur:
self.studyRead =line[0]
#print("총 개수 " ,self.studyTotal ," 학습한개수", self.studyRead )
con.close()
row=0
self.model2.insertRows(row, 1, QModelIndex())
self.model2.setData(self.model2.index(row, 0, QModelIndex()),"학습")
self.model2.setData(self.model2.index(row, 1, QModelIndex()), float(self.studyRead))
self.model2.setData(self.model2.index(row, 0, QModelIndex()), QColor("#99e600"), Qt.DecorationRole)
row=1
self.model2.insertRows(row, 1, QModelIndex())
self.model2.setData(self.model2.index(row, 0, QModelIndex()),"미학습")
self.model2.setData(self.model2.index(row, 1, QModelIndex()), float(self.studyTotal-self.studyRead))
self.model2.setData(self.model2.index(row, 0, QModelIndex()), QColor("#8080b3"), Qt.DecorationRole)
self.table.setSelectionBehavior(QAbstractItemView.SelectRows)
self.table.setSelectionMode(QAbstractItemView.SingleSelection)
self.table.setDragEnabled(False)
#.........这里部分代码省略.........
示例4: ListView
# 需要导入模块: from PyQt5.QtWidgets import QTableView [as 别名]
# 或者: from PyQt5.QtWidgets.QTableView import setSelectionModel [as 别名]
class ListView(QStackedWidget):
PAGE_EMPTY = 0
PAGE_LISTVIEW = 1
def __init__(self, parent = None):
super(ListView, self).__init__(parent=parent)
self.emptyMessage = QLabel("no elements defined yet")
self.emptyMessage.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter )
self.emptyMessage.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.addWidget(self.emptyMessage)
self._table = QTableView()
self.addWidget(self._table)
self._table.clicked.connect(self.tableViewCellClicked)
self._table.doubleClicked.connect(self.tableViewCellDoubleClicked)
self._table.verticalHeader().sectionMoved.connect(self.rowMovedTest)
self._table.setShowGrid(False)
def resetEmptyMessage(self,pystring):
self.emptyMessage.setText(pystring)
def tableViewCellClicked(self, modelIndex):
'''
Reimplemt this function to get interaction when double click
:param modelIndex:
'''
# if (modelIndex.column() == self.model.ColumnID.Delete and
# not self._table.model().flags(modelIndex) == Qt.NoItemFlags):
# self._table.model().removeRow(modelIndex.row())
#
def tableViewCellDoubleClicked(self, modelIndex):
'''
Reimplement this function to get interaction when single click
:param modelIndex:
'''
# if modelIndex.column() == self.model.ColumnID.Color:
# self._colorDialog.setBrushColor(self._table.model()[modelIndex.row()].brushColor())
# self._colorDialog.setPmapColor (self._table.model()[modelIndex.row()].pmapColor())
# self._colorDialog.exec_()
# #print "brush color = {}".format(self._colorDialog.brushColor().name())
# #print "pmap color = {}".format(self._colorDialog.pmapColor().name())
# self._table.model().setData(modelIndex, (self._colorDialog.brushColor(),
# self._colorDialog.pmapColor ()))
def rowMovedTest(self, logicalIndex, oldVisualIndex, newVisualIndex):
logger.debug( "{} {} {}".format(logicalIndex, oldVisualIndex, newVisualIndex) )
def _setListViewLook(self):
table = self._table
#table.setDragEnabled(True)
table.setAcceptDrops(True)
table.setFocusPolicy(Qt.NoFocus)
table.setShowGrid(False)
table.horizontalHeader().hide()
table.verticalHeader().hide()
#table.horizontalHeader().setSectionResizeMode(1, QHeaderView.Stretch)
table.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
table.setSelectionMode(QAbstractItemView.SingleSelection)
table.setSelectionBehavior(QAbstractItemView.SelectRows)
def selectRow(self, *args, **kwargs):
self._table.selectRow(*args, **kwargs)
def _onRowsChanged(self, parent, start, end):
model = self._table.model()
if model and model.rowCount() > 0:
self.setCurrentIndex(self.PAGE_LISTVIEW)
else:
self.setCurrentIndex(self.PAGE_EMPTY)
if self.parent()!=None: self.parent().updateGeometry()
def setModel(self, model):
QTableView.setModel(self._table, model)
self._table.setSelectionModel(model._selectionModel)
if model.rowCount() > 0:
self.setCurrentIndex(self.PAGE_LISTVIEW)
else:
self.setCurrentIndex(self.PAGE_EMPTY)
model.rowsInserted.connect(self._onRowsChanged)
model.rowsRemoved.connect(self._onRowsChanged)
self.model=model
self._setListViewLook()
@property
def allowDelete(self):
return not self._table.isColumnHidden(self.model.ColumnID.Delete)
@allowDelete.setter
def allowDelete(self, allow):
self._table.setColumnHidden(self.model.ColumnID.Delete, not allow)
#.........这里部分代码省略.........
示例5: FreezeTableWidget
# 需要导入模块: from PyQt5.QtWidgets import QTableView [as 别名]
# 或者: from PyQt5.QtWidgets.QTableView import setSelectionModel [as 别名]
class FreezeTableWidget(QTableView):
def __init__(self, model):
super(FreezeTableWidget, self).__init__()
self.setModel(model)
self.frozenTableView = QTableView(self)
self.init()
self.horizontalHeader().sectionResized.connect(self.updateSectionWidth)
self.verticalHeader().sectionResized.connect(self.updateSectionHeight)
self.frozenTableView.verticalScrollBar().valueChanged.connect(
self.verticalScrollBar().setValue)
self.verticalScrollBar().valueChanged.connect(
self.frozenTableView.verticalScrollBar().setValue)
def init(self):
self.frozenTableView.setModel(self.model())
self.frozenTableView.setFocusPolicy(Qt.NoFocus)
self.frozenTableView.verticalHeader().hide()
self.frozenTableView.horizontalHeader().setSectionResizeMode(
QHeaderView.Fixed)
self.viewport().stackUnder(self.frozenTableView)
self.frozenTableView.setStyleSheet('''
QTableView { border: none;
background-color: #8EDE21;
selection-background-color: #999;
}''') # for demo purposes
self.frozenTableView.setSelectionModel(self.selectionModel())
for col in range(1, self.model().columnCount()):
self.frozenTableView.setColumnHidden(col, True)
self.frozenTableView.setColumnWidth(0, self.columnWidth(0))
self.frozenTableView.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.frozenTableView.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.frozenTableView.show()
self.updateFrozenTableGeometry()
self.setHorizontalScrollMode(self.ScrollPerPixel)
self.setVerticalScrollMode(self.ScrollPerPixel)
self.frozenTableView.setVerticalScrollMode(self.ScrollPerPixel)
def updateSectionWidth(self, logicalIndex, oldSize, newSize):
if self.logicalIndex == 0:
self.frozenTableView.setColumnWidth(0, newSize)
self.updateFrozenTableGeometry()
def updateSectionHeight(self, logicalIndex, oldSize, newSize):
self.frozenTableView.setRowHeight(logicalIndex, newSize)
def resizeEvent(self, event):
super(FreezeTableWidget, self).resizeEvent(event)
self.updateFrozenTableGeometry()
def moveCursor(self, cursorAction, modifiers):
current = super(FreezeTableWidget, self).moveCursor(cursorAction, modifiers)
if (cursorAction == self.MoveLeft and
self.current.column() > 0 and
self.visualRect(current).topLeft().x() <
self.frozenTableView.columnWidth(0)):
newValue = (self.horizontalScrollBar().value() +
self.visualRect(current).topLeft().x() -
self.frozenTableView.columnWidth(0))
self.horizontalScrollBar().setValue(newValue)
return current
def scrollTo(self, index, hint):
if index.column() > 0:
super(FreezeTableWidget, self).scrollTo(index, hint)
def updateFrozenTableGeometry(self):
self.frozenTableView.setGeometry(
self.verticalHeader().width() + self.frameWidth(),
self.frameWidth(), self.columnWidth(0),
self.viewport().height() + self.horizontalHeader().height())
示例6: QApplication
# 需要导入模块: from PyQt5.QtWidgets import QTableView [as 别名]
# 或者: from PyQt5.QtWidgets.QTableView import setSelectionModel [as 别名]
if __name__ == '__main__':
app = QApplication(sys.argv)
window = QWidget()
table_view1 = QTableView()
table_view2 = QTableView()
my_model = MyModel(None)
table_view1.setModel(my_model)
table_view2.setModel(my_model)
# http://doc.qt.io/qt-5/model-view-programming.html#sharing-selections-among-views
table_view2.setSelectionModel(table_view1.selectionModel()) # <- Share the SelectionModel
# Set the layout
vbox = QVBoxLayout()
vbox.addWidget(table_view1)
vbox.addWidget(table_view2)
window.setLayout(vbox)
# Show
window.show()
# The mainloop of the application. The event handling starts from this point.
# The exec_() method has an underscore. It is because the exec is a Python keyword. And thus, exec_() was used instead.