4 回答
TA贡献2039条经验 获得超7个赞
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);
可以用这个
添加回答
举报