PyQt5学习笔记6_QTableView中嵌入按钮
来源:互联网 发布:火烧赤壁的网络意思 编辑:程序博客网 时间:2024/05/29 03:28
开发环境:PyQt 5.5.1 Python 3.4.4
参考Button Delegate For QTableViews给出的例程,实现在一个单元格中嵌入多个按钮,代码如下:
import sysfrom PyQt5.QtCore import (Qt, QAbstractTableModel, QModelIndex, QVariant)from PyQt5.QtWidgets import (QApplication, QHBoxLayout, QItemDelegate, QPushButton, QTableView, QWidget)class MyModel(QAbstractTableModel): def __init__(self, parent=None): super(MyModel, self).__init__(parent) def rowCount(self, QModelIndex): return 4 def columnCount(self, QModelIndex): return 3 def data(self, index, role): row = index.row() col = index.column() if role == Qt.DisplayRole: return 'Row %d, Column %d' % (row + 1, col + 1) return QVariant()class MyButtonDelegate(QItemDelegate): def __init__(self, parent=None): super(MyButtonDelegate, self).__init__(parent) def paint(self, painter, option, index): if not self.parent().indexWidget(index): button_read = QPushButton( self.tr('读'), self.parent(), clicked=self.parent().cellButtonClicked ) button_write = QPushButton( self.tr('写'), self.parent(), clicked=self.parent().cellButtonClicked ) button_read.index = [index.row(), index.column()] button_write.index = [index.row(), index.column()] h_box_layout = QHBoxLayout() h_box_layout.addWidget(button_read) h_box_layout.addWidget(button_write) h_box_layout.setContentsMargins(0, 0, 0, 0) h_box_layout.setAlignment(Qt.AlignCenter) widget = QWidget() widget.setLayout(h_box_layout) self.parent().setIndexWidget( index, widget )class MyTableView(QTableView): def __init__(self, parent=None): super(MyTableView, self).__init__(parent) self.setItemDelegateForColumn(0, MyButtonDelegate(self)) def cellButtonClicked(self): print("Cell Button Clicked", self.sender().index)if __name__ == '__main__': a = QApplication(sys.argv) tableView = MyTableView() myModel = MyModel() tableView.setModel(myModel) tableView.show() a.exec_()
效果如下:
阅读全文
0 0
- PyQt5学习笔记6_QTableView中嵌入按钮
- PyQt5学习笔记5_QTableView中嵌入复选框
- 将win32窗口嵌入到pyqt5中
- PyQt5 学习笔记
- PYQT5学习笔记1
- PyQt5笔记(02) -- 按钮点击事件
- PyQt5学习笔记(1)
- PyQt5学习教程6:在窗体中显示状态栏
- PyQt5学习笔记01----第一个窗口
- PyQt5学习笔记07----通用对话框QMessageBox
- PyQt5学习笔记10----QColorDialog与QFontDialog
- PyQt5学习笔记01----第一个窗口
- PyQt5学习笔记2_仪表盘示例
- PyQt5学习笔记3_PyInstaller生成exe
- PyQt5学习笔记01----第一个窗口
- python学习笔记4:PyQt5的安装
- PyQt5学习笔记01----第一个窗口
- pyqt5学习笔记——刷新页面
- 最大连续子序列之和
- springboot支持jsp页面
- cnpm不是内部命令的解决方案:配置环境变量
- 第四次小组讨论 (代码规范)
- 大数据环境搭建
- PyQt5学习笔记6_QTableView中嵌入按钮
- Hello World!
- hdu5319 Painter(模拟题)
- 省选专练ZJOI2004书架
- redis的安装及jedis的jar包下载地址
- 三元组,二元组,排列组合
- NO1 拷贝构造函数
- [LeetCode] 121. Best Time to Buy and Sell Stock ❤
- [luogu]P1631序列合并-堆排的套路运用