将 QItemDelegate 与 QAbstractTableModel 结合使用

发布于 2024-11-29 20:11:42 字数 7517 浏览 1 评论 0原文

我有一个 QAbstractItemModel 和一个 QItemDelegate,这是我的问题。代表什么也不做。正在调用其子例程,但没有任何反应。

这是我希望在表中看到的内容。

Text : QComboBox : Text : Text : QProgressBar

其中 : 是列分隔符。

代表。 #ifndef DELEGATEACTION_H #define DELEGATEACTION_H

#include <QVariant>
#include <QItemDelegate>

#include <QWidget>
#include <QLabel>
#include <QComboBox>
#include <QProgressBar>

class DelegateAction : public QItemDelegate
{
    Q_OBJECT
public:
    explicit DelegateAction(QObject *parent = 0);

    QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

#endif // DELEGATEACTION_H

#include "DelegateAction.h"

DelegateAction::DelegateAction(QObject *parent) :
    QItemDelegate(parent)
{
}

QWidget * DelegateAction::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QWidget* editor = 0;

    switch (index.column())
    {
    case 0:
    default:
    {
        editor = new QLabel();
        break;
    }
    case 1:
    {
        QComboBox* combo = new QComboBox(parent);
        combo->addItem("Test");
        combo->addItem("Test 2");
        editor = combo;
        break;
    }
    case 4:
    {
        editor = new QProgressBar(parent);
        break;
    }
    }

    editor->installEventFilter(const_cast<DelegateAction*>(this));
    return editor;
}

void DelegateAction::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QVariant value = index.model()->data(index, Qt::DisplayRole);

    switch (index.column())
    {
    case 0:
    default:
    {
        QLabel* label = static_cast<QLabel*>(editor);
        label->setText(value.toString());
        break;
    }
    case 1:
    {
        QComboBox* combo = static_cast<QComboBox*>(editor);
        combo->setCurrentIndex(combo->findText(value.toString()));
        break;
    }
    case 4:
    {
        QProgressBar* progress = static_cast<QProgressBar*>(editor);
        progress->setValue(value.toInt());
        break;
    }
    }

}

void DelegateAction::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QVariant value;
    switch (index.column())
    {
    case 0:
    default:
    {
        value = static_cast<QLabel*>(editor)->text();
        break;
    }
    case 1:
    {
        value = static_cast<QComboBox*>(editor)->currentText();
        break;
    }
    case 4:
    {
        value = static_cast<QProgressBar*>(editor)->value();
        break;
    }
    }

    model->setData(index, value);
}

void DelegateAction::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

模型。

#ifndef MODELACTIONS_H
#define MODELACTIONS_H

#include <QAbstractTableModel>

#include <Unit.h>

class ModelAction : public QAbstractTableModel
{
    Q_OBJECT
public:
    explicit ModelAction(QObject *parent = 0);
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    Qt::ItemFlags flags(const QModelIndex &index) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    bool setData(const QModelIndex &index, const QVariant &value, int role);
    void sort(int column, Qt::SortOrder order);


    void setUnits(const QList<Unit*>* units);

private:
    const QList<Unit*>* units;
    bool ascending[5];

};

#endif // MODELACTIONS_H

#include "ModelAction.h"

ModelAction::ModelAction(QObject *parent) :
    QAbstractTableModel(parent),
    units(0)
{
}

int ModelAction::rowCount(const QModelIndex &parent) const
{
    if (units == 0)
    {
        return 0;
    }
    else
    {
        return units->length();
    }
}
int ModelAction::columnCount(const QModelIndex &parent) const
{
    return 5;
}
QVariant ModelAction::data(const QModelIndex &index, int role) const
{
    if (index.isValid() == false)
    {
        return QVariant();
    }

    if (role == Qt::TextAlignmentRole)
    {
        if (index.column() == 0 || index.column() == 2)
        {
            return int(Qt::AlignLeft | Qt::AlignVCenter);
        }
        else
        {
            return int(Qt::AlignRight | Qt::AlignVCenter);
        }
    }
    else if (role == Qt::DisplayRole)
    {
        if (index.column() == 0)
        {
            // Unit's id.
            return index.row() + 1;
        }
        else if (index.column() == 1)
        {
            return "bob";
            // Unit's Action.
            //return mechs.at(index.row())->getWeight();
        }
        else if (index.column() == 2)
        {
            // Unit's Action start.
            //return mechs.at(index.row())->getTechnology();
        }
        else if (index.column() == 3)
        {
            // Unit's Action end.
            //return Currency::numberToCurrency(mechs.at(index.row())->getPurchaseValue());
        }
        else if (index.column() == 4)
        {
            // Unit's Action progress.
            //return Currency::numberToCurrency(mechs.at(index.row())->getSellValue());
        }
    }
    return QVariant();
}

QVariant ModelAction::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
    {
        return QVariant();
    }

    if (orientation == Qt::Horizontal)
    {
        if (section == 0)
        {
            return "Id";
        }
        else if (section == 1)
        {
            return "Action";
        }
        else if (section == 2)
        {
            return "Begin Time";
        }
        else if (section == 3)
        {
            return "End Time";
        }
        else if (section == 4)
        {
            return "Progress";
        }
    }
    return QVariant();
}

void ModelAction::sort(int column, Qt::SortOrder order)
{
//    MechCompare compare;
//    compare.column = column;
//    ascending[column] = !ascending[column];
//    compare.ascending = ascending[column];
//    qSort(mechs.begin(), mechs.end(), compare);
    //    reset();
}

void ModelAction::setUnits(const QList<Unit *> *units)
{
    this->units = units;
    reset();
}

Qt::ItemFlags ModelAction::flags(const QModelIndex &index) const
{
    switch (index.column())
    {
    case 0:
    default:
    {
        return Qt::NoItemFlags;
        break;
    }
    case 1:
    {
        return Qt::ItemIsEditable | Qt::ItemIsEnabled;
    }
    }
}

bool ModelAction::setData(const QModelIndex &index, const QVariant &value, int role)
{
    switch (index.column())
    {
    case 1:
    {

    }
    }
}

我知道的唯一问题是 ModelAction::setData() 函数不完整。在完成该子例程之前,我必须返回并编辑该模型显示的数据类。组合框和进度条仍然应该显示,只是不执行任何操作。

此时,我只能看到表中每一行的 ID 号和测试文本“bob”。 QComboBox 和 QProgressBar 根本渲染。

任何帮助将不胜感激。

杰克

I have a QAbstractItemModel and a QItemDelegate and here is my problem. The Delegate does nothing. Its subroutines are being called but nothing happens.

Here is what I would like to see in my table.

Text : QComboBox : Text : Text : QProgressBar

where : is a column seperator.

Delegate.
#ifndef DELEGATEACTION_H
#define DELEGATEACTION_H

#include <QVariant>
#include <QItemDelegate>

#include <QWidget>
#include <QLabel>
#include <QComboBox>
#include <QProgressBar>

class DelegateAction : public QItemDelegate
{
    Q_OBJECT
public:
    explicit DelegateAction(QObject *parent = 0);

    QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    void setEditorData(QWidget *editor, const QModelIndex &index) const;
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
};

#endif // DELEGATEACTION_H

#include "DelegateAction.h"

DelegateAction::DelegateAction(QObject *parent) :
    QItemDelegate(parent)
{
}

QWidget * DelegateAction::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QWidget* editor = 0;

    switch (index.column())
    {
    case 0:
    default:
    {
        editor = new QLabel();
        break;
    }
    case 1:
    {
        QComboBox* combo = new QComboBox(parent);
        combo->addItem("Test");
        combo->addItem("Test 2");
        editor = combo;
        break;
    }
    case 4:
    {
        editor = new QProgressBar(parent);
        break;
    }
    }

    editor->installEventFilter(const_cast<DelegateAction*>(this));
    return editor;
}

void DelegateAction::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    QVariant value = index.model()->data(index, Qt::DisplayRole);

    switch (index.column())
    {
    case 0:
    default:
    {
        QLabel* label = static_cast<QLabel*>(editor);
        label->setText(value.toString());
        break;
    }
    case 1:
    {
        QComboBox* combo = static_cast<QComboBox*>(editor);
        combo->setCurrentIndex(combo->findText(value.toString()));
        break;
    }
    case 4:
    {
        QProgressBar* progress = static_cast<QProgressBar*>(editor);
        progress->setValue(value.toInt());
        break;
    }
    }

}

void DelegateAction::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QVariant value;
    switch (index.column())
    {
    case 0:
    default:
    {
        value = static_cast<QLabel*>(editor)->text();
        break;
    }
    case 1:
    {
        value = static_cast<QComboBox*>(editor)->currentText();
        break;
    }
    case 4:
    {
        value = static_cast<QProgressBar*>(editor)->value();
        break;
    }
    }

    model->setData(index, value);
}

void DelegateAction::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

Model.

#ifndef MODELACTIONS_H
#define MODELACTIONS_H

#include <QAbstractTableModel>

#include <Unit.h>

class ModelAction : public QAbstractTableModel
{
    Q_OBJECT
public:
    explicit ModelAction(QObject *parent = 0);
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    Qt::ItemFlags flags(const QModelIndex &index) const;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const;
    bool setData(const QModelIndex &index, const QVariant &value, int role);
    void sort(int column, Qt::SortOrder order);


    void setUnits(const QList<Unit*>* units);

private:
    const QList<Unit*>* units;
    bool ascending[5];

};

#endif // MODELACTIONS_H

#include "ModelAction.h"

ModelAction::ModelAction(QObject *parent) :
    QAbstractTableModel(parent),
    units(0)
{
}

int ModelAction::rowCount(const QModelIndex &parent) const
{
    if (units == 0)
    {
        return 0;
    }
    else
    {
        return units->length();
    }
}
int ModelAction::columnCount(const QModelIndex &parent) const
{
    return 5;
}
QVariant ModelAction::data(const QModelIndex &index, int role) const
{
    if (index.isValid() == false)
    {
        return QVariant();
    }

    if (role == Qt::TextAlignmentRole)
    {
        if (index.column() == 0 || index.column() == 2)
        {
            return int(Qt::AlignLeft | Qt::AlignVCenter);
        }
        else
        {
            return int(Qt::AlignRight | Qt::AlignVCenter);
        }
    }
    else if (role == Qt::DisplayRole)
    {
        if (index.column() == 0)
        {
            // Unit's id.
            return index.row() + 1;
        }
        else if (index.column() == 1)
        {
            return "bob";
            // Unit's Action.
            //return mechs.at(index.row())->getWeight();
        }
        else if (index.column() == 2)
        {
            // Unit's Action start.
            //return mechs.at(index.row())->getTechnology();
        }
        else if (index.column() == 3)
        {
            // Unit's Action end.
            //return Currency::numberToCurrency(mechs.at(index.row())->getPurchaseValue());
        }
        else if (index.column() == 4)
        {
            // Unit's Action progress.
            //return Currency::numberToCurrency(mechs.at(index.row())->getSellValue());
        }
    }
    return QVariant();
}

QVariant ModelAction::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
    {
        return QVariant();
    }

    if (orientation == Qt::Horizontal)
    {
        if (section == 0)
        {
            return "Id";
        }
        else if (section == 1)
        {
            return "Action";
        }
        else if (section == 2)
        {
            return "Begin Time";
        }
        else if (section == 3)
        {
            return "End Time";
        }
        else if (section == 4)
        {
            return "Progress";
        }
    }
    return QVariant();
}

void ModelAction::sort(int column, Qt::SortOrder order)
{
//    MechCompare compare;
//    compare.column = column;
//    ascending[column] = !ascending[column];
//    compare.ascending = ascending[column];
//    qSort(mechs.begin(), mechs.end(), compare);
    //    reset();
}

void ModelAction::setUnits(const QList<Unit *> *units)
{
    this->units = units;
    reset();
}

Qt::ItemFlags ModelAction::flags(const QModelIndex &index) const
{
    switch (index.column())
    {
    case 0:
    default:
    {
        return Qt::NoItemFlags;
        break;
    }
    case 1:
    {
        return Qt::ItemIsEditable | Qt::ItemIsEnabled;
    }
    }
}

bool ModelAction::setData(const QModelIndex &index, const QVariant &value, int role)
{
    switch (index.column())
    {
    case 1:
    {

    }
    }
}

The only issue I'm aware of is the ModelAction::setData() function is incomplete. I have to go back and edit the data classes that this model displays before I can complete that subroutine. Still the comboboxes and progressbars should still display, just not do anything.

At this point I only see the id numbers and my test text "bob" for each row in the table. The QComboBox and QProgressBar are not rendered at all.

Any help will be appreciated.

Jec

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人疚 2024-12-06 20:11:42

您实现的委托函数适用于编辑。当您不编辑项目时,它们不会显示。看来您可能需要 QAbstractItemView::setIndexWidget 而不是委托。

The delegate functions you implemented are for editors. They are not displayed when you are not editing an item. It seems you may want QAbstractItemView::setIndexWidget instead of the delegate.

深爱不及久伴 2024-12-06 20:11:42

createEditor 方法仅在某些事件发生后调用。例如,当您双击一个单元格时,...正如巴克指出的那样,您必须使用 setIndexWidget。

The createEditor method is only called after certain events. For example, when you double-click a cell,... As buck pointed out, you've got to use setIndexWidget.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文