QTableWidget 是qt 中常用的控件,下面是一些自己在做項目遇到的一些問題,記錄一下,前車之鑑啊!
一. 空行問題
我用QTableWidget 來顯示從數據庫中查到的數據信息,每次顯示前我會做如下操作:
baseInfoDialog->ui.tableWidget_KS_selected->clear();//清空
int lastCol = baseInfoDialog->ui.tableWidget_KS_selected->rowCount();//獲取當前表中的行數
for (i = 0; i < lastCol; ++i)
{
baseInfoDialog->ui.tableWidget_KS_selected->removeRow(i); //刪除所有行
}
baseInfoDialog->ui.tableWidget_KS_selected->setColumnCount(2);//設置新表的列數
QStringList header;
header.append("One");
header.append("Two");
baseInfoDialog->ui.tableWidget_KS_selected->setHorizontalHeaderLabels(header); //設置表頭
這樣跑着跑着就會在開頭空出2行空行,百思不得其解,後來在初始化的時候加入行數的設置,問題沒有在出現,修改後的代碼如下:
baseInfoDialog->ui.tableWidget_KS_selected->clear();//清空
int lastCol = baseInfoDialog->ui.tableWidget_KS_selected->rowCount();//獲取當前表中的行數
for (i = 0; i < lastCol; ++i)
{
baseInfoDialog->ui.tableWidget_KS_selected->removeRow(i); //刪除所有行
}
baseInfoDialog->ui.tableWidget_KS_selected->setColumnCount(2);//設置新表的列數
baseInfoDialog->ui.tableWidget_KS_selected->setRowCount(0);//設置行數
QStringList header;
header.append("One");
header.append("Two");
baseInfoDialog->ui.tableWidget_KS_selected->setHorizontalHeaderLabels(header); //設置表頭