注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

沙漠里de烟雨

原创分享,禁止转载

 
 
 

日志

 
 

Qt---设置QTableView  

2013-12-29 18:07:41|  分类: QT5.x与QML |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
m_Delegate = new TableItemDelegate(m_ColumnMap);
m_TableModel = new TableModel(itemList,m_ColumnMap,this);

this->setModel(m_TableModel);
this->setItemDelegate(m_Delegate); //在单元格中显示图片;
this->setHorizontalHeader(new TradeHeaderView(m_ColumnMap,Qt::Horizontal,this));//设置列标题;

this->setSelectionBehavior(QAbstractItemView::SelectItems);//选择一项;

this->verticalHeader()->hide();  //隐藏垂直方向上的标头; //this->verticalHeader()->setVisible(false);
this->setSelectionMode(QAbstractItemView::SingleSelection);//只能选择一个单元格;
this->setEditTriggers(QAbstractItemView::NoEditTriggers); //不能编辑
this->setContextMenuPolicy(Qt::CustomContextMenu);

this->resizeColumnsToContents(); //列宽随列标题内容而缩放,下类同;
this->resizeRowsToContents();

this->setAlternatingRowColors(true);  //交替行颜色; 
QString tableBgStyle = "QHeaderView::section{height:28px;background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop: 0 #eeeeee, stop: 1 #555555);color: black;padding-left: 4px;border: 1px solid #6c6c6c;}"
  "QTableView{font-size:18px; color:black; background-color:white;alternate-background-color:rgb(192,192,192);}";
this->setStyleSheet(tableBgStyle);

this->setMouseTracking(true); //鼠标经过时高亮显示;  //需要重写mouseMoveEvent()
this->setContextMenuPolicy(Qt::ActionsContextMenu); //可以有弹出式菜单;需要在mousePressEvent()中增加菜单;

void TradeTable::mouseMoveEvent(QMouseEvent * event)
{
QModelIndex index = indexAt(event->pos());
if(index.isValid() && index.row()>=0)
this->setSelection(this->visualRect(index),QItemSelectionModel::SelectCurrent);
}
void TradeTable::mousePressEvent(QMouseEvent * event)
{  
QModelIndex modelIndex = indexAt(event->pos());
if (event->button() == Qt::RightButton)
{
QMenu * popupMenu = new QMenu(this);
QString menuStyle = "QMenu { font-size:18px; color:blue; background-color:qlineargradient(x1:0, y1:0, x2:0, y2:1, stop: 0 #cccccc, stop: 1 #555555);}" ;
popupMenu->setStyleSheet(menuStyle);
m_SelectedIndex = modelIndex.row();
if(modelIndex.isValid() && modelIndex.row()>=0)
{
this->setSelection(this->visualRect(modelIndex),QItemSelectionModel::SelectCurrent);
popupMenu->addAction(tr("&Order"), this, SLOT(Order()),QKeySequence(Qt::CTRL + Qt::Key_O));
popupMenu->addAction(tr("&Chart"),this,SLOT(Chart()),QKeySequence(Qt::CTRL + Qt::Key_C));
popupMenu->addSeparator();
popupMenu->addAction(tr("&Delete"),this,SLOT(Delete()),QKeySequence(Qt::CTRL + Qt::Key_D));
popupMenu->addAction(tr("&Add"),this,SLOT(Add()),QKeySequence(Qt::CTRL + Qt::Key_A));
popupMenu->addAction(tr("&Move&Up"),this,SLOT(MoveUp()),QKeySequence(Qt::CTRL + Qt::Key_M,Qt::CTRL + Qt::Key_U));
popupMenu->addAction(tr("&UpTo&Top"),this,SLOT(UpToTop()),QKeySequence(Qt::CTRL + Qt::Key_U,Qt::CTRL + Qt::Key_T));
popupMenu->addAction(tr("&Move&Down"),this,SLOT(MoveDown()),QKeySequence(Qt::CTRL + Qt::Key_M,Qt::CTRL + Qt::Key_D));
popupMenu->addAction(tr("&DownTo&Bottom"),this,SLOT(DownToBottom()),QKeySequence(Qt::CTRL + Qt::Key_D,Qt::CTRL + Qt::Key_B));
}
else
{
popupMenu->addAction(tr("&Add"),this,SLOT(Add()),QKeySequence(Qt::CTRL + Qt::Key_A));
}
popupMenu->exec(event->globalPos());  
}
QTableView::mousePressEvent(event);
}

  评论这张
 
阅读(2831)| 评论(0)
推荐

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018