三、常見問題之表頭寬度怎麼調整?
轉載自
https://blog.csdn.net/xinyuelan/article/details/88706486QtTreePropertyBrowser的表頭預設模式是QtTreePropertyBrowser::Stretch,表頭是不允許動态調整大小的,如果屬性名稱比較長,就會出現“XXXXXX...”的情況。
QtTreePropertyBrowser的表頭屬性有以下幾種:
QtTreePropertyBrowser::Interactive //互動
QtTreePropertyBrowser::Fixed //固定
QtTreePropertyBrowser::ResizeToContents //自動調整内容
QtTreePropertyBrowser::Stretch //拉伸
如何設定成可以通過滑鼠動态的調整表頭段的大小呢?
方法1、設定QtTreePropertyBrowser對象模式為QtTreePropertyBrowser::ResizeToContents。
ui->widgetL->setResizeMode(QtTreePropertyBrowser::ResizeToContents);
試試看,能否滿足。如果不行,就嘗試下面的方法2。
方法2、設定QtTreePropertyBrowser對象模式為QtTreePropertyBrowser::Interactive。
ui->widgetL->setResizeMode(QtTreePropertyBrowser::Interactive);
QtTreePropertyBrowser類自帶一個可以指定表頭大小的函數 QtTreePropertyBrowser::setSplitterPosition(int position),可以通過該函數設定表頭的寬度,以适應Item的大小。但該函數僅能設定第一列,看原型函數,如下:
void QtTreePropertyBrowser::setSplitterPosition(int position)
{
d_ptr->m_treeWidget->header()->resizeSection(0, position);
}
通過對函數 setSplitterPosition()的重載,實作可修改任一表頭的寬度,代碼如下:
/**
* @brief QtTreePropertyBrowser::setSplitterPosition修改表頭寬度 - 新增
* @param index 表頭索引
* @param position 表頭寬度
*/
void QtTreePropertyBrowser::setSplitterPosition(int index, int position)
d_ptr->m_treeWidget->header()->resizeSection(index, position);
在自己的cpp屬性樹初始化中,加入如下代碼,可實作需求。
//一次調頻動作結果
m_pResultVarManager = new QtVariantPropertyManager(ui->result_property_tree);
ui->widgetL->setSplitterPosition(0, 235);
ui->widgetL->setSplitterPosition(1, 170);
四、常見問題之表格的标題怎麼修改?
QtPropertyBrowser裡面的表格本質上是QTreeWidget。需要自己手動在類QtTreePropertyBrowser,新增成員函數,實作标題的修改:
class QtTreePropertyBrowser : public QtAbstractPropertyBrowser
{
public:
void setHeaderLabels(const QStringList &labels);
}
void QtTreePropertyBrowser::setHeaderLabels(const QStringList &labels)
{
d_ptr->m_treeWidget->setHeaderLabels(labels);
}
使用說明:
//标題
QStringList head; head << tr("軸号") << tr("坐标值(mm)");
ui->widget->setHeaderLabels(head);

五、常見問題之表格的選中單行的信号響應
QtPropertyBrowser裡面的表格本質上是QTreeWidget。滑鼠單選每一行時,如何判斷目前選中的是哪一行?
通過信号QtTreePropertyBrowser::currentItemChanged來實作。
//信号槽
connect(ui->widgetL, &QtTreePropertyBrowser::currentItemChanged, this, &FormProcessProofing::slotItemChangedL);
void FormProcessProofing::slotItemChangedL(QtBrowserItem *item)
{
int index = -1;
for (int i = 0; i < m_listPropL.size(); i++)
{
if (m_listPropL[i] == item->property())
{
index = i;
break;
}
}
std::cout << index << endl;
}
六、常見問題之小數位數如何調整?
QVariant::Double表格數值預設是2位小數,如何改變?
源檔案qtpropertymanager.cpp提供了接口函數
/*!
\fn void QtDoublePropertyManager::setDecimals(QtProperty *property, int prec)
Sets the precision of the given \a property to \a prec.
The valid decimal range is 0-13. The default is 2.
\sa decimals()
*/
void QtDoublePropertyManager::setDecimals(QtProperty *property, int prec)
QtVariantPropertyManager *pVarManager = new QtVariantPropertyManager(ui->widget);
item = pVarManager->addProperty(QVariant::Double, QStringLiteral("浮點資料"));
item->setAttribute(QLatin1String("decimals"), 5);
item->setValue(3.1415926);
ui->widget->addProperty(item);
七、常見問題之折疊展開
//折疊
QtVariantPropertyManager *pVarManager = new QtVariantPropertyManager(ui->widget);
QSet<QtProperty *> p = pVarManager->properties();
QList<QtBrowserItem *> list = ui->widget_2->items(groupItem);
ui->widget_2->setExpanded(list.at(0), false);
八、常見問題之QSS字型顔色
表格裡的文字顔色,其實就是對應QSS的QTreeWidget
QTreeWidget{
background-color: #1d1f20;
color:#386487;
QTreeWidget::item{
background: #1d1f20;
x、源碼分享
請下載下傳完整的示例1源碼:
https://download.csdn.net/download/libaineu2004/12914683請下載下傳完整的示例2源碼:
https://download.csdn.net/download/libaineu2004/12914790我的開源倉庫:
https://gitee.com/libaineu2004/qproperty-browser-demo