本文介绍: 【代码】Qt手写ListView

创建视图

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);//将父项加入Model

	pChildItem = 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);

效果如下:

原文地址:https://blog.csdn.net/weixin_38241876/article/details/134605636

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_744.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注