【问题标题】:QStandardItem global positionQStandardItem 全局位置
【发布时间】:2017-10-07 02:50:52
【问题描述】:

我的 Qt 版本是 5.5.1 (Linux),由于某种原因我无法升级它。

我有 QTableView(继承自 QWidget),并且这个 QTableView 有模型 QStandardItemModel 和一些 QStandardItem。问题是:在这种情况下如何获得QStandarditem全局协调?

用户界面文件

QWidget *MY_QWidget;
MY_QTableView *MY_QTableView;
MY_QWidget = new QWidget();
MY_QTableView = new QTableView(MY_QWidget);

h 文件

QStandardItemModel * MY_QStandardItemModel;

cpp 文件

MY_QStandardItemModel = new QStandardItemModel();
ui->MY_QTableView->setModel(MY_QStandardItemModel);

QStandardItem *MY_QStandardItem;
MY_QStandardItem  = new QStandardItem(tr("some text"));
MY_QStandardItemModel->setItem(0,0,MY_QStandardItem);// <- Global position of this item                                                                                   

【问题讨论】:

    标签: c++ qt


    【解决方案1】:

    要获取QStandardItem 的本地位置,您可以使用QAbstractItemView::visualRect

    QModelIndex index = MY_QStandardItemModel->indexFromItem(MY_QStandardItem);
    QRect rect = MY_QTableView->visualRect(index);
    Qpoint localPoint = rect.topLeft(); // <- the local position
    

    为了获得全球定位,您可以使用QWidget::mapToGlobal:

    Qpoint globalPosition MY_QTableView->mapToGlobal(localPoint); // <- the global position
    

    【讨论】:

      猜你喜欢
      • 2019-11-16
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 2016-04-20
      • 2015-06-26
      • 1970-01-01
      • 2010-11-05
      • 2016-06-23
      相关资源
      最近更新 更多