benson458
V2EX  ›  问与答

Qt 中 QComboBox 使用了 QTreeView 视图, setCurrentText 不生效了,要如何实现?

  •  
  •   benson458 · Jan 16, 2019 · 2310 views
    This topic created in 2706 days ago, the information mentioned may be changed or developed.
    试了好多方式,
    使用这个 combobox->view()->selectionModel()->select(index,QItemSelectionModel::SelectCurrent)也不行
    Supplement 1  ·  Jan 16, 2019
    解决了,用 setcurrentText 只能设置第一层节点,所以自己实现了下

    ``` cpp
    void TreeCombobox::setCurText(QString text)
    {
    QAbstractItemModel *model = this->view()->model();
    QModelIndexList Items = model->match(model->index(0, 0),Qt::DisplayRole,QVariant::fromValue(text), 2, Qt::MatchRecursive); // look *
    for(QModelIndex index : Items){
    this->setRootModelIndex(index.parent());
    this->setModelColumn(index.column());
    this->setCurrentIndex(index.row());
    this->setRootModelIndex(QModelIndex());
    this->view()->setCurrentIndex(index);
    }
    }

    ```
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   865 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 19:22 · PVG 03:22 · LAX 12:22 · JFK 15:22
    ♥ Do have faith in what you're doing.