下载qtcreator的源代码编译cplusplus、utils、languageUtils这三个项目,使用cplusplus中的OverviewModel类和src\plugins\cppeditor下的OverviewTreeView类,使用这两类来组合显示出来c++头文件中所有的类和函数声明以及相关信息。我写的测试代码如下:
#include "OverviewView.h"
#include "cplusplus/OverviewModel.h"
#include <QTreeView>
#include <QComboBox>
#include <QMouseEvent>
class OverviewTreeView : public QTreeView
{
public:
OverviewTreeView(QWidget *parent = 0)
: QTreeView(parent)
{
// TODO: Disable the root for all items (with a custom delegate?)
setRootIsDecorated(false);
}
void sync()
{
expandAll();
}
void adjustWidth()
{
//const int w = Core::ICore::mainWindow()->geometry().width();
setMaximumWidth(1243);
setMinimumWidth(qMin(qMax(sizeHintForColumn(0), minimumSizeHint().width()), 1000));
}
};
class OverviewCombo : public QComboBox
{
public:
OverviewCombo(QWidget *parent = 0) : QComboBox(parent), m_skipNextHide(false)
{}
bool eventFilter(QObject* object, QEvent* event)
{
if (event->type() == QEvent::MouseButtonPress && object == view()->viewport()) {
QMouseEvent* mouseEvent = static_cast<QMouseEvent*>(event);
QModelIndex index = view()->indexAt(mouseEvent->pos());
if (!view()->visualRect(index).contains(mouseEvent->pos()))
m_skipNextHide = true;
}
return false;
}
void showPopup()
{
static_cast<OverviewTreeView *>(view())->adjustWidth();
QComboBox::showPopup();
}
virtual void hidePopup()
{
if (m_skipNextHide)
m_skipNextHide = false;
else
QComboBox::hidePopup();
}
private:
bool m_skipNextHide;
};
OverviewView::OverviewView(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
//QTableView *tv = ui.tableView;
OverviewTreeView *tv = new OverviewTreeView;
CPlusPlus::OverviewModel *m_outlineModel = new CPlusPlus::OverviewModel;
//qsettings1.h
QString docFile = "G:\\map\\CocosMapEditor\\qsettings1.h";
QFile file(docFile);
file.open(QFile::ReadOnly);
QByteArray ba = file.readAll();
QString fileContent (ba);
CPlusPlus::Document::Ptr cdPtr = CPlusPlus::Document::create("");
cdPtr->setUtf8Source(ba);
cdPtr->parse();
cdPtr->check();
//m_outlineModel->rebuild(cdPtr);
//tv->setModel(m_outlineModel);
//--------------------------------------------------
//OverviewCombo *oc = new OverviewCombo(this);
//oc->setView(tv);
//tv->setModel(NULL);
//m_outlineModel->rebuild(cdPtr);
//tv->setModel(m_outlineModel);
//tv->expandAll();
//tv->show();
OverviewCombo *oc = new OverviewCombo(this);
oc->setView(tv);
//oc->show();
tv->setModel(NULL);
m_outlineModel->rebuild(cdPtr);
oc->setModel(m_outlineModel);
tv->expandAll();
oc->show();
}
OverviewView::~OverviewView()
{
}
核心代码如下:
CPlusPlus::Document::Ptr cdPtr = CPlusPlus::Document::create("");
cdPtr->setUtf8Source(ba);
cdPtr->parse();
cdPtr->check();
qtcreator下载地址:
http://sourceforge.jp/projects/sfnet_qtcreator.mirror/releases/