0

如何在表格的不同列中有不同的代表。例如,我希望第一列中的旋转框委托和第二列中的组合框。

我从示例目录中获取了一个 spinbox 委托的示例,并尝试进行更改:main.cpp

#include "test1.h"

#include <QApplication>
#include <QHeaderView>
#include <QStandardItemModel>
#include <QTableWidget>

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QStandardItemModel model(4, 2);
    QTableView tableView;
    tableView.setModel(&model);

    SpinBoxDelegate sdelegate;
    ComboBoxDelegate comDel;
    tableView.setItemDelegate(&sdelegate);

    tableView.horizontalHeader()->setStretchLastSection(true);


    for (int row = 0; row < 4; ++row) 
    {
        for (int column = 0; column < 1; ++column) 
        {
            tableView.setItemDelegate(&comDel);
            QModelIndex index = model.index(0, 0, QModelIndex());
        }
        for (int column = 1; column < 2; ++column) 
        {
            tableView.setItemDelegate(&sdelegate);
            QModelIndex index = model.index(1, 1, QModelIndex());
        }
    }

    tableView.setWindowTitle(QObject::tr("Spin Box & combo Box Delegate"));
    tableView.show();
    return app.exec();
}

test1.h 我的头文件

 #ifndef LINEEDIT_H
    #define LINEEDIT_H
    #include <QStyledItemDelegate>
    class SpinBoxDelegate : public QStyledItemDelegate
    {
        Q_OBJECT
    public:
        SpinBoxDelegate(QObject *parent = 0);
        QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
                              const QModelIndex &index) /*const Q_DECL_OVERRIDE*/;

        void setEditorData(QWidget *editor, const QModelIndex &index) /*const Q_DECL_OVERRIDE*/;
        void setModelData(QWidget *editor, QAbstractItemModel *model,
                          const QModelIndex &index) /*const Q_DECL_OVERRIDE*/;

        void updateEditorGeometry(QWidget *editor,
            const QStyleOptionViewItem &option, const QModelIndex &index) /*const Q_DECL_OVERRIDE*/;
    };

    class ComboBoxDelegate : public QStyledItemDelegate
    {
    Q_OBJECT

    public:
    ComboBoxDelegate(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 // LINEEDIT_H

test1.cpp 我的 cpp 文件

#include "test1.h"
#include <QSpinBox>
#include <QComboBox>


SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &/* option */,
    const QModelIndex &/* index */)/* const*/
{

    QSpinBox *editor = new QSpinBox(parent);
    editor->setFrame(false);
    editor->setMinimum(0);
    editor->setMaximum(100);
    return editor;
}

void SpinBoxDelegate::setEditorData(QWidget *editor,
                                    const QModelIndex &index) /*const*/
{
    int value = index.model()->data(index, Qt::EditRole).toInt();
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->setValue(value);
}

void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
                                   const QModelIndex &index) /*const*/
{
    QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
    spinBox->interpretText();
    int value = spinBox->value();
    model->setData(index, value, Qt::EditRole);
}

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




ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}

QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &/* option */,
const QModelIndex &/* index */) const
{
QComboBox *editor = new QComboBox(parent);
//QStringList list ;
//list << "srikanth" << "dilip";
//editor->addItems(list);
editor->installEventFilter(const_cast<ComboBoxDelegate*>(this));

return editor;
}

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

QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->addItem(value);
}

void ComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString value = comboBox->currentText();

model->setData(index, value);
}

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

0 回答 0