c++ - Qt 在 QTableView 中设置自定义小部件

标签 c++ qt view model

我需要将一个自定义小部件放入子类 QAbstractTableModelQTableView 单元格中。

我搜索了已经给出的解决方案,但没有人满足我的需求。 自定义小部件必须始终保留在此处,而不仅仅是像 QItemDelegate::createEditor 那样处于编辑模式。 自定义小部件可能是一切,我正在寻找所有小部件的通用解决方案,而不仅仅是 QPushButtonsQCheckBox

抱歉我的英语不好。

最佳答案

您可以使用QAbstractItemView::setIndexWidget并压倒一切QAbstractItemView::dataChanged实现您想要的目标如下:

class MyTableView : public QTableView
{
protected:
  void dataChanged(const QModelIndex &topLeft, const QModelIndex & bottomRight, const QVector<int> & roles)
  {
    QPushButton *pPushButton = qobject_cast<QPushButton*>(indexWidget(topLeft));
    if (pPushButton)
        pPushButton->setText(model()->data(topLeft, Qt::DisplayRole).toString());
    else
        QTableView::dataChanged(topLeft, bottomRight, roles);
  }
};

void main ()
{
    MyTableView table;
    table.setIndexWidget(table.model()->index(0, 0, QModelIndex()),new QPushButton());
}

请注意,这是一个不完整的实现,但它应该向您展示如何解决您的问题。真正的实现应该更新topLeft和bottomRight之间的所有QPushButton。

关于c++ - Qt 在 QTableView 中设置自定义小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43277569/

相关文章:

c++ - 是否有任何带有执行器的在线编译器可以编译使用特定于 GPU 的 C/C++ 代码的应用程序?

qt - 没有小部件的系统托盘图标

android - 以编程方式修改所有按钮的文本大小?

php - 在 PHP 应用程序中使用 MySQL View

c++ - 了解 Qt View 模型架构 : when to create and how to cleanup indexes in QAbstractItemModel implementation?

view - 我的 IAP 不工作。 func Paymentqueue 的错误

php - 如何捕获 Windows cmd shell 的输出?

c++ - 数组作为指针传递的不明确重载

c++ - 在std::unique_ptr的 vector 上使用make_transform_iterator获取const指针

c++ - Qt C++ : Line edit accept only alphanumeric characters, 破折号和下划线