天天看點

轉載:QTableView中嵌入可視化元件

QTableView中嵌入可視化元件方法有四種:

        第一種不能之前顯示,必須輕按兩下/選中後才能顯示,不适用。

        第二種比較簡單,通常用這種方法。

        第三種隻适合靜态顯示靜态資料用

        第四種比較适合擴充,它除了可以嵌入複選框,還可以通過paint()繪制其它控件,圖檔等自定義風格。

第一種方法是:編輯委托法

這種方法直接利用委托中重載createEditor(),激活QCheckBox,這個缺點是必須輕按兩下/選中,才能顯示CheckBox控件。一般不滿足我們實際中的直接顯示的需要。可以參考Qt中的QSpinBoxDelegate例子。

第三種方法是:用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:

繼續閱讀