QTreeWidget里嵌套表格QTableView
InformationPositionSubTableView::InformationPositionSubTableView(QStringList& columnNameList,QList<PositionItem>& positionItemList,QWidget *parent) : m_ColumnNameList(columnNameList), m_PositionItemList(positionItemList),QTableView(parent)
{
m_InformationPositionSubTableModel = new InformationPositionSubTableModel(columnNameList,positionItemList);
this->setModel(m_InformationPositionSubTableModel); this->setMouseTracking(true);
this->setFocusPolicy(Qt::NoFocus);
this->setGridStyle(Qt::NoPen);
this->setSelectionBehavior(QAbstractItemView::SelectItems);
this->setSelectionMode(QAbstractItemView::SingleSelection); QHeaderView* headerView = new InformationPositionSubHeaderview(Qt::Horizontal,this);
headerView->setSectionsClickable(true);
this->setHorizontalHeader(headerView);
this->horizontalHeader()->setStretchLastSection(true);
this->verticalHeader()->setVisible(false); this->setEditTriggers(QAbstractItemView::NoEditTriggers);
this->setContextMenuPolicy(Qt::CustomContextMenu); this->resizeColumnsToContents();
// this->resizeRowsToContents(); //不能加上,否则做为QTreeWidget的子控件会多出一行的间隙; this->setFrameShape(QFrame::NoFrame); //设置成无边框; this->horizontalHeader()->setStyleSheet("QHeaderView{background-color:gray;}"); //设置表头背景为灰色(这样也看起来扁平);
} InformationPositionSubTableView::~InformationPositionSubTableView()
{ } void InformationPositionSubTableView::mouseMoveEvent(QMouseEvent * event)
{
QModelIndex index = indexAt(event->pos());
if(index.isValid() && index.row()>=)
{
this->setSelection(visualRect(index),QItemSelectionModel::SelectCurrent);
} } ======== InformationPositionSubTableModel::InformationPositionSubTableModel(QStringList& columnNameList,QList<PositionItem>& positionItemList,QObject *parent) : m_ColumnNameList(columnNameList),m_PositionItemList(positionItemList),QAbstractTableModel(parent)
{
this->m_ColumnNameList = columnNameList;
} InformationPositionSubTableModel::~InformationPositionSubTableModel()
{ }
QVariant InformationPositionSubTableModel::data(const QModelIndex & index, int role) const
{
Q_UNUSED(index);
if(!index.isValid())
return QVariant(); if(role==Qt::DisplayRole)
{
PositionItem item = m_PositionItemList[index.row()];
int column = index.column();
switch (column)
{
case :
return item.GetAccountName();
case :
return item.GetBuyLot();
case :
return item.GetAvgBuyPrice();
case :
return item.GetSellLot();
case :
return item.GetAvgSellPrice();
case :
return item.GetNet();
default:
return item.GetFloatingMap()[headerData(column,Qt::Horizontal,Qt::DisplayRole).toString()];
}
} if(role==Qt::TextAlignmentRole)
{
return Qt::AlignCenter;
} if(role==Qt::TextColorRole)
{
if(index.column()== || index.column()==)
{
return QColor(Qt::red);
}
if(index.column()==)
{
return QColor(Qt::green);
}
} return QVariant();
}
QVariant InformationPositionSubTableModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(role!=Qt::DisplayRole)
return QVariant(); if(orientation == Qt::Horizontal)
{
return m_ColumnNameList[section];
} return QVariant();
}
bool InformationPositionSubTableModel::setHeaderData(int section, Qt::Orientation orientation, const QVariant & value, int role)
{
if(role!=Qt::EditRole)
return false;
if(orientation == Qt::Horizontal && section >=)
{
m_ColumnNameList.append(value.toString());
emit headerDataChanged(orientation,section,section);
return true;
}
return false;
} int InformationPositionSubTableModel::rowCount(const QModelIndex & parent) const
{
int rows = m_PositionItemList.count();
return rows;
}
int InformationPositionSubTableModel::columnCount(const QModelIndex & parent) const
{
return m_ColumnNameList.count();
}
bool InformationPositionSubTableModel::setData(const QModelIndex & index, const QVariant & value, int role)
{
Q_UNUSED(index);
if (index.isValid() && role == Qt::DisplayRole)
{
int row = index.row();
m_PositionItemList.replace(row, m_UpdatingItem);
emit(dataChanged(index, index));
}
return true;
}
Qt::ItemFlags InformationPositionSubTableModel::flags(const QModelIndex & index) const
{
Q_UNUSED(index);
if (!index.isValid())
return Qt::ItemIsEditable; return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
} bool InformationPositionSubTableModel::insertRow(int position,const QModelIndex &index)
{
Q_UNUSED(index);
beginInsertRows(index, position, position); int count = m_UpdatingItem.GetFloatingMap().count();
QMapIterator<QString,float> it(m_UpdatingItem.GetFloatingMap());
while(it.hasNext())
{
it.next();
this->AddHeaderSection(it.key());
} m_PositionItemList.insert(position,m_UpdatingItem);
endInsertRows();
return true;
}
bool InformationPositionSubTableModel::removeRow(int position,const QModelIndex &index)
{
Q_UNUSED(index);
beginRemoveRows(index, position, position);
m_PositionItemList.removeAt(position);
endRemoveRows();
return true;
}
void InformationPositionSubTableModel::AddHeaderSection(const QString& columnName)
{
if(!m_ColumnNameList.contains(columnName))
{
setHeaderData(m_ColumnNameList.count(),Qt::Horizontal,Qt::EditRole);
m_ColumnNameList.append(columnName);
}
} ===== InformationPositionSubHeaderview::InformationPositionSubHeaderview(Qt::Orientation orientation,QWidget* parent) : QHeaderView(orientation,parent)
{
this->setSectionsClickable(false);
this->m_IsHided = false;
} InformationPositionSubHeaderview::~InformationPositionSubHeaderview()
{
} void InformationPositionSubHeaderview::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
painter->save();
QHeaderView::paintSection(painter, rect, logicalIndex);
painter->restore(); if(logicalIndex== && m_IsHided==false)
{
painter->drawText(rect,Qt::AlignRight|Qt::AlignVCenter, "<<");
} if(logicalIndex== && m_IsHided==true)
{
QRect leftRect = QRect(rect.x(),rect.y(),HIDEDCOLORBTNWIDTH,rect.height());
painter->setBrush(QBrush(Qt::blue));
painter->drawRect(leftRect);
} } void InformationPositionSubHeaderview::mousePressEvent(QMouseEvent * event)
{
if(event->button()==Qt::LeftButton)
{
QFontMetrics fm(this->font());
int textWidth = fm.width("<<"); int index = logicalIndexAt(event->pos());
int xPos =event->pos().x();
int xLeft = sectionPosition(index)+sectionSize(index);
if(index== && m_IsHided==false && (xPos<xLeft-HIDEDCOLORBTNWIDTH && xPos>xLeft-textWidth) )
{
setSectionHidden(index,true);
m_IsHided = true;
}
else if(index== && m_IsHided==true && xPos<=HIDEDCOLORBTNWIDTH)
{
setSectionHidden(index-,false);
m_IsHided = false;
}
}
QHeaderView::mousePressEvent(event);
} ====== InformationPositionTreeView::InformationPositionTreeView(InformationPositionItem& infoItem,QWidget *parent) : QTreeWidget(parent)
{
m_SubColumnNameList = infoItem.GetColumnNameList();
m_SubColumnNameList.removeAt();
m_SubColumnNameList.replace(,"Account ");//多出四个空格,是为"<<"腾出空间; this->Init();
this->InitPositionItemListMap(infoItem); int columnNameCount = infoItem.GetColumnNameList().count();
this->setHeaderLabels(infoItem.GetColumnNameList()); //增加标题; int count = m_InstrumentStringList.count();
for(int i=;i<count;i++)
{
QList<PositionItem> positionItemList = m_PositionItemListMap[m_InstrumentStringList[i]];
QStringList summaryList;
summaryList << "" << (m_InstrumentStringList[i]) << QString("%0").arg(GetSumValue(positionItemList,"buy")) << QString("%0").arg(GetSumValue(positionItemList,"avgbuy")) << QString("%0").arg(GetSumValue(positionItemList,"sell")) << QString("%0").arg(GetSumValue(positionItemList,"avgsell")) << QString("%0").arg(GetSumValue(positionItemList,"net")) ;
QTreeWidgetItem* node = new QTreeWidgetItem(summaryList);
this->addTopLevelItem(node); InformationPositionSubTableView* subTableView = new InformationPositionSubTableView(m_SubColumnNameList,m_PositionItemListMap[m_InstrumentStringList[i]],this);
QTreeWidgetItem* subItem = new QTreeWidgetItem;
node->addChild(subItem);
setFirstItemColumnSpanned(subItem,true);
setItemWidget(subItem,,subTableView); int rowHeight = subTableView->verticalHeader()->defaultSectionSize();
node->setSizeHint(i,QSize(rowHeight,rowHeight)); /////////////--附注:
如果要让子tableview的宽度随着行数的变化而变化,却不会出现滚动条的话,应先将其滚动条关闭
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
然后在此treeWidget中,
int subTableItemHeight= subTableView->rowHeight();
int height = subTableItemHeight+subTableItemHeight*m_PositionItemListMap[m_PositionItemListMap.keys()[subIndex]].count();//算出子table的高度;
subTableView->setMaximumHeight(height);
subItem->setSizeHint(,QSize(subTableView->size().width(),height));//改变树节点的宽度
//这样,子表就能动态变化而不会高度太大或太小。
如果动态变化后发现要出现大小 变化后才能正常显示,可能如下
topLevelItem(parentIndex)->setExpanded(false);
topLevelItem(parentIndex)->setExpanded(true);
即先收起子Treewidgetitem,然后再展开。这只是一种强制其更新的方法。
//////////////// node->setIcon(,QIcon(QPixmap("Fold.png")));
m_IsFoldedList.append(true); int id = ;
while(id<columnNameCount)
node->setTextAlignment(id++,Qt::AlignCenter); }
connect(this,SIGNAL(itemClicked(QTreeWidgetItem*,int)),this,SLOT(ChangeIconSlot(QTreeWidgetItem*,int))); } InformationPositionTreeView::~InformationPositionTreeView()
{ }
void InformationPositionTreeView::mouseMoveEvent(QMouseEvent * event)
{
QModelIndex index = indexAt(event->pos());
if(index.isValid() && index.row()>=)
{
this->setSelection(visualRect(index),QItemSelectionModel::SelectCurrent);
} }
void InformationPositionTreeView::ChangeIconSlot(QTreeWidgetItem * item, int column)
{
QIcon icon;
bool isNeedExpanded = true;
if(column== && item->parent()==nullptr)
{
if(m_IsFoldedList[column])
{
icon = QIcon(QPixmap("UnFold.png"));
isNeedExpanded = true;
}
else
{
icon = QIcon(QPixmap("Fold.png"));
isNeedExpanded = false;
}
item->setIcon(column,icon);
m_IsFoldedList.replace(column,!m_IsFoldedList[column]);
item->setExpanded(isNeedExpanded);
}
}
void InformationPositionTreeView::Init()
{
// this->setMouseTracking(true);
this->setFocusPolicy(Qt::NoFocus);
this->setSelectionBehavior(QAbstractItemView::SelectRows);
this->setSelectionMode(QAbstractItemView::SingleSelection); this->setEditTriggers(QAbstractItemView::NoEditTriggers);
this->setContextMenuPolicy(Qt::CustomContextMenu); QHeaderView* headerView = new QHeaderView(Qt::Horizontal,this);
this->setHeader(headerView);
headerView->setStretchLastSection(true);
headerView->setDefaultAlignment(Qt::AlignCenter);
headerView->setSectionsClickable(true);
headerView->setSectionResizeMode(QHeaderView::ResizeToContents); this->setStyleSheet("QTreeView::branch {image:none;}");
// this->setRootIsDecorated(false);
// this->setItemDelegateForColumn(0,);
}
void InformationPositionTreeView::InitPositionItemListMap(InformationPositionItem& infoItem)
{
QList<PositionItem> positionItemList = infoItem.GetPositionItemList();
int count = positionItemList.count();
for(int i=;i<count;i++)
{
m_InstrumentStringList << positionItemList[i].GetInstrument();
} m_InstrumentStringList.removeDuplicates();
int keyCount = m_InstrumentStringList.count();
for(int i=;i<keyCount;i++)
{
QList<PositionItem> value;
for(int j=;j<count;j++)
{
if(positionItemList[j].GetInstrument()==m_InstrumentStringList[i])
value.append(positionItemList[j]);
}
m_PositionItemListMap.insert(m_InstrumentStringList[i],value);
} }
float InformationPositionTreeView::GetSumValue(const QList<PositionItem>& positionItemList,const QString& itemName)
{
float sum = 0.0;
int count = positionItemList.count();
//..... return 2.01;
} ====== InformationPositionTreeModel::InformationPositionTreeModel(InformationPositionItem& infoItem,QObject * parent):m_InformationPositionItem(infoItem),QAbstractItemModel(parent)
{ } InformationPositionTreeModel::~InformationPositionTreeModel(void)
{
} int InformationPositionTreeModel::rowCount(const QModelIndex &parent) const
{
return m_InformationPositionItem.GetPositionItemList().count();
}
int InformationPositionTreeModel::columnCount(const QModelIndex &parent) const
{
return m_InformationPositionItem.GetColumnNameList().count();
}
QVariant InformationPositionTreeModel::data(const QModelIndex &index, int role) const
{
if(!index.isValid())
return QVariant(); if(role==Qt::DisplayRole)
{
PositionItem item = m_InformationPositionItem.GetPositionItemList()[index.row()];
switch (index.column())
{
case :
return "";
case :
return item.GetInstrument();
case :
return item.GetBuyLot();
case :
return item.GetAvgBuyPrice();
case :
return item.GetSellLot();
case :
return item.GetAvgSellPrice();
case :
return item.GetNet();
default:
return item.GetFloatingMap()[headerData(index.column(),Qt::Horizontal,Qt::DisplayRole).toString()];;
}
}
if(role==Qt::DecorationRole && index.column()==)
{
return QIcon(QPixmap("Fold.png"));
}
if(role==Qt::TextAlignmentRole)
{
Qt::AlignCenter;
}
return QVariant();
}
QVariant InformationPositionTreeModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if(orientation == Qt::Horizontal && role == Qt::DisplayRole)
{
return m_InformationPositionItem.GetColumnNameList()[section];
}
return QVariant();
}
Qt::ItemFlags InformationPositionTreeModel::flags(const QModelIndex &index) const
{
if (!index.isValid())
return Qt::ItemIsSelectable; return QAbstractItemModel::flags(index) | Qt::ItemIsSelectable;
}
bool InformationPositionTreeModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
if(!index.isValid())
return false;
return false;
} QModelIndex InformationPositionTreeModel::parent(const QModelIndex & index) const
{
if(index.isValid())
return index.parent();
return QModelIndex();
}
QModelIndex InformationPositionTreeModel::index(int row, int column, const QModelIndex & parent) const
{
if(parent.isValid())
return parent.child(row,column);
return QModelIndex();
}
QTreeWidget里嵌套表格QTableView的更多相关文章
- Java 操作Word表格——创建嵌套表格、添加/复制表格行或列、设置表格是否禁止跨页断行
本文将对如何在Java程序中操作Word表格作进一步介绍.操作要点包括 如何在Word中创建嵌套表格. 对已有表格添加行或者列 复制已有表格中的指定行或者列 对跨页的表格可设置是否禁止跨页断行 创建表 ...
- elementUI表单嵌套表格并对每行进行校验
elementUI表单嵌套表格并对每行进行校验 elementUI 表单嵌套表格并进行校验. 目录 效果展示 代码链接 关键代码 完整代码 效果展示 先看看这是不是需要的效果^_^ 如图,Elem ...
- html嵌套表格示例
常用嵌套表格示例,出自<网页开发手记:HTML+CSS+JavaScript实战详解> <html> <head> <title>嵌套表格布 ...
- div里嵌套了img底部会有白块问题和图片一像素问题解决
div里嵌套了img底部会有白块 因为img默认是按基线(baseline)对齐的.对比一下图片和右边的p, q, y等字母,你会发现这三个字母的“小尾巴”和图片下方的空白一样高.下面这张图中的黑线就 ...
- 跟我一起玩转FineUI之嵌套表格
最近一直在研究FineUI(http://www.fineui.com/),那么什么是FineUI呢,FineUI是基于 ExtJS 的专业 ASP.NET 控件库.创建 No JavaScript, ...
- 火狐下<a>标签里嵌套的<select>不能选的bug
今天遇到了这个问题,网上一找就找到原因了:在狐火下<a>标签里嵌套的<select>不能选 可是我查找这个问题过程中依然饶了一些时间,原因是在<a>标签没有写hre ...
- RecyclerFullyManagerDemo【ScrollView里嵌套Recycleview的自适应高度功能】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 对于Recyclerview自己的LinearLayoutManager和GridLayoutManager,在版本23.2.0之后 ...
- C# 绘制PDF嵌套表格
嵌套表格,即在一张表格中的特定单元格中再插入一个或者多个表格,使用嵌套表格的优点在于能够让内容的布局更加合理,同时也方便程序套用.下面的示例中,将介绍如何通过C#编程来演示如何插入嵌套表格到PDF文档 ...
- [Android] (在ScrollView里嵌套view)重叠view里面的onTouchEvent的调用方法
在我前面的自定义裁剪窗口的代码中,我把裁剪的view放在了大的scrollview里,这样就出现了程序只能触发scrollview,无法操作我的裁剪窗口.所以我加了那篇博客下面最后两段代码.其实我遇到 ...
随机推荐
- 【一个比较bug free的二分写法】
lower_bound: [l, r)区间内大于等于val的第一个位置 int lower_bound(int l, int r, int val){ while(l < r){ ); if(a ...
- python百度贴吧爬虫
# -*- coding: utf-8 -*- #coding=utf-8 import urllib import urllib2 import re import thread import ti ...
- get****Context各个方法分析
1 getApplicationContext分析 该方法为contextImpl类的方法,返回一个ApplicationContext.方法代码为: public Context getApplic ...
- vim 编辑器的使用
相信一个linux运维人员不可能不知道vim ,下面我们一起来学习vim的日常操作吧.(不要追求多,工作中用到了再去学也不迟.) 1.vim 的几种模式 *正常模式:快捷键or命令行操作 *插入模式: ...
- react中对props.children进行操作
之前写的更多的时候是直接使用简写 {props.children} 这样就可以了,但是当有时候需要对传入的子组件进行一些操作的时候需要用到React.Children方法 {React.Childre ...
- Bzoj1879 [Sdoi2009]Bill的挑战
Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 724 Solved: 363 Description Input 本题包含多组数据. 第一行:一个整数T ...
- 时间戳/Date(1354116249000)/ 转换
从C#的Datatime格式通过Json传到Js里面,时间会显示成时间戳/Date(1354116249000)/ ,js转化如下 function GetDateFormat(str) { retu ...
- 语法错误: 标识符“__RPC__out_xcount_part” 解决方法
1.错误描述 2.解决方案:将 $(DXSDK_DIR)\Include; 放到最后面,如下
- android基本控件学习-----SeekBar&RatingBar
SeekBar(拖动条)和RatingBar(星级评分条)讲解 一.SeekBar(拖动条) (1)拖动条简单理解就是可以拖动的线,这个对我们来讲很常见,比如视频播放或者音乐播放我们拖动播放的进度,下 ...
- Synchronized 实现原理
记得刚刚开始学习Java的时候,一遇到多线程情况就是synchronized.对于当时的我们来说,synchronized是如此的神奇且强大.我们赋予它一个名字"同步",也成为我们 ...