设置具有多列的 QComboBox 文本

setting QComboBox text with multiple columns

我有一个包含 QTableView 的 QComboBox,如下所示。当我连续 select 时,QComboBox 标题只显示“Alex”,但我想要“Alex - Alex 地址”。我该怎么做? 谢谢。

假设您有 table,您可以将 att“选择行为”设置为 select Rows

:

Have you tried signal QComboBox::currentIndexChanged(int index) in combination with QComboBox::setEditText()? I'm not quite sure whether this works as well if editable is false but might be worth to check out.

OP 要求回答,因为这似乎很有价值。

那么,我们开始吧:

// standard C++ header:
#include <string>
#include <vector>

// Qt header:
#include <QtWidgets>

// table data entry
struct Entry {
  int i;
  std::string name;
  std::string address;
};

// custom table model
class TableModel: public QAbstractTableModel {
  private:
    std::vector<Entry> _entries;

  public:
    explicit TableModel(QObject *pQParent = nullptr):
      QAbstractTableModel(pQParent)
    { }

    template <typename ITER>
    TableModel(ITER first, ITER last, QObject *pQParent = nullptr) :
      TableModel(pQParent)
    {
      for (; first != last; ++first) _entries.push_back(*first);
    }

    virtual ~TableModel() = default;

    TableModel(const TableModel&) = delete;
    TableModel& operator=(const TableModel&) = delete;

    virtual int rowCount(const QModelIndex &parent = QModelIndex()) const override
    {
      return (int)_entries.size();
    }

    virtual int columnCount(const QModelIndex &parent = QModelIndex()) const override
    {
      return 3;
    }

    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override
    {
      const size_t i = (size_t)index.row();
      if (i >= _entries.size()) return QVariant();
      if (role == Qt::DisplayRole) {
        switch (index.column()) {
          case 0: return _entries[i].i;
          case 1: return QString::fromStdString(_entries[i].name);
          case 2: return QString::fromStdString(_entries[i].address);
        }
      }
      return QVariant();
    }
};

// sample data
const Entry entries[] = {
  { 1, "Alex", "Alex address" },
  { 5, "Ben", "Ben address" },
  { 6, "Mary", "Mary address" },
  { 2, "Max", "Max address" },
  { 4, "Nicole", "Nicole address" },
  { 3, "Tim", "Tim address" }
};

// main application
int main(int argc, char **argv)
{
  qDebug() << "Qt Version:" << QT_VERSION_STR;
  QApplication app(argc, argv);
  // setup GUI
  TableModel qTblModel(std::begin(entries), std::end(entries));
  QComboBox qCBox;
  qCBox.setWindowTitle("Test QComboBox with Table Model");
  QTableView qTblView(&qCBox);
  qTblView.setModel(&qTblModel);
  qTblView.horizontalHeader()->hide();
  qTblView.verticalHeader()->hide();
  qTblView.resizeColumnsToContents();
  qTblView.setSelectionBehavior(QTableView::SelectRows);
  qCBox.setView(&qTblView);
  qCBox.setModel(&qTblModel);
  qCBox.setEditable(true); // allow access to line edit
  qCBox.lineEdit()->setReadOnly(true); // prevent editing in line edit
  qCBox.show();
  // install signal handlers
  QObject::connect(&qCBox, QOverload<int>::of(&QComboBox::currentIndexChanged),
    [&](int i) {
      if ((size_t)i < std::size(entries)) {
        qCBox.setEditText(
          QString::fromStdString(entries[i].name + " | " + entries[i].address));
      }
    });
  qCBox.setCurrentIndex(-1);
  // runtime loop
  return app.exec();
}

输出:

实际上有必要将 QComboBox::editable 设置为 true 以使 QComboBox::setEditText() 正常工作。

为了防止意外的用户编辑,我将嵌入的QLineEdit依次设置为readOnly