猿问

请问在qt中 QSqlQueryModel 列宽根据内容改变?

qt中 QSqlQueryModel 列宽根据内容改变


慕盖茨4494581
浏览 1546回答 4
4回答

largeQ

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函数试试看效果。

千万里不及你

QHeaderView *headerView = tableView->verticalHeader();headerView->setHidden(true);QStringList header;header<<tr("Name")<<tr("Path")<<tr("随便改");tableView->setHorizontalHeaderLabels(header);

慕斯709654

用qsqltablemodel的insetrow()、setdata()、submitall()函数实现增;  &nbsp;officeTable->insertRow(0);  &nbsp;officeTable->setData(officeTable->index(0,&nbsp;0),&nbsp;row);  &nbsp;officeTable->setData(officeTable->index(0,&nbsp;1),&nbsp;newWnd->imageFileEditor->currentIndex());  &nbsp;officeTable->setData(officeTable->index(0,&nbsp;2),&nbsp;newWnd->locationText->text());  &nbsp;officeTable->setData(officeTable->index(0,&nbsp;3),&nbsp;newWnd->countryText->currentText());  &nbsp;officeTable->setData(officeTable->index(0,&nbsp;4),&nbsp;newWnd->descriptionEditor->toPlainText());  &nbsp;officeTable->submitAll();&nbsp;用removerow()、submitall()函数实现删;  &nbsp;int&nbsp;officeCount&nbsp;=&nbsp;officeTable->rowCount();  &nbsp;officeTable->removeRow(id);  &nbsp;for(int&nbsp;i&nbsp;=&nbsp;id;&nbsp;i&nbsp;<&nbsp;officeCount&nbsp;-&nbsp;1;i++)  &nbsp;{  &nbsp; officeTable->setData(officeTable->index(i,&nbsp;0),&nbsp;i);  &nbsp;}  &nbsp;officeTable->submitAll();用QSqlRecord类的setvalue实现改;  &nbsp; QSqlRecord&nbsp;recordCurrentRow&nbsp;=&nbsp;officeTable->record(id);  &nbsp; recordCurrentRow.setValue("id",&nbsp;id&nbsp;-&nbsp;1);  &nbsp; officeTable->setRecord(id&nbsp;-&nbsp;1,&nbsp;recordCurrentRow);  &nbsp; officeTable->submitAll();&nbsp;用QSqlRecord类的.value进行比较实现查;  int&nbsp;Dialog::findArtistId(const&nbsp;QString&nbsp;&artist)  {  &nbsp; &nbsp; QSqlTableModel&nbsp;*artistModel&nbsp;=&nbsp;model->relationModel(2);&nbsp; &nbsp; int&nbsp;row&nbsp;=&nbsp;0;&nbsp;  &nbsp; &nbsp; while&nbsp;(row&nbsp;<&nbsp;artistModel->rowCount())&nbsp;{  &nbsp; &nbsp; &nbsp; &nbsp; QSqlRecord&nbsp;record&nbsp;=&nbsp;artistModel->record(row);  &nbsp; &nbsp; &nbsp; &nbsp; if&nbsp;(record.value("artist")&nbsp;==&nbsp;artist)  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return&nbsp;record.value("id").toInt();  &nbsp; &nbsp; &nbsp; &nbsp; else  &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row++;  &nbsp; &nbsp; }  &nbsp; &nbsp; return&nbsp;addNewArtist(artist);}&nbsp;

MMTTMM

ui->tableView->setSortingEnabled(true);ui->tableView->horizontalHeader()->setSortIndicator(1,Qt::AscendingOrder);QSortFilterProxyModel *sqlproxy = new QSortFilterProxyModel(this);sqlproxy->setSourceModel(m_model);ui->tableView->setModel(sqlproxy);可以用这个
随时随地看视频慕课网APP
我要回答