table_widget->setColumnCount(4); //设置列数
table_widget->horizontalHeader()->setDefaultSectionSize(150);
table_widget->horizontalHeader()->setClickable(false); //设置表头不可点击(默认点击后进行排序)
//设置表头内容
QStringList header;
header<
void RefreshListViewHeader(CListCtrl *pListCtrl,CStringArray m_arrHeaderLabel,
long* col_wide_arr)
{
if(pListCtrl==NULL)
return;
int i, NUM_COLUMNS, nColumnCount;
CHeaderCtrl *pHeader = pListCtrl->GetHeaderCtrl();
nColumnCount = pHeader->GetItemCount();
//删除所有列
for (i=0;i < nColumnCount;i++)
pListCtrl->DeleteColumn(0);
//m_arrHeaderLabel数组长度为列数
if(m_arrHeaderLabel.GetSize()>0)
NUM_COLUMNS = m_arrHeaderLabel.GetSize();
else
NUM_COLUMNS = 0;
//插入列
LV_COLUMN lvc;
lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
for(i = 0; i
//设置列索引号
lvc.iSubItem = i;
//设置内容
lvc.pszText = m_arrHeaderLabel[i].GetBuffer(1);
//设置列宽
if(col_wide_arr!=NULL)
lvc.cx = col_wide_arr[i];
else
lvc.cx = 80;
//设置对齐方式
lvc.fmt = LVCFMT_LEFT;
//插入列
pListCtrl->InsertColumn(i,&lvc);
}
}
BOOL CSDlg::OnInitDialog()
{
CStringArray m_arrHeaderLabel;
m_arrHeaderLabel.Add("第一列");
m_arrHeaderLabel.Add("第二列");
m_arrHeaderLabel.Add("第三列");
//必须保证该数组长度大于等于m_arrHeaderLabel长度
long col_wide_arr[4]={90,70,70,90};
//设置listctrl风格
m_list.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_HEADERDRAGDROP);
RefreshListViewHeader(&m_list,m_arrHeaderLabel,col_wide_arr);
CDialog::OnInitDialog();
}