c++ - 在 QItemEditorCreatorBase 中使用后取回 QWidget

标签 c++ qt qtablewidget qtablewidgetitem

我有一个扩展 QSpinBox

的数字编辑器
NumericEditor::NumericEditor(QWidget *widget): QSpinBox(widget)

我使用此编辑器编辑 QTableWidget 中的类型 QVariant::Int

QItemEditorCreatorBase *numericEditor = new QStandardItemEditorCreator<NumericEditor>();
factory->registerEditor(QVariant::Int, numericEditor); 

数据正常输入到表中。忽略“颜色”一词的用法。它基于颜色编辑器示例。

QTableWidgetItem *nameItem2 = new QTableWidgetItem(QString("label2"));
QTableWidgetItem *colorItem2 = new QTableWidgetItem();
colorItem2->setData(Qt::DisplayRole, QVariant(int(4)));
table->setItem(1, 0, nameItem2);
table->setItem(1, 1, colorItem2);

微调框出现并在 QTableWidget 中正常工作。

我的愿望是访问表格在编辑 QVariant::Int 单元格时使用的 QSpinBox 实例,以便我可以设置最小值和最大值。

我该怎么做?

最佳答案

您可以使用 QTableWidget::setItemDelegateForColumn 在列上安装委托(delegate),当打开编辑器时,将调用其 createEditor 方法。

class MyDelegate : public QStyledItemDelegate {
public:
    QWidget *createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const {
        // You could create the widget from scratch or call 
        // the base function which will use the QItemEditor you already wrote
        QWidget * editor = QStyledItemDelegate::createEditor(parent, option, index);

        // do whatever you need to do with the widget
        editor->setProperty("minimum", 100);
        editor->setProperty("maximum", 100);

        return editor;
    }
};

关于c++ - 在 QItemEditorCreatorBase 中使用后取回 QWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10869181/

相关文章:

qt - 在设计时为 QTableWidget 设置列宽

c++ - 在处理顺序访问的对象时,链表与数组的性能相比?

c++ - 为每个源文件单独设置跟踪级别的技术

linux - Nvidia Linux 图形调试器 Qt 平台插件 "xcb"

c++ - 尝试使用 visual studio 2008 在 Windows 中编译 qt-labs-qt3d 时出现错误

c++ - 在 QTableView 中适配行,摆脱多余的空间

c++ - 调试时将鼠标悬停在 Visual Studio 中的 operator->() 之后查看字段

c++ - 有效地遍历数据库行(libpqxx),为结构分配值

qt - 如何将 rowCountChanged 连接到插槽

c++ - Qt:从QTableWidget中的按钮获取文本