修改后的代码如下:
QDomDocument* domdoc = m_IChildViewer->getTOC(); if (domdoc == NULL)
return;
// 创建大纲树形视图 QTreeView* treeView = new QTreeView(); m_OutlineLayout->addWidget(treeView); treeView->setStyleSheet( “QTreeView::item:hover{background-color:rgb(0,255,0,50)}”
"QTreeView::item:selected{background-color:rgb(255,0,0,100)}");
// 创建大纲树形模型并绑定到视图 TocTreeModel* treeModel = new TocTreeModel(domdoc); treeView->setModel(treeModel);
// 监听双击事件,实现跳转功能 connect(treeView, &QTreeView::doubleClicked, ={
// 获取节点的位置信息
int position = treeModel->getPosition(index);
if (position < 0)
return;
// 跳转到对应的位置
QTextCursor cursor = m_TextEdit->textCursor();
cursor.setPosition(position);
m_TextEdit->setTextCursor(cursor);
});
// 展开所有节点 treeView->expandAll(); treeView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);