本文整理汇总了C++中QSqlTableModel::select方法的典型用法代码示例。如果您正苦于以下问题:C++ QSqlTableModel::select方法的具体用法?C++ QSqlTableModel::select怎么用?C++ QSqlTableModel::select使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类QSqlTableModel
的用法示例。
在下文中一共展示了QSqlTableModel::select方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sql_intro_snippets
//.........这里部分代码省略.........
QSqlQuery query;
query.exec("UPDATE employee SET salary = 70000 WHERE id = 1003");
//! [37]
}
{
// DELETE1
//! [38]
QSqlQuery query;
query.exec("DELETE FROM employee WHERE id = 1007");
//! [38]
}
{
// TRANSACTION
//! [39]
QSqlDatabase::database().transaction();
QSqlQuery query;
query.exec("SELECT id FROM employee WHERE name = 'Torild Halvorsen'");
if (query.next()) {
int employeeId = query.value(0).toInt();
query.exec("INSERT INTO project (id, name, ownerid) "
"VALUES (201, 'Manhattan Project', "
+ QString::number(employeeId) + ')');
}
QSqlDatabase::database().commit();
//! [39]
}
{
// SQLQUERYMODEL1
//! [40]
QSqlQueryModel model;
model.setQuery("SELECT * FROM employee");
for (int i = 0; i < model.rowCount(); ++i) {
int id = model.record(i).value("id").toInt();
QString name = model.record(i).value("name").toString();
qDebug() << id << name;
}
//! [40]
}
{
// SQLTABLEMODEL1
//! [41]
QSqlTableModel model;
model.setTable("employee");
model.setFilter("salary > 50000");
model.setSort(2, Qt::DescendingOrder);
model.select();
for (int i = 0; i < model.rowCount(); ++i) {
QString name = model.record(i).value("name").toString();
int salary = model.record(i).value("salary").toInt();
qDebug() << name << salary;
}
//! [41]
}
{
// SQLTABLEMODEL2
QSqlTableModel model;
model.setTable("employee");
//! [42]
for (int i = 0; i < model.rowCount(); ++i) {
QSqlRecord record = model.record(i);
double salary = record.value("salary").toInt();
salary *= 1.1;
record.setValue("salary", salary);
model.setRecord(i, record);
}
model.submitAll();
//! [42]
// SQLTABLEMODEL3
int row = 1;
int column = 2;
//! [43]
model.setData(model.index(row, column), 75000);
model.submitAll();
//! [43]
// SQLTABLEMODEL4
//! [44]
model.insertRows(row, 1);
model.setData(model.index(row, 0), 1013);
model.setData(model.index(row, 1), "Peter Gordon");
model.setData(model.index(row, 2), 68500);
model.submitAll();
//! [44]
//! [45]
model.removeRows(row, 5);
//! [45] //! [46]
model.submitAll();
//! [46]
}
}
示例2: createBars
void MainWindow::createBars() {
createActions();
qpbMain = new QProgressBar;
qpbMain->setMaximumSize( 150, 15 );
qpbMain->setTextVisible( 0 );
setStatusBar(qsbMain = new QStatusBar);
qsbMain->showMessage( tr( "Ready" ));
qsbMain->addPermanentWidget( qpbMain );
setMenuBar( qmbMain = new QMenuBar );
qmbMain->addMenu( qmFile = new QMenu( tr( "&File" )));
qmFile->addSeparator();
qmFile->addAction( qaPrintDialog );
qmFile->addSeparator();
qmFile->addAction( qaExit );
qmbMain->addMenu( qmEdit = new QMenu( tr( "&Edit" )));
qmEdit->addActions( qagNavigation->actions ());
qmEdit->addAction( qaSearch );
qmEdit->addMenu (qmSubEdit = new QMenu( tr( "Languages" )));
qmSubEdit->addActions (qagLanguages->actions ());
qmbMain->addMenu( qmView = new QMenu( tr( "&View" )));
qmView->addActions ( qagZoom->actions ());
qmbMain->addMenu( qmHelp = new QMenu( tr( "&Help" )));
qmHelp->addAction( qaAboutQt );
qmHelp->addSeparator();
qmHelp->addAction( qaAbout );
qtbDeleteSearch = new QToolButton(this);
qtbDeleteSearch->setDefaultAction( qaClearSearch );
qtbDeleteSearch->setToolTip( "Clear" );
qtbDeleteSearch->setFocusPolicy( Qt::NoFocus );
qleSearch = new QLineEdit;
connect( qleSearch, SIGNAL( returnPressed ()), this, SLOT( lineSearch ()));
connect( qleSearch, SIGNAL( returnPressed ()), this, SLOT( setSearchWord ()));
#if ( QT_VERSION >= 0x040700 )
qleSearch->setPlaceholderText( tr( "Search" ));
#endif
QSqlTableModel qstm;
qstm.setTable("searchWords"); // table name
qstm.removeColumn(0); // remove the id column
qstm.removeColumn(2); // remove the numberofused column
qstm.select();
QCompleter::CompletionMode mode = QCompleter::InlineCompletion; // a new completer mode
QCompleter *qcSearchWordHelp = new QCompleter(&qstm);
qcSearchWordHelp->setCompletionMode(mode); // set the mode
qleSearch->setCompleter(qcSearchWordHelp);
qtbMain = new QToolBar( "Toolbar" );
qtbMain->setFloatable( false );
qtbMain->setMovable( false );
qtbMain->addAction( qaHome );
qtbMain->addSeparator();
qtbMain->addActions( qagNavigation->actions ());
qtbMain->addSeparator();
qtbMain->addWidget( qtbDeleteSearch );
qtbMain->addWidget( qleSearch );
addToolBar( qtbMain );
}
示例3: createEditor
// Предоставление редактора
QWidget* ComboBoxMailDelegate::createEditor(QWidget* parent, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
if (index.column() == 1) {
// QMessageBox::critical(0,"",index.model()->data(index.sibling(index.row(),0)).toString());
QString pole = index.model()->data(index.sibling(index.row(), 0)).toString();
int j;
for (int i = 0; i < fieldName.count(); i++) {
QString s = model->headerData(fieldName.at(i) , Qt::Horizontal).toString();
s.replace("\n", " ");
if (s == pole) {
j = fieldName.at(i);
break;
}
}
//QMessageBox::critical(0,"",QString("%1").arg(j));
QSqlRelation rel = model->relation(j);
if (rel.indexColumn() != QString("")) {
//QMessageBox::critical(0,"",rel.indexColumn()+" "+rel.displayColumn()+" "+rel.tableName());
QComboBox* pRes = new QComboBox(parent);
QSqlTableModel* relModel = new QSqlTableModel;
relModel->setTable(rel.tableName());
/* Удаление выбранных значений в кому */
if (pole == tr("Кому")) {
QString relFilter = QString("user_id != '00000000-0000-0000-0000-000000000000'");
for (int i = 0; i < index.model()->rowCount(); i++) {
QString pole = index.model()->data(index.sibling(i, 0)).toString();
if (pole == tr("Кому") && index.row() != i) {
QString val = index.model()->data(index.sibling(i, 1)).toString();
if ( val != QString(""))
relFilter = QString("%1 and not user_id = '%2'").arg(relFilter).arg(val);
}
//QMessageBox::critical(0,"",index.model()->data(index.sibling(i,1)).toString());
}
//QMessageBox::critical(0,"","-"+relFilter+"-");
relModel->setFilter(relFilter);
}
relModel->select();
pRes->setModel(relModel);
pRes->setModelColumn(relModel->fieldIndex(rel.displayColumn()));
return pRes;
}
if (model->data(model->index(0, j)).type() == QVariant::Date) {
QDateEdit* pRes = new QDateEdit(parent);
pRes->setCalendarPopup(true);
pRes->setDisplayFormat("dd.MM.yyyy");
return pRes;
}
if (model->data(model->index(0, j)).type() == QVariant::Bool) {
QComboBox* pRes = new QComboBox(parent);
pRes->addItem(tr("Нет"));
pRes->addItem(tr("Да"));
return pRes;
}
return QItemDelegate::createEditor(parent, option, index);
}
if (index.column() == 0) {
QComboBox* pRes = new QComboBox(parent);
bool typeflag = true;
bool priorflag = true;
bool recipientflag = true;
bool beginflag = true;
bool endflag = true;
for (int i = 0; i < index.model()->rowCount(); i++) {
QString pole = index.model()->data(index.sibling(i, 0)).toString();
if (pole == tr("Тип") && index.row() != i)
typeflag = false;
if (pole == tr("Приоритет") && index.row() != i)
priorflag = false;
if (pole == tr("Начало") && index.row() != i)
beginflag = false;
if (pole == tr("Конец") && index.row() != i)
endflag = false;
}
//if (recipientflag)
pRes->addItem(tr("Кому"));
//.........这里部分代码省略.........
示例4: on_confirm_clicked
void estore::on_confirm_clicked()
{
int num = ui->num->text().toInt();
QSqlTableModel model;
model.setTable("commodity");
model.setFilter(tr("number = '%1'").arg(num));
model.select();
if(model.rowCount()==1)//查询到有一个结果
{
QString name =model.record(0).value("name").toString();
QString description =model.record(0).value("description").toString();
double primeprice = model.record(0).value("primeprice").toDouble();
double saleprice = model.record(0).value("saleprice").toDouble();
int column = model.record(0).value("column").toInt();
int sum = model.record(0).value("sum").toInt();
double discount = model.record(0).value("discount").toDouble();
QString publishmentbrand =model.record(0).value("publishmentbrand").toString();
QString ISBNmaterialcolumn =model.record(0).value("ISBNmaterialcolumn").toString();
QString date =model.record(0).value("date").toString();
QString pagenumbersizeweight =model.record(0).value("pagenumbersizeweight").toString();
QString si = ui->sum->text();
int suminput= si.toInt();
int numflag1=1;
for(int i=0;i<si.size();i++)
if ((si[i]<'0')||(si[i]>'9'))
{
numflag1=0;
break;
}
if(si.size()==0)
{
QMessageBox::critical(NULL, "错误","数量未填写!");
}
else if(numflag1==0)
{
QMessageBox::critical(NULL, "错误","数量填写格式错误!");
}
else if(suminput > sum )
{
QMessageBox::warning(NULL, tr("错误"), tr("输入商品数量大于库存!"));
}
else
{
confirm c(name, suminput);
if(c.exec()==QDialog::Accepted)
{
if (addTrolley(num, name, description, primeprice, saleprice,column, suminput, discount, publishmentbrand, ISBNmaterialcolumn, date, pagenumbersizeweight))
{
model.setData(model.index(0,6),sum-suminput);
model.submitAll();
QMessageBox::information(NULL, tr("成功"), tr("商品成功添加至购物车!"));
accept();
}
}
}
}else
{
QMessageBox::warning(NULL, tr("错误"), tr("未查询到商品!"));
}
}
示例5: on_selectAction_triggered
void Browser::on_selectAction_triggered()
{
QSqlTableModel * tm = qobject_cast<QSqlTableModel *>(table->model());
if (tm)
tm->select();
}
示例6: saveDocument
bool DocumentSaverDB::saveDocument(KraftDoc *doc )
{
bool result = false;
if( ! doc ) return result;
QSqlTableModel model;
model.setTable("document");
QSqlRecord record;
kDebug() << "############### Document Save ################" << endl;
if( doc->isNew() ) {
record = model.record();
} else {
model.setFilter("docID=" + doc->docID().toString());
model.select();
if ( model.rowCount() > 0 ) {
record = model.record(0);
} else {
kError() << "Could not select document record" << endl;
return result;
}
// The document was already saved.
}
if( !doc->isNew() && doc->docTypeChanged() && doc->newIdent() ) {
// an existing doc has a new document type. Fix the doc number cycle and pick a new ident
DocType dt( doc->docType() );
QString ident = dt.generateDocumentIdent( doc );
doc->setIdent( ident );
}
fillDocumentBuffer( record, doc );
if( doc->isNew() ) {
kDebug() << "Doc is new, inserting" << endl;
if( !model.insertRecord(-1, record)) {
QSqlError err = model.lastError();
kDebug() << "################# SQL Error: " << err.text();
}
model.submitAll();
dbID id = KraftDB::self()->getLastInsertID();
doc->setDocID( id );
// get the uniq id and write it into the db
DocType dt( doc->docType() );
QString ident = dt.generateDocumentIdent( doc );
doc->setIdent( ident );
model.setFilter("docID=" + id.toString());
model.select();
if ( model.rowCount() > 0 ) {
model.setData(model.index(0, 1), ident);
model.submitAll();
}
} else {
kDebug() << "Doc is not new, updating #" << doc->docID().intID() << endl;
record.setValue( "docID", doc->docID().toString() );
model.setRecord(0, record);
model.submitAll();
}
saveDocumentPositions( doc );
kDebug() << "Saved document no " << doc->docID().toString() << endl;
return result;
}
示例7: saveDocumentPositions
void DocumentSaverDB::saveDocumentPositions( KraftDoc *doc )
{
DocPositionList posList = doc->positions();
// invert all pos numbers to avoid a unique violation
// FIXME: We need non-numeric ids
QSqlQuery upq;
QString queryStr = "UPDATE docposition SET ordNumber = -1 * ordNumber WHERE docID=";
queryStr += doc->docID().toString();
queryStr += " AND ordNumber > 0";
upq.prepare( queryStr );
upq.exec();
int ordNumber = 1;
QSqlTableModel model;
model.setTable("docposition");
model.setEditStrategy(QSqlTableModel::OnManualSubmit);
QVector<int> deleteIds;
DocPositionListIterator it( posList );
while( it.hasNext() ) {
DocPositionBase *dpb = it.next();
DocPosition *dp = static_cast<DocPosition*>(dpb);
QSqlRecord record ;
bool doInsert = true;
int posDbID = dp->dbId().toInt();
kDebug() << "Saving Position DB-Id: " << posDbID << endl;
if( dp->toDelete() ) {
kDebug() << "Delete item " << dp->dbId().toString() << endl;
// store the id to delete, rather than killing the model index.
// did that before here, which removed wrong items.
deleteIds << posDbID;
// delete all existing attributes no, which will not disturb the model index
dp->attributes().dbDeleteAll( dp->dbId() );
continue;
}
if( posDbID > -1 ) {
const QString selStr = QString("docID=%1 AND positionID=%2").arg( doc->docID().toInt() ).arg( posDbID );
// kDebug() << "Selecting with " << selStr << endl;
model.setFilter( selStr );
model.select();
if ( model.rowCount() > 0 ) {
if( ! dp->toDelete() )
record = model.record(0);
doInsert = false;
} else {
kError() << "ERR: Could not select document position record" << endl;
return;
}
} else {
// The record is new
record = model.record();
}
if( record.count() > 0 ) {
// kDebug() << "Updating position " << dp->position() << " is " << dp->text() << endl;
QString typeStr = PosTypePosition;
double price = dp->unitPrice().toDouble();
if ( dp->type() == DocPositionBase::ExtraDiscount ) {
typeStr = PosTypeExtraDiscount;
}
record.setValue( "docID", QVariant(doc->docID().toInt()));
record.setValue( "ordNumber", QVariant(ordNumber));
record.setValue( "text", QVariant(dp->text()));
record.setValue( "postype", QVariant(typeStr));
record.setValue( "amount", QVariant(dp->amount()));
int unitId = dp->unit().id();
record.setValue( "unit", QVariant(unitId));
record.setValue( "price", QVariant(price));
record.setValue( "taxType", QVariant(dp->taxType()));
ordNumber++; // FIXME
if( doInsert ) {
kDebug() << "Inserting!" << endl;
model.insertRecord(-1, record);
model.submitAll();
dp->setDbId( KraftDB::self()->getLastInsertID().toInt() );
} else {
kDebug() << "Updating!" << endl;
model.setRecord(0, record);
model.submitAll();
}
} else {
kDebug() << "ERR: No record object found!" << endl;
}
dp->attributes().save( dp->dbId() );
QSqlError err = model.lastError();
//.........这里部分代码省略.........
示例8: on_confirm_clicked
void event::on_confirm_clicked()
{
QString discount = ui->discount->text();
int numflag1=1,a=1;
for(int i=0;i<discount.size();i++)
if ((discount[i]<'0')||(discount[i]>'9'))
{
numflag1=0;
break;
}
if(numflag1==0)
{
QMessageBox::critical(NULL, "警告","折扣输入格式有误!");
a=0;
}
float dis = discount.toFloat();
if(dis<1||dis>100)
{
QMessageBox::critical(NULL, "警告","折扣输入格式有误!");
a=0;
}
float d =1-dis/100;
if(ui->book->isChecked())
{
QSqlTableModel model;
model.setTable("commodity");
model.setFilter(QObject::tr("column = 1 ")); //根据类别进行筛选
model.select(); //显示结果
for (int i = 0; i < model.rowCount(); i++)
{
qDebug()<<model.record(i);
qDebug()<<d;
model.setData(model.index(i,7),d);
double p= model.record(i).value("primeprice").toFloat();
double a = d*p;
int yy = (int)(a*100+1.0e-6);
double b = (double)yy/100;
model.setData(model.index(i,4),b);
model.submitAll();
qDebug()<<model.record(i);
}
if(a==1)
{
QMessageBox::information(NULL, "成功","信息更改成功!");
accept();
}
}
else if(ui->clothes->isChecked())
{
QSqlTableModel model;
model.setTable("commodity");
model.setFilter(QObject::tr("column = 2 ")); //根据类别进行筛选
model.select(); //显示结果
for (int i = 0; i < model.rowCount(); i++)
{
qDebug()<<model.record(i);
qDebug()<<d;
model.setData(model.index(i,7),d);
double p= model.record(i).value("primeprice").toFloat();
double a = d*p;
int yy = (int)(a*100+1.0e-6);
double b = (double)yy/100;
model.setData(model.index(i,4),b);
model.submitAll();
qDebug()<<model.record(i);
}
if(a==1)
{
QMessageBox::information(NULL, "成功","信息更改成功!");
accept();
}
}
else if(ui->food->isChecked())
{
QSqlTableModel model;
model.setTable("commodity");
model.setFilter(QObject::tr("column = 3 ")); //根据类别进行筛选
model.select(); //显示结果
qDebug()<<model.rowCount();
for (int i = 0; i < model.rowCount(); i++)
{
qDebug()<<model.record(i);
qDebug()<<d;
model.setData(model.index(i,7),d);
double p= model.record(i).value("primeprice").toFloat();
double a = d*p;
int yy = (int)(a*100+1.0e-6);
double b = (double)yy/100;
model.setData(model.index(i,4),b);
model.submitAll();
qDebug()<<model.record(i);
}
if(a==1)
{
QMessageBox::information(NULL, "成功","信息更改成功!");
accept();
}
}
//.........这里部分代码省略.........
示例9: updateTable
void ClinicChargeStatisticForm::updateTable()
{
initTable();
QDate startDate = m_startDateEdit->date();
QDate endDate = m_endDateEdit->date();
if(startDate > endDate)
return;
QString strName = m_nameEdit->text();
Gender eGender = (Gender)m_genderComboBox->currentIndex();
if(myDB::connectDB())
{
QSqlTableModel *model = new QSqlTableModel;
model->setTable(strClinicCharge);
QString strSql = "" , strTemp = "";
strTemp = "Gender = " + QString::number((int)eGender);
strSql += strTemp;
if(!strName.isEmpty())
{
strTemp = " and Name = \'" + strName + "\'";
strSql += strTemp;
}
QString strStartTime = startDate.toString("yyyy-MM-dd") + "T00:00:00";
QString strEndTime = endDate.toString("yyyy-MM-dd") + "T23:59:59";
strTemp = " and Time between \'" + strStartTime + "\' and \'" + strEndTime + "\'";
strSql += strTemp;
model->setFilter(strSql);
model->select();
int index = 0;
for(int i = 0; i < model->rowCount();i++)
{
QSqlRecord record = model->record(i);
QString strID = record.value("ID").toString();
QSqlTableModel *mymodel = new QSqlTableModel;
mymodel->setTable(strClinicChargeDetails);
mymodel->setFilter("ChargeId = \'" + strID + "\'");
mymodel->select();
for(int j = 0; j < mymodel->rowCount(); j++)
{
m_resultsModel->setItem(index,0,new QStandardItem(strID));
m_resultsModel->setItem(index,1,new QStandardItem(record.value("Name").toString()));
m_resultsModel->setItem(index,2,new QStandardItem(record.value("Department").toString()));
m_resultsModel->setItem(index,3,new QStandardItem(record.value("Doctor").toString()));
QSqlRecord record1 = mymodel->record(j);
m_resultsModel->setItem(index,4,new QStandardItem(record1.value("ChargeItemName").toString()));
m_resultsModel->setItem(index,5,new QStandardItem(record1.value("ChargeItemCount").toString()));
m_resultsModel->setItem(index,6,new QStandardItem(record1.value("ChargeItemPrice").toString()));
m_resultsModel->setItem(index,7,new QStandardItem(record.value("Time").toString()));
index++;
}
}
}
}
示例10: QDialog
QgsBookmarks::QgsBookmarks( QWidget *parent, Qt::WindowFlags fl )
: QDialog( parent, fl )
{
setupUi( this );
restorePosition();
//
// Create the zoomto and delete buttons and add them to the
// toolbar
//
QPushButton *btnAdd = new QPushButton( tr( "&Add" ) );
QPushButton *btnDelete = new QPushButton( tr( "&Delete" ) );
QPushButton *btnZoomTo = new QPushButton( tr( "&Zoom to" ) );
QPushButton *btnImpExp = new QPushButton( tr( "&Share" ) );
btnZoomTo->setDefault( true );
buttonBox->addButton( btnAdd, QDialogButtonBox::ActionRole );
buttonBox->addButton( btnDelete, QDialogButtonBox::ActionRole );
buttonBox->addButton( btnZoomTo, QDialogButtonBox::ActionRole );
buttonBox->addButton( btnImpExp, QDialogButtonBox::ActionRole );
QMenu *share = new QMenu();
QAction *btnExport = share->addAction( tr( "&Export" ) );
QAction *btnImport = share->addAction( tr( "&Import" ) );
connect( btnExport, SIGNAL( triggered() ), this, SLOT( exportToXML() ) );
connect( btnImport, SIGNAL( triggered() ), this, SLOT( importFromXML() ) );
btnImpExp->setMenu( share );
connect( btnAdd, SIGNAL( clicked() ), this, SLOT( addClicked() ) );
connect( btnDelete, SIGNAL( clicked() ), this, SLOT( deleteClicked() ) );
connect( btnZoomTo, SIGNAL( clicked() ), this, SLOT( zoomToBookmark() ) );
// open the database
QSqlDatabase db = QSqlDatabase::addDatabase( "QSQLITE", "bookmarks" );
db.setDatabaseName( QgsApplication::qgisUserDbFilePath() );
if ( !db.open() )
{
QMessageBox::warning( this, tr( "Error" ),
tr( "Unable to open bookmarks database.\nDatabase: %1\nDriver: %2\nDatabase: %3" )
.arg( QgsApplication::qgisUserDbFilePath() )
.arg( db.lastError().driverText() )
.arg( db.lastError().databaseText() )
);
deleteLater();
return;
}
QSqlTableModel *model = new QSqlTableModel( this, db );
model->setTable( "tbl_bookmarks" );
model->setSort( 0, Qt::AscendingOrder );
model->setEditStrategy( QSqlTableModel::OnFieldChange );
model->select();
// set better headers then column names from table
model->setHeaderData( 0, Qt::Horizontal, tr( "ID" ) );
model->setHeaderData( 1, Qt::Horizontal, tr( "Name" ) );
model->setHeaderData( 2, Qt::Horizontal, tr( "Project" ) );
model->setHeaderData( 3, Qt::Horizontal, tr( "xMin" ) );
model->setHeaderData( 4, Qt::Horizontal, tr( "yMin" ) );
model->setHeaderData( 5, Qt::Horizontal, tr( "xMax" ) );
model->setHeaderData( 6, Qt::Horizontal, tr( "yMax" ) );
model->setHeaderData( 7, Qt::Horizontal, tr( "SRID" ) );
lstBookmarks->setModel( model );
QSettings settings;
lstBookmarks->header()->restoreState( settings.value( "/Windows/Bookmarks/headerstate" ).toByteArray() );
#ifndef QGISDEBUG
lstBookmarks->setColumnHidden( 0, true );
#endif
}