转载:QTableView中嵌入可视化组件
出處:http://qimo601.iteye.com/blog/1538364
?
?
?
QTableView中嵌入可視化組件方法有四種:
?
?
? ? ? ? 第一種不能之前顯示,必須雙擊/選中后才能顯示,不適用。
? ? ? ? 第二種比較簡單,通常用這種方法。
? ? ? ? 第三種只適合靜態顯示靜態數據用
? ? ? ? 第四種比較適合擴展,它除了可以嵌入復選框,還可以通過paint()繪制其它控件,圖片等自定義風格。
第一種方法是:編輯委托法
這種方法直接利用委托中重載createEditor(),激活QCheckBox,這個缺點是必須雙擊/選中,才能顯示CheckBox控件。一般不滿足我們實際中的直接顯示的需要。可以參考Qt中的QSpinBoxDelegate例子。
?
第二種方法是:設置QAbstractTableModel的flags()函數法。
?
第三種方法是:用QTableView中的方法void setIndexWidget(const QModelIndex &index, QWidget *widget)來設置QCheckBox。
?
代碼:setIndexWidget(index, new QTextEdit);
Qt Assistant 寫道 The items shown in a table view, like those in the other item views, are rendered and edited using standard delegates. However, for some tasks it is sometimes useful to be able to insert widgets in a table instead. Widgets are set for particular indexes with the setIndexWidget() function, and later retrieved with indexWidget().?
? ??Qt Assistant?寫道關于setIndexWidget()
Sets the given widget on the item at the given index, passing the ownership of the widget to the viewport.If index is invalid (e.g., if you pass the root index), this function will do nothing.
The given widget's autoFillBackground property must be set to true, otherwise the widget's background will be transparent, showing both the model data and the item at the given index.
If index widget A is replaced with index widget B, index widget A will be deleted. For example, in the code snippet below, the QLineEdit object will be deleted.
setIndexWidget(index, new QLineEdit);
...
setIndexWidget(index, new QTextEdit);
This function should only be used to display static content within the visible area corresponding to an item of data. If you want to display custom dynamic content or implement a custom editor widget, subclass QItemDelegate instead.
?
此功能只應該用來顯示可視區域內對應一個數據項的靜態內容。如果你想顯示自定義的動態內容或執行自定義編輯器部件,子類化QItemDelegate代替。
就是說這個只適合用來做不變數據的顯示,而不適合做一些會產生插入,更新,刪除這樣的操作的數據顯示.
?
?
?
此處向下為 博主 自己 原載:
在tableview中添加 QCheckBox:
?
class C_Delegate(QtWidgets.QStyledItemDelegate):def createEditor(self, parent, option, index):columnIndex = index.column()if columnIndex == 5:editor = QtWidgets.QCheckBox('是', parent)editor.setAutoFillBackground(True) return editorelse:return super(C_Delegate, self).createEditor(parent, option, index)# 計算 check_box的位置 和 大小 def checkBoxRect(self, option):but_style = QtWidgets.QStyleOptionButton()check_box_rect =QtWidgets. QApplication.style().subElementRect(QtWidgets.QStyle.SE_CheckBoxIndicator, but_style)check_box_point = QtCore.QPoint(option.rect.x() +option.rect.width() / 2 -check_box_rect.width() / 2,option.rect.y() +option.rect.height() / 2 -check_box_rect.height() / 2);return QtCore.QRect(check_box_point, check_box_rect.size())def paint(self, painter, option, index):columnIndex = index.column()if columnIndex == 5:#獲取值 checked = index.model().data(index, QtCore.Qt.DisplayRole)#按鈕的風格選項 checkBoxOption = QtWidgets.QStyleOptionButton()checkBoxOption.state |= QtWidgets.QStyle.State_Enabled; #根據值判斷是否選中 if checked > 0 : checkBoxOption.state |= QtWidgets.QStyle.State_On else : checkBoxOption.state |= QtWidgets.QStyle.State_Off #返回QCheckBox幾何形狀 checkBoxOption.rect = self.checkBoxRect(option)#繪制QCheckBox QtWidgets.QApplication.style().drawControl(QtWidgets.QStyle.CE_CheckBox,checkBoxOption,painter) else:super(C_Delegate, self).paint(painter, option, index)def setEditorData(self, spinBox, index):columnIndex = index.column()if columnIndex == 5:data = index.data()if data > 0:spinBox.setChecked(True)else:spinBox.setChecked(False)else: super(C_Delegate, self).setEditorData( spinBox, index)def setModelData(self, spinBox, model, index): columnIndex = index.column()if columnIndex == 5:data = spinBox.isChecked()if data:model.setData(index, 1)else:model.setData(index, 0)else:super(C_Delegate, self).setModelData( spinBox, model, index)def updateEditorGeometry(self, editor, option, index):editor.setGeometry(option.rect)?
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的转载:QTableView中嵌入可视化组件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (二)简单的登陆注册系统--增加验证码部
- 下一篇: C++隐式转换