【问题标题】:QStyledItemDelegate how to make two widgets in one row?QStyledItemDelegate 如何在一行中制作两个小部件?
【发布时间】:2018-07-29 06:09:35
【问题描述】:

我创建了一个 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]

这就是我最终想要实现的目标:

【问题讨论】:

  • 好的,添加截图...
  • 好的,我添加了我的 main.cpp。我从 qt 示例中对此进行了修改,并尝试提出我的问题。我试图将示例中的 QTableView 更改为 QTreeView,似乎没有什么不同... QSpinbox 与我的问题无关。
  • 我添加了我想要实现的目标,基本上是一个带有 LineEdit 和一个 ComboBox 的可检查项目......我现在只是在一个例子中尝试一下。很抱歉造成混乱。谢谢。
  • 效果很好。谢谢。因此,如果我有其他具有不同小部件的项目,我需要为每种类型创建不同的类吗?还有什么是 CustomRoles::SelectRole ?非常感谢。

标签: c++ qt qt5 qstyleditemdelegate


【解决方案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"

完整的例子可以在下面的link找到

【讨论】:

  • 一个额外的问题......在树视图中,我在每个根项目下都有一些根项目和一些子项目。我尝试将诸如 EditorWidget 之类的东西分配给根项目,但没有任何反应。我错过了什么?谢谢。
  • @YeP 我认为你分配不正确,没有代码很难知道问题出在哪里。
  • 我认为“treeView.openPersistentEditor(index);”主要是事情。您的代码循环遍历所有项目,因此所有项目都显示正确。出于某种原因,在我的代码中,我没有对根项目执行此操作。是 openPersistentEditor 导致项目被更新吗?可以在我不想/使用任何编辑器的项目上打开 PersistentEditor 吗?谢谢。
猜你喜欢
  • 2020-03-08
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 2018-10-26
  • 1970-01-01
  • 2020-04-23
  • 2017-03-23
相关资源
最近更新 更多