【问题标题】:QComboBox in QTreeView-cellQTreeView-cell 中的 QComboBox
【发布时间】:2016-08-17 06:48:39
【问题描述】:

我想在 QTreeView 的特定单元格中显示一个 QComboBox。我知道我必须为此使用自己的模型。整个事情已经在单元格中显示的 QIcons 上正常工作,但我没有用组合框做同样的事情。这就是我的模型的样子(一些不完整的伪代码):

QVariant MyListModel::data(const QModelIndex &index, int role) const
{
   ...

   switch(role)
   {
      ...
      case Qt::DecorationRole:
         switch(index.column())
         {
            case eBLA:
               // return QIcon(); --> compiles properly
               return m_placePosCombos[index.row()]; --> compilation fails
               return QComboBox(); --> compilation fails
               break;
            default:

当我尝试返回一个 QComboBox 时,我得到一个编译错误

cannot convert from 'const QComboBox' to 'QVariant'

MyListModel 继承自 QAbstractListModel。

知道我必须做什么才能使用 QComboBox 而不是愚蠢的图标吗?

谢谢!

【问题讨论】:

  • 您不能通过复制(或移动)返回QObject,也不能将一个包裹在QVariant 中(尽管您可以存储指向一个的指针)。

标签: c++ qt qt5 qcombobox qabstractlistmodel


【解决方案1】:

不,模型仅用于视图中的数据处理,而不用于更改视图。 您需要学习和使用: QItemDelegate(很抱歉没有在这里显示所有代码,它需要时间和地点......你可能需要阅读tuto和相关文档的时间......希望有所帮助)

tuto for QItemDelegate

QItemDelegate API class Qt5

【讨论】:

    猜你喜欢
    • 2016-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-29
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    相关资源
    最近更新 更多