model = new QSqlQueryModel(); model->setQuery(QString("SELECT -1 as Id, '(Выберите категорию)' as Name UNION SELECT Id, Name FROM Cat WHERE Cat.Art=%1").arg(articleId)); ui->chooseCategory->setModel(model); ui->chooseCategory->setModelColumn(1);
ui->chooseCategory->setModelColumn(0); int Id = ui->chooseCategory->currentText().toInt(); ui->chooseCategory->setModelColumn(1);
C++ (Qt)#ifndef WIDGET_H#define WIDGET_H #include <QWidget>#include <QMultiHash> class QSqlQuery; namespace Ui { class Widget;} class Widget : public QWidget{ Q_OBJECT public: bool createConnection(); explicit Widget(QWidget *parent = 0); ~Widget(); private slots: void on_pushButton_clicked(); private: Ui::Widget *ui; QSqlQuery *query; QMultiHash<QString, int> *queryHash; }; #endif // WIDGET_H
C++ (Qt)#include <QtWidgets>#include <QSqlDatabase>#include <QSqlError>#include <QSqlQuery>#include <QMultiHash>#include "widget.h"#include "ui_widget.h" Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { ui->setupUi(this); if(!createConnection()) { // connection don't create return; } query = new QSqlQuery; QString strQuery = "SELECT Id, Art, Name " "FROM MyTable "; query->setForwardOnly(true); // faster if(!query->exec(strQuery)) { QMessageBox::critical(this, "Error", QString("Query error") + query->lastError().driverText() + "; " + query->lastError().databaseText()); return; } queryHash = new QMultiHash<QString, int>(); // insert items to queryHash and comboBox from field "Name" while(query->next()) { queryHash->insert(query->value("Name").toString(), query->value("Id").toInt()); ui->comboBox->addItem(QString(query->value("Name").toString())); } } bool Widget::createConnection() { // create connection to DB QSqlDatabase db = QSqlDatabase::addDatabase("QODBC3"); QString host = "SERVER\\SQL2008SRV"; QString dataBase= "MyBase"; QString userName = "sa"; QString password = "password"; db.setDatabaseName(QString("Driver={SQL Server};" "Server=%1;Database=%2; Persist Security Info=true;" "uid=%3;pwd=%4").arg(host).arg(dataBase).arg(userName).arg(password)); if(!db.open()) { QMessageBox::critical(this, "Error", QString("Connection error")); return false; } return true;} void Widget::on_pushButton_clicked() { if(queryHash->contains(ui->comboBox->currentText())) { QMessageBox::information(this, "Value", QString("Id = <b>" + QString().setNum(queryHash->value(ui->comboBox->currentText())) + "</b>") ); }} Widget::~Widget(){ delete ui; delete query; delete queryHash;}
C++ (Qt)#include "widget.h"#include <QApplication> int main(int argc, char *argv[]){ QApplication a(argc, argv); Widget w; w.show(); return a.exec();}
C++ (Qt)void Widget::on_pushButton_clicked() { if(queryHash->contains(ui->comboBox->currentText())) { QMessageBox::information(this, "Value", QString("Id = <b>" + QString().setNum(queryHash->value(ui->comboBox->currentText())) + "</b>") ); }}
model = new QSqlQueryModel();model->setQuery(QString("SELECT -1 as Id, '(Выберите категорию)' as Name UNION SELECT Id, Name FROM Cat WHERE Cat.Art=%1").arg(articleId));ui->chooseCategory->setModel(model);ui->chooseCategory->setModelColumn(1);//В комбобоксе отображается содержимое столбца Name//Если нужно получить Id:int id = model->record(ui->chooseCategory->currentIndex()).value(0).toInt();