V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
wbing
V2EX  ›  问与答

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

  •  
  •   wbing · 2019-01-16 11:41:13 +08:00 · 1893 次点击
    这是一个创建于 1925 天前的主题,其中的信息可能已经有所发展或是发生改变。
    试了好多方式,
    使用这个 combobox->view()->selectionModel()->select(index,QItemSelectionModel::SelectCurrent)也不行
    第 1 条附言  ·  2019-01-16 14:17:23 +08:00
    解决了,用 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);
    }
    }

    ```
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4564 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 01:05 · PVG 09:05 · LAX 18:05 · JFK 21:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.