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