C++ 仅使 QTreeWidgetItem 的一列可编辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2801959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Making only one column of a QTreeWidgetItem editable
提问by Andreas Brinck
I have a QTreeWidgetItem
with two columns of data, is there any way to make only the second column editable? When I do the following:
我有QTreeWidgetItem
两列数据,有什么办法只能使第二列可编辑?当我执行以下操作时:
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setFlags(item->flags() | Qt::ItemIsEditable);
all columns become editable.
所有列都可以编辑。
采纳答案by Harald Scheirich
Looks like you will have to forgo using QTreeWidget
and QTreeWidgetItem
and go with QTreeView
and QAbstractItemModel
. The "Widget" classes are convenience classes that are concrete implementations of the more abstract but more flexible versions. QAbstractItemModel
has a call flags(QModelIndex index)
where you would return the appropriate value for your column.
看起来您将不得不放弃使用QTreeWidget
andQTreeWidgetItem
并使用QTreeView
and QAbstractItemModel
。“Widget”类是便利类,它们是更抽象但更灵活版本的具体实现。QAbstractItemModel
有一个电话flags(QModelIndex index)
,您可以在其中为您的列返回适当的值。
回答by d11
You can make only certain columns in a QTreeWidget editable using a workaround:
您可以使用一种解决方法使 QTreeWidget 中的某些列可编辑:
1) Set the editTriggers property of the QTreeWidget to NoEditTriggers
1)将QTreeWidget的editTriggers属性设置为NoEditTriggers
2) On inserting items, set the Qt:ItemIsEditable flag of the QTreeWidgetItem object
2) 在插入项目时,设置 QTreeWidgetItem 对象的 Qt:ItemIsEditable 标志
3) Connect the following slot to the "itemDoubleClicked" signal of the QTreeWidget object:
3) 将以下槽连接到 QTreeWidget 对象的“itemDoubleClicked”信号:
void MainWindow::onTreeWidgetItemDoubleClicked(QTreeWidgetItem * item, int column)
{
if (isEditable(column)) {
ui.treeWidget->editItem(item, column);
}
}
where "isEditable" is a function you wrote that returns true for editable columns and false for non-editable columns.
其中“isEditable”是您编写的函数,它对可编辑列返回 true,对不可编辑列返回 false。
回答by user571167
I had the same problem recently and discovered a solution which works with all EditTriggers, not only the DoubleClicked one (and the connection to the double clicked signal)
我最近遇到了同样的问题,并发现了一个适用于所有 EditTriggers 的解决方案,而不仅仅是 DoubleClicked 一个(以及与双击信号的连接)
Create a Delegate, that returns a NULL Pointer for the editor:
创建一个委托,为编辑器返回一个 NULL 指针:
class NoEditDelegate: public QStyledItemDelegate {
public:
NoEditDelegate(QObject* parent=0): QStyledItemDelegate(parent) {}
virtual QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const {
return 0;
}
};
And later use it as a custom delegate for your column
稍后将其用作您的专栏的自定义委托
ui->parameterView->setItemDelegateForColumn(0, new NoEditDelegate(this));
回答by Blaise
Seem like the standard QTreeWidget doesn't allow this. I think there are two ways to do this:
似乎标准 QTreeWidget 不允许这样做。我认为有两种方法可以做到这一点:
Use a QTreeView with your own class derived from QAbstractItemModel and override the flags function
Use a QTreeView with a QStandardItemModel. Then when you add the item just set the appropriate column to allow edits:
将 QTreeView 与从 QAbstractItemModel 派生的您自己的类一起使用并覆盖标志函数
将 QTreeView 与 QStandardItemModel 一起使用。然后,当您添加项目时,只需设置适当的列以允许编辑:
Here's some code for the second option:
这是第二个选项的一些代码:
QString x, y;
QList<QStandardItem*> newIt;
QStandardItem * item = new QStandardItem(x);
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
newIt.append(item);
item = new QStandardItem(y);
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled | Qt::ItemIsEditable);
newIt.append(item);
model->appendRow(newIt);
I find the second approach simpler but that depends on how much flexibility you want with your model.
我发现第二种方法更简单,但这取决于您希望模型具有多大的灵活性。
回答by clark
The simplest way that I found was to use Qt::ItemFlags
我发现的最简单的方法是使用 Qt::ItemFlags
void myClass::treeDoubleClickSlot(QTreeWidgetItem *item, int column)
{
Qt::ItemFlags tmp = item->flags();
if (isEditable(item, column)) {
item->setFlags(tmp | Qt::ItemIsEditable);
} else if (tmp & Qt::ItemIsEditable) {
item->setFlags(tmp ^ Qt::ItemIsEditable);
}
}
The top of the if
adds the editing functionality through an OR
, and the bottom checks if it is there with AND
, then removes it with a XOR
.
顶部if
通过 an 添加编辑功能OR
,底部检查它是否存在AND
,然后使用 将其删除XOR
。
This way the editing functionality is added when you want it, and removed when you don't.
这样,编辑功能会在您需要时添加,并在您不需要时删除。
Then connect this function to the tree widget's itemDoubleClicked()
signal, and write your 'to edit or not to edit' decision inside of isEditable()
然后将此功能连接到树小部件的itemDoubleClicked()
信号,并在其中写下您的“编辑或不编辑”决定isEditable()
回答by clark
class EditorDelegate : public QItemDelegate
{
Q_OBJECT
public:
EditorDelegate(QObject *parent):QItemDelegate(parent){};
QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};
QWidget* EditorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.column() == 1)
{
return QItemDelegate::createEditor(parent, option, index);
}
return nullptr;
}
In the QTreeWidget
:
在QTreeWidget
:
myQTreeWidget::myQTreeWidget()
{
EditorDelegate *d = new EditorDelegate(this);
this->setItemDelegate(d);
}
回答by chraz
Maybe a little late, but may help :
也许有点晚,但可能会有所帮助:
void MyClass::on_treeWidget_itemDoubleClicked(QTreeWidgetItem *item, int column) {
Qt::ItemFlags flags = item->flags();
if(column == 0)
{
item->setFlags(flags & (~Qt::ItemIsEditable));
}
else
{
item->setFlags(flags | Qt::ItemIsEditable);
}
}
Here 0 is the index of the column you want to make readonly.
这里 0 是您要设为只读的列的索引。
flags & (~Qt::ItemIsEditable)
Sets the ItemIsEditable position to 0 regardless the previous flag of your item.
将 ItemIsEditable 位置设置为 0,而不考虑项目的前一个标志。
flags | Qt::ItemIsEditable
Sets it to 1 regardless the previous flag.
无论前一个标志如何,都将其设置为 1。
回答by Dariusz
I found out that the code below works well for my needs and does "kinda" stop the user from editing certain parts of columns:
我发现下面的代码很适合我的需要,并且“有点”阻止用户编辑列的某些部分:
I basically check for role and then column. I only allow for editing in column 0. So if user edit it in any other column, then I stop the setData edit and no change is being made.
我基本上检查角色,然后是列。我只允许在第 0 列中进行编辑。因此,如果用户在任何其他列中对其进行编辑,那么我将停止 setData 编辑并且不会进行任何更改。
void treeItemSubclassed::setData(int column, int role, const QVariant &value) {
if (role == Qt::ItemIsEditable && column != 0){
return;
}
QTreeWidgetItem::setData(column, role, value);
}
回答by Kerry
I'm new to PySide and Python in general, but I was able to get this to work by registering with the QTreeWidget for itemClicked callbacks. Within the callback, check the column and only call 'editItem' if it's for a column you want to allow editing.
我一般是 PySide 和 Python 的新手,但我能够通过向 QTreeWidget 注册 itemClicked 回调来使其工作。在回调中,检查列,如果它是针对您要允许编辑的列,则仅调用“editItem”。
class Foo(QtGui.QMainWindow):
...
def itemClicked(self, item, column):
if column > 0:
self.qtree.editItem(item, column)
By not invoking editItem for column 0, the event is basically discarded.
通过不为第 0 列调用 editItem,该事件基本上被丢弃。
回答by Naruto
Set the child of the tree-widget editable or not(itmes of tree), based on the row and column.
根据行和列设置树小部件的子级可编辑或不可编辑(树的元素)。