4 回答

TA貢獻2039條經驗 獲得超8個贊
Qt幫助文檔里都是用QTabView顯示QSqlQueryModel里的數據的,
真要按內容改寬度很麻煩,因為不同數據長度差距太大,從幾字節到幾百字節可能都有。
所以可以間接一點處理,你對列寬合適的寬度做一個估值,
比如顯示日期加時間20字節的樣子,大概寬度比如200,
用QTableView 的:
void QTableView::setColumnWidth ( int column, int width )
把每個列寬估計一個寬度,設置一下每個列寬,
看起來差不多就行了。
又找了一下,好像找到你要的函數了:
void QTableView::resizeColumnsToContents () [slot]
Resizes all columns based on the size hints of the delegate used to render each item in the columns.
Resizes all rows based on the size hints of the delegate used to render each item in the rows.
你調用resizeColumnsToContents函數試試看效果。

TA貢獻1784條經驗 獲得超9個贊
QHeaderView *headerView = tableView->verticalHeader();
headerView->setHidden(true);
QStringList header;
header<<tr("Name")<<tr("Path")<<tr("隨便改");
tableView->setHorizontalHeaderLabels(header);

TA貢獻1840條經驗 獲得超5個贊
用qsqltablemodel的insetrow()、setdata()、submitall()函數實現增;
officeTable->insertRow(0);
officeTable->setData(officeTable->index(0, 0), row);
officeTable->setData(officeTable->index(0, 1), newWnd->imageFileEditor->currentIndex());
officeTable->setData(officeTable->index(0, 2), newWnd->locationText->text());
officeTable->setData(officeTable->index(0, 3), newWnd->countryText->currentText());
officeTable->setData(officeTable->index(0, 4), newWnd->descriptionEditor->toPlainText());
officeTable->submitAll();
用removerow()、submitall()函數實現刪;
int officeCount = officeTable->rowCount();
officeTable->removeRow(id);
for(int i = id; i < officeCount - 1;i++)
{
officeTable->setData(officeTable->index(i, 0), i);
}
officeTable->submitAll();
用QSqlRecord類的setvalue實現改;
QSqlRecord recordCurrentRow = officeTable->record(id);
recordCurrentRow.setValue("id", id - 1);
officeTable->setRecord(id - 1, recordCurrentRow);
officeTable->submitAll();
用QSqlRecord類的.value進行比較實現查;
int Dialog::findArtistId(const QString &artist)
{
QSqlTableModel *artistModel = model->relationModel(2);
int row = 0;
while (row < artistModel->rowCount()) {
QSqlRecord record = artistModel->record(row);
if (record.value("artist") == artist)
return record.value("id").toInt();
else
row++;
}
return addNewArtist(artist);
}

TA貢獻1869條經驗 獲得超4個贊
ui->tableView->setSortingEnabled(true);
ui->tableView->horizontalHeader()->setSortIndicator(1,Qt::AscendingOrder);
QSortFilterProxyModel *sqlproxy = new QSortFilterProxyModel(this);
sqlproxy->setSourceModel(m_model);
ui->tableView->setModel(sqlproxy);
可以用這個
添加回答
舉報