QStyledItemDelegate как сделать два виджета в один ряд?

Я создал класс QStyledItemDelegate, в котором я хочу сделать некоторые элементы доступными для проверки, а некоторые - двумя виджетами. Но это не работает правильно. Что мне не хватает? Вот как это выглядит:

введите здесь описание изображения

См. строку 1, похоже, что два виджета есть, но на самом деле они не отображаются. И мне нужна помощь, чтобы сделать элемент доступным для проверки (это отличается от добавления флажка?). Спасибо.

Вот мой класс QStyledItemDelegate:

//! [0]
SpinBoxDelegate::SpinBoxDelegate(QObject *parent)
    : QStyledItemDelegate(parent)
{
}
//! [0]

//! [1]
QWidget *SpinBoxDelegate::createEditor(QWidget *parent,
    const QStyleOptionViewItem &option,
    const QModelIndex &index) const
{
    if (index.row()==1) {
    QLineEdit* lineBox;
    QCheckBox* checkBox;
    QWidget *panel;
    panel = new QWidget(parent);
    QHBoxLayout *layout = new QHBoxLayout;

    lineBox = new QLineEdit( );
    lineBox->setText("abc");
    checkBox = new QCheckBox( );

    layout->addWidget(checkBox);
    layout->addWidget(lineBox);
    panel->setLayout(layout);
    return panel;
}else if (index.row()==2) {
// need to make this check-able item?
}else{
    QLineEdit *editor = new QLineEdit(parent);
    return editor;
}
}
//! [1]

//! [2]
void SpinBoxDelegate::setEditorData(QWidget *editor,
                                const QModelIndex &index) const
{
int value = index.model()->data(index, Qt::EditRole).toInt();

if (index.row()==1) {
// need something here?
}else{
    QLineEdit *spinBox = static_cast<QLineEdit*>(editor);
    spinBox->setText("value");

}
}
//! [2]

//! [3]
void SpinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
if (index.row()==1) {
// need something here?
}else{
   QLineEdit *spinBox = static_cast<QLineEdit*>(editor);
   model->setData(index, spinBox->text(), Qt::EditRole);
}


}
//! [3]

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

это мой main.cpp:

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

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

SpinBoxDelegate delegate;
treeView.setItemDelegate(&delegate);
//! [0]

//tableView.horizontalHeader()->setStretchLastSection(true);
treeView.setRootIsDecorated(false);
treeView.setHeaderHidden(true);
treeView.setIndentation(20);
//! [1]
for (int row = 0; row < 4; ++row) {
    for (int column = 0; column < 2; ++column) {
        QModelIndex index = model.index(row, column, QModelIndex());
        model.setData(index, QVariant((row + 1) * (column + 1)));
    }
//! [1] //! [2]
}
//! [2]

//! [3]
treeView.setWindowTitle(QObject::tr("Spin Box Delegate"));
treeView.show();
return app.exec();
}
//! [3]

В конечном итоге это то, чего я хочу достичь:

введите здесь описание изображения


person YeP    schedule 29.07.2018    source источник
comment
хорошо, добавил скрин...   -  person YeP    schedule 29.07.2018
comment
хорошо, я добавил свой main.cpp. Я изменил это из примера qt и просто попытался задать свой вопрос. Я попытался изменить QTableView в примере на QTreeView, похоже, это не имеет значения ... QSpinbox не имеет ничего общего с моим вопросом.   -  person YeP    schedule 29.07.2018
comment
Я добавил то, чего хочу добиться, в основном проверяемый элемент с LineEdit и ComboBox, следующим за ним... Пока я просто пробую что-то в примере. Извините за путаницу. Спасибо.   -  person YeP    schedule 29.07.2018
comment
Это работает отлично. Спасибо. Итак, если у меня есть другие элементы с другим виджетом, мне нужно создать отдельный класс для каждого вида? Также для чего CustomRoles::SelectRole? Большое спасибо.   -  person YeP    schedule 30.07.2018


Ответы (1)


Для поддержания порядка необходимо создать класс, реализующий пользовательский виджет.

С другой стороны, вы должны хранить данные в ролях, поскольку редакторы могут постоянно создаваться и уничтожаться.

Чтобы получить правильный размер, используйте sizeHint(), хранящийся в роли.

И, наконец, чтобы вас всегда видели, вы должны использовать метод openPersistentEditor():

#include <QApplication>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QCheckBox>
#include <QStyledItemDelegate>
#include <QStandardItemModel>
#include <QTreeView>
#include <QComboBox>
#include <QHeaderView>

enum CustomRoles{
    SelectRole = Qt::UserRole
};

class EditorWidget: public QWidget{
    Q_OBJECT
public:
    EditorWidget(QWidget *parent=nullptr)
        : QWidget(parent),
          checkBox(new QCheckBox),
          lineBox(new QLineEdit),
          comboBox(new QComboBox)
    {
        QHBoxLayout *layout = new QHBoxLayout(this);
        comboBox->addItems({"item1", "item2", "item3"});
        layout->addWidget(checkBox);
        layout->addWidget(lineBox);
        layout->addWidget(comboBox);
    }

    int currentIndex(){
        return comboBox->currentIndex();
    }
    void setCurrentIndex(int index){
        comboBox->setCurrentIndex(index);
    }
    QString text() const{
        return  lineBox->text();
    }
    void setText(const QString &text){
        lineBox->setText(text);
    }
    Qt::CheckState checkState() const{
        return checkBox->checkState();
    }
    void setCheckState(Qt::CheckState state){
        checkBox->setCheckState(state);
    }
private:
    QCheckBox *checkBox;
    QLineEdit *lineBox;
    QComboBox *comboBox;
};

class Delegate: public QStyledItemDelegate{
    Q_OBJECT
public:
    using QStyledItemDelegate::QStyledItemDelegate;
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
        Q_UNUSED(painter)
        Q_UNUSED(option)
        Q_UNUSED(index)
    }
    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const{
        Q_UNUSED(option)
        Q_UNUSED(index)
        EditorWidget *editor = new EditorWidget(parent);
        return editor;
    }
    void setEditorData(QWidget *editor, const QModelIndex &index) const{
        EditorWidget *widget = static_cast<EditorWidget*>(editor);
        widget->blockSignals(true);
        widget->setText(index.data(Qt::DisplayRole).toString());
        Qt::CheckState state = static_cast<Qt::CheckState>(index.data(Qt::CheckStateRole).toInt());
        widget->setCheckState(state);
        widget->setCurrentIndex(index.data(CustomRoles::SelectRole).toInt());
        widget->blockSignals(false);
    }
    void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const{
        EditorWidget *widget = static_cast<EditorWidget*>(editor);
        model->setData(index, widget->text(), Qt::DisplayRole);
        model->setData(index, widget->checkState(), Qt::CheckStateRole);
        model->setData(index, widget->sizeHint(), Qt::SizeHintRole);
        model->setData(index, widget->currentIndex(), CustomRoles::SelectRole);
    }
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const{
        Q_UNUSED(index)
        editor->setGeometry(option.rect);
    }
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
    {
        QSize s =  index.data(Qt::SizeHintRole).toSize();
        return s.isValid() ? s: QStyledItemDelegate::sizeHint(option, index);
    }
};

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

    QTreeView treeView;
    QStandardItemModel model(4, 2);
    treeView.setModel(&model);

    Delegate delegate;
    treeView.setItemDelegate(&delegate);

    treeView.header()->setSectionResizeMode(QHeaderView::ResizeToContents);
    treeView.setRootIsDecorated(false);
    treeView.setHeaderHidden(true);
    treeView.setIndentation(20);
    for (int row = 0; row < 4; ++row) {
        for (int column = 0; column < 2; ++column) {
            QModelIndex index = model.index(row, column, QModelIndex());
            model.setData(index, QVariant((row + 1) * (column + 1)));
            treeView.openPersistentEditor(index);
        }
    }
    treeView.resize(treeView.sizeHint());
    treeView.show();

    return a.exec();
}

#include "main.moc"

введите описание изображения здесь

Полный пример можно найти по следующей ссылке.

person eyllanesc    schedule 29.07.2018
comment
Еще один вопрос... В дереве у меня есть несколько корневых элементов и несколько дочерних элементов под каждым корневым элементом. Я пытаюсь назначить что-то вроде EditorWidget корневым элементам, и ничего не происходит. Что мне не хватает? Спасибо. - person YeP; 01.08.2018
comment
@YeP Я думаю, вы неправильно его назначаете, без кода будет трудно понять, в чем проблема. - person eyllanesc; 01.08.2018
comment
Я думаю, что treeView.openPersistentEditor(index); в основном дело. Ваш код перебирает все элементы, поэтому все отображается правильно. По какой-то причине в моем коде я не сделал этого для корневых элементов. Это тот openPersistentEditor, который вызывает обновление элемента? Можно ли открывать PersistentEditor для элементов, которые мне не нужны/используются в каком-либо редакторе? Спасибо. - person YeP; 03.08.2018