16.3 表格里显示自定义控件

进度条控制的例子:

#python 3.4
import sys
from PySide.QtCore import *
from PySide.QtGui import *
 
class ExampleApp(QDialog):
    ''' 创建对话框的例子'''
    def __init__(self):
        # 创建Qt应用程序实例
        self.qt_app = QApplication(sys.argv)
       
        # 调用基类构造函数
        QDialog.__init__(self, None)
 
        # 设置窗口
        self.setWindowTitle('PySide Example')
        self.setMinimumSize(300, 200)
 
        #添加一个垂直布局组件
        self.vbox = QVBoxLayout()
        #添加一个组合框
        self.greeting = QComboBox(self)
        self.vbox.addWidget(self.greeting)
        #往组合框里添加内容
        self.greetings = ['测试', 'blog', 'blog.csdn.net/caimouse']
        list(map(self.greeting.addItem, self.greetings))
 
        #添加一行输入框
        self.recipient = QLineEdit('world', self)
        self.vbox.addWidget(self.recipient)
        #强制按钮在布局最底部
        self.vbox.addStretch(100)
 
        #添加按钮
        self.go_button = QPushButton('&Go')
        #绑定按钮的响应函数
        self.go_button.clicked.connect(self.print_out)
        self.vbox.addWidget(self.go_button)
 
        #添加进度条
        self.progress = QProgressBar()
        self.vbox.addWidget(self.progress)
        
        #设置窗口布局
        self.setLayout(self.vbox)
 
        #定时器
        self.timer = QTimer(self)
        self.connect(self.timer, SIGNAL("timeout()"), self.update)
        self.timer.start(1000)
        self.timecount = 0                
        
    def run(self):
        ''' 运行程序,并显示主窗口'''
        self.show()
        self.qt_app.exec_()
        self.timer.stop()        
    def print_out(self):
        print(self.recipient.displayText())
    def  update(self):
        print('timer')
        self.recipient.setText('{0}'.format(self.timecount))
        self.timecount += 1
        self.progress.setValue(self.timecount)
        if self.timecount == 100:
            self.timecount = 0
app = ExampleApp()
app.run()


结果输出如下:

在这个例子里,创建了进度条QProgressBar对象,通过进度条函数setValue()更新进度,默认进度条的值是从0100,每增加1就是百分之一。

16.3 表格里显示自定义控件

在表格视类里添加自定义列表框的例子:

#python 3.4
import sys
from PySide.QtCore import *
from PySide.QtGui import *
 
class DBComboBoxDelegate(QItemDelegate):
 
  def __init__(self, comboModel, parent=None):
    QItemDelegate.__init__(self, parent)
    self.comboModel = comboModel
 
  def __createComboView(self, parent):
    view = QTableView(parent)
    view.setModel(self.comboModel)
    view.setAutoScroll(False)
    view.setHorizontalScrollBarPolicy(Qt.ScrollBarAsNeeded)
    view.setSelectionMode(QAbstractItemView.SingleSelection)
    view.setSelectionBehavior(QAbstractItemView.SelectRows)
    view.resizeColumnsToContents()
    view.resizeRowsToContents()
    view.setMinimumWidth(view.horizontalHeader().length())
    return view
 
  def createEditor(self, parent, option, index):
    combo = QComboBox(parent)
    #!! The important part: First set the model, then the view on the combo box
    combo.setModel(self.comboModel)
    #combo.setModelColumn(1)
    combo.setView(self.__createComboView(parent))
    return combo
 
  def setEditorData(self, editor, index):
    value = index.model().data(index, Qt.EditRole)
    editor.setCurrentIndex(editor.findText(value))
 
  def setModelData(self, editor, model, index):
    if editor.currentIndex() >= 0:
      realidx = editor.model().index(editor.currentIndex(), 0) #确保取第一列的值
      value = editor.model().data(realidx)
      model.setData(index, value, Qt.EditRole)
 
###############################################################################
 
if __name__ == '__main__':
  app = QApplication(sys.argv)
  table = QTableView()
 
  comboModel = QStandardItemModel(4, 2, table)
  comboModel.setHorizontalHeaderLabels(['Name', 'Description'])
  comboModel.setData(comboModel.index(0, 0, QModelIndex()), '树袋熊')
  comboModel.setData(comboModel.index(0, 1, QModelIndex()), '生活在树上的熊')
  comboModel.setData(comboModel.index(1, 0, QModelIndex()), '松鼠')
  comboModel.setData(comboModel.index(1, 1, QModelIndex()), '可爱的松树精灵')
  comboModel.setData(comboModel.index(2, 0, QModelIndex()), '大眼猴')
  comboModel.setData(comboModel.index(2, 1, QModelIndex()), '这猴眼睛真大')
  comboModel.setData(comboModel.index(3, 0, QModelIndex()), '猫头鹰')
  comboModel.setData(comboModel.index(3, 1, QModelIndex()), '夜的精灵正站在树枝上')
 
  model = QStandardItemModel(2, 3, table)
  model.setHorizontalHeaderLabels(['Name', 'Height', 'Weight'])
  model.setData(model.index(0, 0, QModelIndex()), '松鼠')
  model.setData(model.index(0, 1, QModelIndex()), '80cm')
  model.setData(model.index(0, 2, QModelIndex()), '12Kg')
 
  table.setModel(model)
  table.setItemDelegateForColumn(0, DBComboBoxDelegate(comboModel, table))
  table.horizontalHeader().setStretchLastSection(True)
  table.setGeometry(80, 20, 400, 300)
  table.setWindowTitle('表格和组合框演示')
  table.show()
 
  sys.exit(app.exec_())


结果输出如下:

 

 

你可能感兴趣的:(python,qt,PyQt,milang)