免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4056 | 回复: 1

pyqt 界面QTreeView 鼠标应用 [复制链接]

论坛徽章:
0
发表于 2013-12-30 16:56 |显示全部楼层

我想加入双击一条数据弹出一个窗口,在窗口中显示数据的索引

*************************************************************

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This is only needed for Python v2 but is harmless for Python v3.
import sip
sip.setapi('QVariant', 2)

from PyQt4 import QtCore, QtGui
import codebase

SUBJECT, SENDER, DATE, KAIJIANGLI = range(4)


              
# Work around the fact that QSortFilterProxyModel always filters datetime
# values in QtCore.Qt.ISODate formeat, but the tree views display using
# QtCore.Qt.DefaultLocaleShortDate format.
class SortFilterProxyModel(QtGui.QSortFilterProxyModel):
    def filterAcceptsRow(self, sourceRow, sourceParent):
        # Do we filter for the date column?
        if self.filterKeyColumn() == DATE:
            # Fetch datetime value.
            index = self.sourceModel().index(sourceRow, DATE, sourceParent)
            data = self.sourceModel().data(index)

            # Return, if regExp match in displayed format.
            return (self.filterRegExp().indexIn(data.toString(QtCore.Qt.DefaultLocaleShortDate)) >= 0)

        # Not our business.
        return super(SortFilterProxyModel, self).filterAcceptsRow(sourceRow, sourceParent)


class Window(QtGui.QWidget):
    def __init__(self):
        super(Window, self).__init__()

        self.proxyModel = SortFilterProxyModel()
        self.proxyModel.setDynamicSortFilter(True)

        self.proxyGroupBox = QtGui.QGroupBox(u" 两码定位 ")


        self.proxyView = QtGui.QTreeView()
        self.proxyView.setRootIsDecorated(False)
        self.proxyView.setAlternatingRowColors(True)
        self.proxyView.setModel(self.proxyModel)
        self.proxyView.setSortingEnabled(True)

      
        proxyLayout = QtGui.QGridLayout()
        proxyLayout.addWidget(self.proxyView, 0, 0, 1, 3)
      
        
      
        self.proxyGroupBox.setLayout(proxyLayout)

        mainLayout = QtGui.QVBoxLayout()
      
        mainLayout.addWidget(self.proxyGroupBox)
        self.setLayout(mainLayout)

        self.setWindowTitle(u"两码定位 - 0369")
        self.resize(500, 450)

        self.proxyView.sortByColumn(SENDER, QtCore.Qt.AscendingOrder)
        
  

    def setSourceModel(self, model):
        self.proxyModel.setSourceModel(model)
        

    def filterRegExpChanged(self):
        syntax = QtCore.QRegExp.PatternSyntax(syntax_nr)
        if self.filterCaseSensitivityCheckBox.isChecked():
            caseSensitivity = QtCore.Qt.CaseSensitive
        else:
            caseSensitivity = QtCore.Qt.CaseInsensitive

        

def addMail(model, subject, sender, date, kaijiangli):
    model.insertRow(0)
    model.setData(model.index(0, SUBJECT), subject)
    model.setData(model.index(0, SENDER), sender)
    model.setData(model.index(0, DATE), date)
    model.setData(model.index(0, KAIJIANGLI), kaijiangli)


def createMailModel(parent):
    model = QtGui.QStandardItemModel(0, 4, parent)

    model.setHeaderData(SUBJECT, QtCore.Qt.Horizontal, "Subject")
    model.setHeaderData(SENDER, QtCore.Qt.Horizontal, u"title1")
    model.setHeaderData(DATE, QtCore.Qt.Horizontal, u"title2")
    model.setHeaderData(KAIJIANGLI, QtCore.Qt.Horizontal, u"title3")

   
    addMail(model,"1","2","3","4")
    addMail(model,"1","2","3","4")
    addMail(model,"1","2","3","4")
    addMail(model,"1","2","3","4")
    addMail(model,"1","2","3","4")

    return model


if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setSourceModel(createMailModel(window))
    window.show()
    sys.exit(app.exec_())

论坛徽章:
0
发表于 2013-12-31 09:59 |显示全部楼层
没人看呀。顶一下
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

DTCC2020中国数据库技术大会 限时8.5折

【架构革新 高效可控】2020年8月17日~19日第十一届中国数据库技术大会将在北京隆重召开。

大会设置2大主会场,20+技术专场,将邀请超百位行业专家,重点围绕数据架构、AI与大数据、传统企业数据库实践和国产开源数据库等内容展开分享和探讨,为广大数据领域从业人士提供一场年度盛会和交流平台。

http://dtcc.it168.com


大会官网>>
  

北京盛拓优讯信息技术有限公司. 版权所有 16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122
中国互联网协会会员  联系我们:huangweiwei@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP