我已经向 QTableView
添加了一个自定义委托(delegate)器。当我双击一个项目时,我看到编辑器小部件是一个“QSpinBox”,我可以很好地编辑该值。一旦失去焦点,这个编辑器小部件就会消失,我明白这一点。我想要的是 QSpinBox
一直在那里。查看 Qt 示例 here我知道我需要覆盖 QAbstractItemDelegate
类的 paint
函数来绘制 QSpinBox
但我不知道该怎么做。一般来说,我想知道如何在绘制函数中绘制任何 Qt 小部件。
作为引用,我有以下测试代码:
#include <QtWidgets/QApplication>
#include <QtGui>
#include <QTableview>
#include <QLayout>
#include <QColor>
#include <QStyledItemDelegate>
#include <QSpinbox>
class SpinBoxDeligate : public QStyledItemDelegate {
public:
QWidget * createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const override {
auto w = new QSpinBox(parent);
w->setFrame(false);
w->setMinimum(0);
w->setMaximum(100);
return w;
}
void setEditorData(QWidget *editor, const QModelIndex &index) const override {
static_cast<QSpinBox*>(editor)->setValue(index.data(Qt::EditRole).toInt());
}
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override {
model->setData(index, static_cast<QSpinBox*>(editor)->value(), Qt::EditRole);
}
void paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
// What to replace below line with to have a QSpinBox
QStyledItemDelegate::paint(painter, option, index);
}
};
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QStandardItemModel model(3, 1);
for (int r = 0; r < 3; ++r)
{
auto text = QString("%0").arg(r);
QStandardItem* item = new QStandardItem(text);
item->setFlags(Qt::ItemIsUserCheckable
| Qt::ItemIsEnabled
| Qt::ItemIsEditable
);
item->setData(Qt::Unchecked, Qt::CheckStateRole);
item->setData(text, Qt::ToolTipRole);
item->setData(QSize(100, 30), Qt::SizeHintRole);
item->setData(QIcon(":/QtMVC/Desert.jpg"), Qt::DecorationRole);
model.setItem(r, 0, item);
}
QTableView* table = new QTableView();
table->setModel(&model);
table->setItemDelegate(new SpinBoxDeligate());
QWidget w;
QVBoxLayout* containerLayout = new QVBoxLayout();
w.setLayout(containerLayout);
containerLayout->addWidget(table);
w.show();
return app.exec();
}
最佳答案
您的背景问题的一个可能的解决方案是使用 paint()
并且为此您可以创建一个 QSpinBox 并使用 grab 来拍摄图像,但在此之前您应该计算几何形状以便它不包括 QCheckBox,如您所见,这是一项乏味的工作,另一种方法是使用 QStyle,但它仍然需要更多代码。
一个简单的解决方案是使用 openPersistentEditor()
保持编辑器打开方法。
int main(int argc, char** argv)
{
QApplication app(argc, argv);
QStandardItemModel model(3, 1);
QTableView* table = new QTableView();
table->setModel(&model);
table->setItemDelegate(new SpinBoxDeligate());
for (int r = 0; r < 3; ++r)
{
auto text = QString("%0").arg(r);
QStandardItem* item = new QStandardItem(text);
item->setFlags(Qt::ItemIsUserCheckable
| Qt::ItemIsEnabled
| Qt::ItemIsEditable
);
item->setData(Qt::Unchecked, Qt::CheckStateRole);
item->setData(text, Qt::ToolTipRole);
item->setData(QSize(100, 30), Qt::SizeHintRole);
item->setData(QIcon(":/QtMVC/Desert.jpg"), Qt::DecorationRole);
model.setItem(r, 0, item);
table->openPersistentEditor(model.indexFromItem(item));
}
QWidget w;
QVBoxLayout* containerLayout = new QVBoxLayout();
w.setLayout(containerLayout);
containerLayout->addWidget(table);
w.show();
return app.exec();
}
关于c++ - 如何覆盖自定义委托(delegate)类的 'paint'函数来绘制QSpinBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51743463/