Qt中combox连接数据库

示例源码:
#include "widget.h"
#include "ui_widget.h"
#include <QtGui/QCompleter>
#include <QStringList>

Widget::Widget(QWidget *parent)
    : QWidget(parent), ui(new Ui::Widget) {
    ui->setupUi(this);

    QStringList strings;
    strings << "Biao" << "Biao Huang" << "Mac" << "MacBook" << "MacBook Pro" << "Mac Pro";
    completer = new QCompleter(strings, this);

    ui->comboBox->clear();
    ui->comboBox->addItems(strings);
    ui->comboBox->setEditable(true);
    ui->comboBox->setCompleter(completer);
}

Widget::~Widget() {
    delete ui;
    delete completer;
}

动态数据源 真正的解决方案:

      QStringList strings;
  if(db.isOpen())
    {

      QSqlQuery query("SELECT goodsno,caw  FROM ct_goods");
      while (query.next()) {
          QString goodsno = query.value(0).toString();
          strings.append(goodsno);
      }
      completer = new QCompleter(strings, this);
           ui->comboBox->clear();
           ui->comboBox->addItems(strings);
           ui->comboBox->setCompleter(completer);

你可能感兴趣的:(combox)