4 回答

TA貢獻2039條經(jīng)驗 獲得超8個贊
Qt幫助文檔里都是用QTabView顯示QSqlQueryModel里的數(shù)據(jù)的,
真要按內(nèi)容改寬度很麻煩,因為不同數(shù)據(jù)長度差距太大,從幾字節(jié)到幾百字節(jié)可能都有。
所以可以間接一點處理,你對列寬合適的寬度做一個估值,
比如顯示日期加時間20字節(jié)的樣子,大概寬度比如200,
用QTableView 的:
void QTableView::setColumnWidth ( int column, int width )
把每個列寬估計一個寬度,設(shè)置一下每個列寬,
看起來差不多就行了。
又找了一下,好像找到你要的函數(shù)了:
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.
你調(diào)用resizeColumnsToContents函數(shù)試試看效果。

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

TA貢獻1840條經(jīng)驗 獲得超5個贊
用qsqltablemodel的insetrow()、setdata()、submitall()函數(shù)實現(xiàn)增;
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()函數(shù)實現(xiàn)刪;
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實現(xiàn)改;
QSqlRecord recordCurrentRow = officeTable->record(id);
recordCurrentRow.setValue("id", id - 1);
officeTable->setRecord(id - 1, recordCurrentRow);
officeTable->submitAll();
用QSqlRecord類的.value進行比較實現(xiàn)查;
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條經(jīng)驗 獲得超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);
可以用這個
添加回答
舉報