源码之家:www.mycodes.net

python

QT_QTableWidget

 

PyQt4 QTableWidget 使用教程

self.table=QTableWidget() #实例化

self.table.setColumnCount(4) #设置列数

self.table.setRowCount(6) #设置行数

self.table.setWhatsThis("mantou") ###

self.table.setEditTriggers(QTableWidget.NoEditTriggers) #设置为不能编辑单元格

  #set Column tab title

   self.table.setHorizontalHeaderLabels(list(range(5,10)))

   self.table.setVerticalHeaderLabels(["a","d"] ) #设置 行title

self.table.setAlternatingRowColors(bool) #设置交替行颜色

 

   for i in range(0,5):

    for x in range(0,7):

         item=QTableWidgetItem(str(i+x)) 实例一个item对象

         item.setTextAlignment(Qt.AlignLeft |Qt.AlignCenter) 设置对齐方式

        item.setBackgroundColor(Qt.green) 设置背景

        self.table.setItem(x,i,item) 添加Item


 

 

  •         self.MyTable = QTableWidget(4,3)  
  •         self.MyTable.setHorizontalHeaderLabels(['姓名','身高','体重'])  
  •           
  •         newItem = QTableWidgetItem("松鼠")  
  •         self.MyTable.setItem(00, newItem)  
  •           
  •         newItem = QTableWidgetItem("10cm")  
  •         self.MyTable.setItem(01, newItem)  
  •           
  •         newItem = QTableWidgetItem("60g")  
  •         self.MyTable.setItem(02, newItem)  

 

 

 


  1. self.MyCombo = QComboBox()  
  2. self.MyCombo.addItem("√")  
  3. self.MyCombo.addItem("×")           
  4. self.MyTable.setCellWidget(1,0,self.MyCombo) 

 


  1. textFont = QFont("song"12, QFont.Bold)  
  2.           
  3. newItem = QTableWidgetItem("松鼠")  
  4. #newItem.setBackgroundColor(QColor(0,60,10))  
  5. #newItem.setTextColor(QColor(200,111,100))   
  6. newItem.setFont(textFont)  
  7. self.MyTable.setItem(00, newItem) 

 


QTableWidget.clear(self) 清楚所有表项及表头

QTableWidget.clearContents(self) 只清楚表项,不清楚表头。

QTableWidget.insertColumn(self, int column) 在某一列插入新的一列。

QTableWidget.insertRow(self, int row)在某一行插入新的一行。

QTableWidget.removeColumn(self, int column) 移除column列及其内容。

QTableWidget.removeRow(self, int row)移除第row行及其内容。

 


 TableView

 

        table_model = MyTableModel(self, dataList, header)
        table_view = QTableView()
        # bind cell click to a method reference
        table_view.clicked.connect(self.showSelection)
 
        table_view.setModel(table_model)
        # enable sorting
        table_view.setSortingEnabled(True)
 
        layout = QVBoxLayout(self)
        layout.addWidget(table_view)
        self.setLayout(layout)

 

header = ['First Name', 'Last Name', 'Age', 'Weight']
# a list of (fname, lname, age, weight) tuples
dataList = [
('Ben', 'Dover', 36, 127),
('Foster', 'Krampf', 27, 234),
('Barry', 'Chaurus', 19, 315),
('Sede', 'Anowski', 59, 147),
('Carolus', 'Gabel', 94, 102),
('Michel', 'Zittus', 21, 175),
(QCheckBox(u"选择"), 'Waters', 31, 114)
]
 

from PyQt4.QtCore import *
from PyQt4.QtGui import *
 

class MyTableModel(QAbstractTableModel):
    """
    keep the method names
    they are an integral part of the model
    """
    def __init__(self, parent, mylist, header, *args):
        QAbstractTableModel.__init__(self, parent, *args)
        self.mylist = mylist
        self.header = header
        self.timer = QTimer()
        self.change_flag = True
        #self.timer.timeout.connect(self.updateModel)
        #self.timer.start(1000)
        

    def rowCount(self, parent):
        return len(self.mylist)
 
    def columnCount(self, parent):
        return len(self.mylist[0])
 
    def data(self, index, role):
        if not index.isValid():
            return None
        if (index.column() == 0):
            value = self.mylist[index.row()][index.column()].text()
        else:
            value = self.mylist[index.row()][index.column()]
        if role == Qt.EditRole:
            return value
        elif role == Qt.DisplayRole:
            return value
        elif role == Qt.CheckStateRole:
            if index.column() == 0:               
                if self.mylist[index.row()][index.column()].isChecked():
                    return Qt.Checked
                else:
                    return Qt.Unchecked
 
    def headerData(self, col, orientation, role):
        if orientation == Qt.Horizontal and role == Qt.DisplayRole:
            return self.header[col]
        return None
 

 
    def flags(self, index):
        if not index.isValid():
            return None
        if index.column() == 0:
            # return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable
            return Qt.ItemIsEnabled | Qt.ItemIsSelectable | Qt.ItemIsUserCheckable
        else:
            return Qt.ItemIsEnabled | Qt.ItemIsSelectable
 
    def setData(self, index, value, role):
        if not index.isValid():
            return False

        if role == Qt.CheckStateRole and index.column() == 0:
            if value == Qt.Checked:
                self.mylist[index.row()][index.column()].setChecked(True)
                #self.mylist[index.row()][index.column()].setText(u"开")
            else:
                self.mylist[index.row()][index.column()].setChecked(False)
                #self.mylist[index.row()][index.column()].setText(u"关")

        return True
 
 ==============
table_model = MyTableModel(self,  dataList, header)
self.tableView.setModel(table_model)
 

 
model.rowCount()#返回总行数,model.columnCount()#返回总列数

model.headerData(i,Qt.Horizontal)#返回第i列表头内容

model.data(model.index(row, column))#返回第row行,第column列内容

model.removeRow(row)#移除某行

model.insertRow(row)#插入新行


#下面调用的时候是选中表格某个位置

table.currentIndex().row()#返回表格被选中行 数值
table.currentIndex().column()#返回表格被选中列 数值
 

 

 

 

 

 

 

 

 

 

联系我们Contact

Copyright © 2013 - 2024 mycodes.net All rights reserved.

公司位置