演示如何使用QTableView显示列表

Author:kagula@20150415

测试环境:vs2010sp1+qt541

调用者-初始化的时候

TestListTable::TestListTable(QWidget *parent)
	: QMainWindow(parent)
{
	ui.setupUi(this);
	
	//connect controls with process!
	connect(ui.pbChangeCellValue, SIGNAL(released()), this, SLOT(OnChangeCellValue()));
	connect(ui.pbGrow, SIGNAL(released()), this, SLOT(OnAddRow()));
	connect(ui.pbShrinkColumns, SIGNAL(released()), this, SLOT(OnRemoveRow()));

	//sort by user click table header title!
	QHeaderView *headerGoods = ui.tableView->horizontalHeader();
	//SortIndicator为水平标题栏文字旁边的三角指示器
	headerGoods->setSortIndicator(0, Qt::AscendingOrder);
	headerGoods->setSortIndicatorShown(true);
	connect(headerGoods, SIGNAL(sectionClicked(int)), ui.tableView, SLOT (sortByColumn(int)));

	//Note:below statement which set default sort will not take effect,only you can do is modify the model directly!
	//ui.tableView->sortByColumn(1,Qt::SortOrder::DescendingOrder);
	
	//i have not test.
	//ui.tableView->setAlternatingRowColors(true);

	//set model. the correlation from model to view.
	ui.tableView->setModel(&_myTable);

	//custom cell background color in table
	ui.tableView->setItemDelegate(new ColorDelegate(ui.tableView));

	//change select mode is single row selection. default is single cell selection.
	ui.tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
	ui.tableView->setSelectionMode(QAbstractItemView::SingleSelection);//if no this invoke, user can select multi-rows.

	//hide first anonymous column. by default is show in table.
	ui.tableView->verticalHeader()->hide();

	//set column width in tableView of QTableView.
	ui.tableView->setColumnWidth(0,32);

	//set row height in tableView of QTableView.
	QHeaderView *verticalHeader = ui.tableView->verticalHeader();
	verticalHeader->sectionResizeMode(QHeaderView::Fixed);
	verticalHeader->setDefaultSectionSize(64);

	//set do not show grid line. default have gray grid line.
	ui.tableView->setShowGrid(false);

	//Using QSS for Custom QHeaderView!
	//《Can headeview stylesheet style just the selected section?》
	//http://www.qtforum.org/article/28848/can-headeview-stylesheet-style-just-the-selected-section.html
	//《Qt Style Sheets Examples》
	//http://qt.developpez.com/doc/4.6/stylesheet-examples/
	//ui.tableView->setStyleSheet("QHeaderView::section:horizontal {margin-right: 0; background-color: white;color: blue;font: 10pt \"MS Shell Dlg 2\";padding-left: 4px;border: 1px solid #6c6c6c;}");

	//if you do not like using QSS, you can derive QHeaderView class to custom your table header.
	//MyHeaderView *pHeaderView = new MyHeaderView(Qt::Orientation::Horizontal);
	//pHeaderView->setFixedHeight(32);
	//ui.tableView->setHorizontalHeader(pHeaderView);

	//set grid line style!
	//ui.tableView->setGridStyle(Qt::PenStyle::NoPen);

	/*
	//change default sel
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值