在QTreeView中导出看到的项目

时间:2015-10-25 18:47:23

标签: pyqt export-to-excel qtreeview qsortfilterproxymodel

我有一个新问题,我正在设计一个应用程序而且我不知道如何将项目导出到excel文件,在我使用带有QSortFilteredProxyModel的QTreeView的代码中,我包含了一个用于在QLineEdit中编写的文本的过滤器,我需要导出过滤后可以看到的所有项目。但是,如何将这些项目添加到列表中,以便将它们导出到Excel文件中?

如果有人能帮助我,我会非常感激。

我的坏英语

# -*- coding: utf-8 -*-
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from dbFacades.profitFacade import ProfitFacade


class MainTabProfits(QGridLayout):
    def __init__(self):
        super(MainTabProfits, self).__init__()
        self.screen = QDesktopWidget().screenGeometry()

        self.profitList = ProfitFacade().allProfit()

        self.addWidget(self.topGroupBox(),0,0,1,4)
        self.addWidget(self.rightGroupBox(), 1, 0,1,4)
        self.addWidget(self.botGroupBox(),2,3)

    # -----------------------------------------------------
    # Se define el grupo superior
    def topGroupBox(self):
        groupBox = QGroupBox()
        groupBox.setTitle("Busqueda")

        self.lineEdit1 = QLineEdit()
        self.lineEdit1.setPlaceholderText("Ingrese una fecha con el siguiente formato ->  dd/mm/aaaa")
        self.lineEdit1.textChanged.connect(self.filterRegExpChanged)
        self.lineEdit1.textChanged.connect(self.setSlashChanged)
        self.lineEdit1.setMaxLength(10)

        leftLayout = QHBoxLayout()
        leftLayout.addWidget(self.lineEdit1)
        leftLayout.stretch(1)

        leftGroupBox = QGroupBox("Fecha Exacta")
        leftGroupBox.setLayout(leftLayout)

        hBoxLayout = QHBoxLayout()
        hBoxLayout.addWidget(leftGroupBox)
        groupBox.setLayout(hBoxLayout)

        return groupBox

    # -----------------------------------------------------
    # Se define el grupo principal(izquierdo) de contenido
    def rightGroupBox(self):
        groupBox = QGroupBox()
        groupBox.setTitle("Control de Finanzas y busqueda")

        self.setHeaders()

        self.proxyModel = QSortFilterProxyModel()
        self.proxyModel.setDynamicSortFilter(True)
        self.proxyModel.setSourceModel(self.model)
        self.proxyModel.setFilterKeyColumn(0)

        self.treeView = QTreeView()
        self.treeView.setRootIsDecorated(False)
        self.treeView.setAlternatingRowColors(True)
        self.treeView.setModel(self.proxyModel)
        self.treeView.setSortingEnabled(True)
        self.treeView.sortByColumn(0, Qt.AscendingOrder)
        self.treeView.resizeColumnToContents(0)
        self.treeView.setEditTriggers(QTreeView.NoEditTriggers)


        vBoxLayout = QVBoxLayout()
        vBoxLayout.addWidget(self.treeView)

        groupBox.setLayout(vBoxLayout)

        self.setProfit()

        return groupBox

    def botGroupBox(self):
        groupBox = QGroupBox()
        groupBox.setTitle("Exportacion")

        self.radioButton0 = QRadioButton("*.PDF")
        self.radioButton0.setIcon(QIcon("icons/pdf.png"))
        self.radioButton0.setIconSize(QSize(self.screen.width() / 50, self.screen.width() / 50))

        self.radioButton1 = QRadioButton("*.Excel")
        self.radioButton1.setIcon(QIcon("icons/excel.png"))
        self.radioButton1.setIconSize(QSize(self.screen.width() / 50, self.screen.width() / 50))

        pushButton = QPushButton(QIcon("icons/export.png"),"Exportar")
        pushButton.released.connect(self.exportList)
        pushButton.setIconSize(QSize(self.screen.width() / 50, self.screen.width() / 50))

        hBoxLayout = QHBoxLayout()
        hBoxLayout.addWidget(self.radioButton0)
        hBoxLayout.addWidget(self.radioButton1)
        hBoxLayout.addWidget(pushButton)
        hBoxLayout.stretch(1)

        groupBox.setLayout(hBoxLayout)
        return groupBox

    # ------------------------------------------------------
    # ZONA METODOS
    # Metodo que define las cabezeras del menu y crea el model
    def setHeaders(self):
        self.model = QStandardItemModel(0, 5, self)
        self.model.setHeaderData(0, Qt.Horizontal, "Fecha")
        self.model.setHeaderData(1, Qt.Horizontal, "Monto")
        self.model.setHeaderData(2, Qt.Horizontal, "Mesa")
        self.model.setHeaderData(3, Qt.Horizontal, "Tipo de Pago")
        self.model.setHeaderData(4, Qt.Horizontal, "Codigo Operacion")

    # Metodo para filtrar por texto escrito en line edit
    def filterRegExpChanged(self):
        regExp = QRegExp(self.lineEdit1.text(), Qt.CaseInsensitive)
        self.proxyModel.setFilterRegExp(regExp)

    # Metodo para el formato de fecha en LineEdit1
    def setSlashChanged(self):
        text = self.lineEdit1.text()
        if(len(text)==2):
            self.lineEdit1.setText(text+"-")
        elif(len(text)==5):
            self.lineEdit1.setText(text+"-")
        self.lineEdit2.setText("")
        self.lineEdit3.setText("")

    # Metodo para el formato de fecha en lineEdit2 y 2
    def inRangeChanged(self):
        text = self.lineEdit2.text()
        text1 = self.lineEdit3.text()
        if(len(text)==2):
            self.lineEdit2.setText(text+"-")
        elif(len(text)==5):
            self.lineEdit2.setText(text+"-")
        elif(len(text1)==2):
            self.lineEdit3.setText(text1+"-")
        elif(len(text1)==5):
            self.lineEdit3.setText(text1+"-")
        self.lineEdit1.setText("")

    # --------------------------------------------------------------------------------
    # Metodo que establece los datos en la lista
    def setProfit(self):
        for profit in self.profitList:
            self.model.insertRow(0)
            self.model.setData(self.model.index(0,0), profit[0])
            self.model.setData(self.model.index(0,1), profit[1])
            self.model.setData(self.model.index(0,2), profit[2])
            self.model.setData(self.model.index(0,3), profit[3])
            self.model.setData(self.model.index(0,4), profit[4])

    # --------------------------------------------------------------------------------
    # Metodo para exportar a excel
    def exportList(self):
        if self.radioButton0.isChecked():
            pass
        if self.radioButton1.isChecked():
            HERE I WANT EXPORT ITEMS FILTERED
            HERE I WANT EXPORT ITEMS FILTERED
            HERE I WANT EXPORT ITEMS FILTERED
            HERE I WANT EXPORT ITEMS FILTERED

1 个答案:

答案 0 :(得分:1)

您可以使用QSortFilterProxyModel中的休眠函数:

rowCount(),columnCount()和index(),然后使用返回的索引来获取数据:

rowCnt = self.proxyModel.rowCount()
colCnt = self.proxyModel.columnCount()

for row in range(0, rowCnt):
  for col in range(0, colCnt):
    modelIndex = self.proxyModel.index(row, col)
    print modelIndex.data().toString()