亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請問在qt中 QSqlQueryModel 列寬根據內容改變?

請問在qt中 QSqlQueryModel 列寬根據內容改變?

慕蓋茨4494581 2019-07-01 10:06:56
qt中 QSqlQueryModel 列寬根據內容改變
查看完整描述

4 回答

?
largeQ

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函數試試看效果。

查看完整回答
反對 回復 2019-07-14
?
千萬里不及你

TA貢獻1784條經驗 獲得超9個贊

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

查看完整回答
反對 回復 2019-07-14
?
慕斯709654

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);

}

 





查看完整回答
反對 回復 2019-07-14
?
MMTTMM

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);

可以用這個

查看完整回答
反對 回復 2019-07-14
  • 4 回答
  • 0 關注
  • 1620 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號