简体   繁体   English

在QTreeWidget中仅禁用一个项目

[英]Disable only one item in QTreeWidget

I have a QTreeWidget that can have many rows but only 3 columns: 我有一个QTreeWidget,可以有很多行但只有3列:

在此输入图像描述

In the picture I have selected my last column. 在图片中,我选择了我的最后一栏。 What I would like to do, is to disable the last column for every item in the tree UNLESS it is selected. 我想做的是禁用树中每个项目的最后一列,除非它被选中。 So in my situation only the pidtest.xml item would have enabled checkbox. 所以在我的情况下,只有pidtest.xml项才会启用复选框。 I dont know how to do this with existing methods. 我不知道如何使用现有方法执行此操作。 I would aprichiate all help! 我会帮助你!

Here you have a complete working example on how to do that. 在这里,您有一个完整的工作示例,说明如何做到这一点。

As Pavel Strakhov have pointed out, the example use a QTreeView , since QTreeWidgetItem do not support "partial disabling". 正如Pavel Strakhov所指出的,该示例使用QTreeView ,因为QTreeWidgetItem不支持“部分禁用”。

In the example a TreeView is displayed, showing 2 columns (name, enabled). 在示例中,将显示TreeView,显示2列(名称,已启用)。 You will be able to edit the first column only if the second is true. 只有当第二列为真时,您才能编辑第一列。

There is no implementation in the example for changing the values, you will have to add the setData function to the model to do that. 在示例中没有用于更改值的实现,您必须将setData函数添加到模型中才能执行此操作。

Complete example: 完整的例子:

#include <QtWidgets/QApplication>
#include <QTreeView>
#include <QAbstractTableModel>
#include <QString>
#include <QVariant>
#include <QList>

typedef struct entry_{
    entry_(const QString &n, bool e) : name(n), enabled(e) {}
    QString name; bool enabled;
} table_entry_t;

class SimpleModel : public QAbstractTableModel
{
public:
    SimpleModel(QWidget *parent = nullptr) : QAbstractTableModel(parent)
    {
        m_entries = {
            {"Jhon Doe", false},
            {"Jhon Doe Jr.", true}
        };
    }

    QVariant data(const QModelIndex &index, int role) const {
        switch (role) {
        case Qt::DisplayRole:
            table_entry_t entry = m_entries[index.row()];
            if (index.column() == 0)
                return QVariant(entry.name);
            if (index.column() == 1)
                return QVariant(entry.enabled);
        }
        return QVariant();
    }

    Qt::ItemFlags flags(const QModelIndex &index) const {
        Qt::ItemFlags flags = QAbstractTableModel::flags(index);
        if (!m_entries[index.row()].enabled && index.column() == 0)
            flags ^ Qt::ItemIsEnabled;
        else
            flags |= Qt::ItemIsEditable;
        return flags;
    }

    int rowCount(const QModelIndex &parent /* = QModelIndex() */) const {Q_UNUSED(parent); return static_cast<int>(m_entries.size());}
    int columnCount(const QModelIndex &parent /* = QModelIndex() */) const {Q_UNUSED(parent); return 2; }

private:
    QList<table_entry_t> m_entries;
};


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QTreeView tree;
    SimpleModel *model = new SimpleModel();
    tree.setModel(model);
    tree.show();
    return a.exec();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM