簡體   English   中英

將 QStandartItem 設置為 Expandable 而沒有子項

[英]Set a QStandartItem as Expandable without having a child item

我正在嘗試在可以位於不同計算機上的兩個不同程序之間發送文件夾結構。 在我的服務器上,我有一個QFileSystemModel ,在我的客戶端上,我有一個QTreeView ,它的QStandardItemModel為 model。 而且我有一個預構建信號/插槽系統,可以在程序之間發送QStringQStringList

客戶:

auto *m_stdItemModel = new QStandardItemModel(this);

auto *m_treeView = new QTreeView(this);
m_treeView->setModel(m_stdItemModel);

每次單擊客戶端m_treeView上的展開按鈕時,我都想從服務器發送子目錄。 問題是條目只有在有子項時才能擴展。

我這樣做的方法是添加一個虛擬孩子並在用戶單擊展開按鈕時將其刪除。

添加假人:

void addChildToParent(QString child, QStandardItem *parentItem, bool isExpandable)
{
    auto *childItem = new QStandardItem(child);

    if(isExpandable)
    {
        childItem->appendRow(new QStandardItem("dummy"));
    }

   parentItem->appendRow(childItem);
}

是否有一種解決方法可以在不添加虛擬孩子的情況下添加展開按鈕?

親切的問候

我認為您最好的選擇是覆蓋QStandardItemModel::hasChildren ...

class item_model: public QStandardItemModel {
  using super = QStandardItemModel;
public:
  virtual bool hasChildren (const QModelIndex &parent = QModelIndex()) const override
    {
      if (const auto *item = itemFromIndex(parent)) {

        /*
         * Here you need to return true or false depending on whether
         * or not any attached views should treat `item' as having one
         * or more child items.  Presumably based on the same logic
         * that governs `isExpandable' in the code you've shown.
         */
        return is_expandable(item);
      }
      return super::hasChildren(parent);
    }
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM