Qt model 顯示設定(居中靠右)、表頭、自定義排序
阿新 • • 發佈:2018-11-10
設定:顯示居中、靠左
QVariant SoloDccDataSegmentInfoTableModel::data(const QModelIndex &index, int role) const
{
if (role == Qt::TextAlignmentRole)
{
return int(Qt::AlignRight | Qt::AlignVCenter); //按需調整
}
}
表頭設定:
QVariant SoloDccAdsrLogDataTableModelBattery::headerData(int section, Qt::Orientation orientation, int role) const { if (section >= 0 && section < C_NUM) { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { const char* header_title[C_NUM] = { "IGU SN#","Duration Time" }; return header_title[section]; } } if (orientation == Qt::Vertical && role == Qt::DisplayRole) return section+1; if (role == Qt::TextAlignmentRole){ return Qt::AlignCenter; } return QVariant(); }
排序:
void SoloDccTestResultsModel::sort(int column, Qt::SortOrder order) { if (column > TEST_RESULTS_COLUME_INDEX) return; class ProjectInfoSort { public: ProjectInfoSort(int column, Qt::SortOrder order) : mColumn(column) , mSortOrder(order) {} bool operator()(const SoloAdsrLog::AdsrSummaryDataRecord& info1, const SoloAdsrLog::AdsrSummaryDataRecord& info2) { bool is_less = false; switch (mColumn) { case ADSR_SN_COLUME_INDEX: is_less = info1.adsrSN < info2.adsrSN;; break; case BEGIN_TIME_COLUME_INDEX: is_less = info1.startTime < info2.startTime;; break; case END_TIME_COLUME_INDEX: is_less = info1.endTime < info2.endTime; break; case DEVICE_TYPE_COLUME_INDEX: is_less = info1.deviceType < info2.deviceType; break; case TEST_RESULTS_COLUME_INDEX: is_less = info1.testResult < info2.testResult; break; } if (mSortOrder == Qt::DescendingOrder) { is_less = !is_less; } return is_less; } private: int mColumn; Qt::SortOrder mSortOrder; }; ProjectInfoSort sorter(column, order); qSort(m_testResult_List.begin(), m_testResult_List.end(), sorter); dataChanged(index(0, 0), index(m_testResult_List.size() - 1, TEST_RESULTS_COLUME_INDEX - 1)); }