创建视图:
QHBoxLayout* pHLay = new QHBoxLayout(this);m_pLeftTree = new QTreeView(this);m_pLeftTree->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不可编辑m_pLeftTree->setFixedWidth(300);
创建模型和模型项:
m_pLeftTree->setFrameShape(QFrame::NoFrame);QStandardItemModel* model = new QStandardItemModel(m_pLeftTree);model->setHorizontalHeaderLabels(QStringList() << "draw all");QStandardItem* pParentItem = NULL;QStandardItem* pChildItem = NULL;// 点pParentItem = new QStandardItem(QIcon(":/resources/point.png"), "draw ponit");model->appendRow(pParentItem);//将父项加入ModelpChildItem = new QStandardItem(QIcon(":/resources/point.png"), "point");pParentItem->appendRow(pChildItem);//将子项加入父节点pChildItem = new QStandardItem(QIcon(":/resources/multipoints.png"), "multipoints");pParentItem->appendRow(pChildItem);// 线pParentItem = new QStandardItem(QIcon(":/resources/line.png"), "draw line");model->appendRow(pParentItem);pChildItem = new QStandardItem(QIcon(":/resources/line.png"), "line");pParentItem->appendRow(pChildItem);pChildItem = new QStandardItem(QIcon(":/resources/arc.png"), "arc");pParentItem->appendRow(pChildItem);// 封闭的图形pParentItem = new QStandardItem(QIcon(":/resources/rect.png"), "draw rect");model->appendRow(pParentItem);pChildItem = new QStandardItem(QIcon(":/resources/rect.png"), "rect");pParentItem->appendRow(pChildItem);pChildItem = new QStandardItem(QIcon(":/resources/roundrect.png"), "roundrect");pParentItem->appendRow(pChildItem);pChildItem = new QStandardItem(QIcon(":/resources/chord.png"), "chord");pParentItem->appendRow(pChildItem);pChildItem = new QStandardItem(QIcon(":/resources/ellipse.png"), "ellipse");pParentItem->appendRow(pChildItem);// 任意路径绘制pParentItem = new QStandardItem(QIcon(":/resources/polygon.png"), "draw polygon");model->appendRow(pParentItem);pChildItem = new QStandardItem(QIcon(":/resources/polygon.png"), "polygon");pParentItem->appendRow(pChildItem);pChildItem = new QStandardItem(QIcon(":/resources/polyline.png"), "polyline");pParentItem->appendRow(pChildItem);pChildItem = new QStandardItem(QIcon(":/resources/ConvexPloygon.png"), "ConvexPloygon");pParentItem->appendRow(pChildItem);pChildItem = new QStandardItem(QIcon(":/resources/lines.png"), "lines");pParentItem->appendRow(pChildItem);pChildItem = new QStandardItem(QIcon(":/resources/path.png"), "path");pParentItem->appendRow(pChildItem);pChildItem = new QStandardItem(QIcon(":/resources/pie.png"), "pie");pParentItem->appendRow(pChildItem);// 图片绘制pParentItem = new QStandardItem(QIcon(":/resources/image.png"), "draw image");model->appendRow(pParentItem);pChildItem = new QStandardItem(QIcon(":/resources/image.png"), "image");pParentItem->appendRow(pChildItem);pChildItem = new QStandardItem(QIcon(":/resources/pixmap.png"), "pixmap");pParentItem->appendRow(pChildItem);// 文本绘制pParentItem = new QStandardItem(QIcon(":/resources/text.png"), "draw text");model->appendRow(pParentItem);// 擦除pParentItem = new QStandardItem(QIcon(":/resources/erase.png"), "draw erase");model->appendRow(pParentItem);// 路径填充pParentItem = new QStandardItem(QIcon(":/resources/fillpath.png"), "draw fillpath");model->appendRow(pParentItem);// 矩形填充pParentItem = new QStandardItem(QIcon(":/resources/fillrect.png"), "draw fillrect");model->appendRow(pParentItem);
效果如下: