【问题标题】:QCompleter and QListWidget as custom popup issueQCompleter 和 QListWidget 作为自定义弹出问题
【发布时间】:2012-08-10 04:29:35
【问题描述】:

我的 QPlainTextEdit (check this example) 有一个使用 QStringListModel 的 QCompleter:

  QStringListModel* model = new QStringListModel(names);
  QCompleter* completer = new QCompleter(model);
  completer->setCompletionMode(QCompleter::PopupCompletion);
  completer->setModelSorting(QCompleter::UnsortedModel);

它工作正常。现在我需要一些图标,工具提示,用于我尝试使用 QListWidget 作为自定义弹出窗口的每个建议:

  QListWidget* w = new QListWidget();
  foreach(name, names) {
    QListWidgetItem* i = new QListWidgetItem(name);
    i->setIcon(/*my Icon*/);
    i->setToolTip("");
    w->addItem(i);
  }
  completer->setPopup(w);

弹出窗口正常,就像我需要的那样,但完成没有更多工作。我无法键入文本以使其过滤建议,只需向上/向下键即可。
我试过了:

  completer->setModel(w->model());

但没有帮助!
我的错误是什么,或者只是 QStringListModel 让我能够过滤建议?你有什么建议?
谢谢你!

【问题讨论】:

    标签: qt model autocomplete qcompleter


    【解决方案1】:

    我主要处理 PyQt,但同样的处理。我的语法可能不正确,但您应该使用 QStandardItemModel 与 QStringListModel。从那里,您可以将其保留为标准弹出窗口 (QListView)

    类似:

    QStandardItemModel* model = new QStandardItemModel();
    
    // initialize the model
    int rows = names.count();  // assuming this is a QStringList
    model->setRowCount(rows);
    model->setColumnCount(1);
    
    // load the items
    int row = 0;
    foreach(name, names) {
        QStandardItem* item = new QStandardItem(name);
        item->setIcon(QIcon(":some/icon.png");
        item->setToolTip("some tool tip");
        model->setItem(row, 0, item);
        row++;
    }
    
    completer->setModel(model);
    completer->popup()->setModel(model); // may or may not be needed
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-04
      相关资源
      最近更新 更多