当前位置: 首页>>代码示例>>C++>>正文


C++ QSqlRelationalTableModel::lastError方法代码示例

本文整理汇总了C++中QSqlRelationalTableModel::lastError方法的典型用法代码示例。如果您正苦于以下问题:C++ QSqlRelationalTableModel::lastError方法的具体用法?C++ QSqlRelationalTableModel::lastError怎么用?C++ QSqlRelationalTableModel::lastError使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在QSqlRelationalTableModel的用法示例。


在下文中一共展示了QSqlRelationalTableModel::lastError方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: query

HandbookDialog::HandbookDialog(QString tableName, QString tableRusName,
                               const QSqlDatabase &database,
                               QWidget *parent,
                               QStringList hiddenColumns):
    m_database(database),
    mHiddenColumns(hiddenColumns),
    QDialog(parent),
    ui(new Ui::HandbookDialog)
{
    ui->setupUi(this);

    ui->label->setText(tableRusName);
    this->setWindowTitle("Редактирование справочника: " + tableRusName);

    QMap<QString, QString> rusFieldNames = DataBaseExpert::fieldTranslationMap(m_database);
    auto ralTablesForFields = DataBaseExpert::ralationTablesForFields(m_database);
    m_record = database.record(tableName);

    QSqlRelationalTableModel * model = new QSqlRelationalTableModel(0, database);
    model->setTable(tableName);
    for (int i = 0; i < m_record.count(); ++i)
    {
        QString fieldName = m_record.fieldName(i);
        if (fieldName.size() > 3 && fieldName.right(3) == "_FK")
        {
            model->setRelation(i, QSqlRelation(ralTablesForFields[fieldName], "UID", "NAME"));
        }
    }
    model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    //model->removeColumn(0);
    model->select();

    for (int i = 0; i < m_record.count(); ++i)
    {
        model->setHeaderData(i, Qt::Horizontal, rusFieldNames[m_record.fieldName(i)]);
        if (mHiddenColumns.contains(m_record.fieldName(i), Qt::CaseInsensitive))
        {
            ui->tableView->setColumnHidden(i, true);
        }
    }

    ui->tableView->setModel(model);
    ui->tableView->setItemDelegate(new MySqlRelationDelegate(ui->tableView));

    for (int i = 0; i < m_record.count(); ++i)
    {
        if (mHiddenColumns.contains(m_record.fieldName(i), Qt::CaseInsensitive))
        {
            ui->tableView->setColumnHidden(i, true);
        }
    }

    connect(ui->btnInsert, &QPushButton::clicked, [model]()
    {
        model->insertRow(model->rowCount());
    });
    connect(ui->btnSave, &QPushButton::clicked, [model]()
    {
        if (!model->submitAll())
            qDebug() << model->lastError().text();
    });
    connect(ui->btnRevert, &QPushButton::clicked, [model]()
    {
        model->revertAll();
    });
    connect(ui->btnExit, &QPushButton::clicked, [this]()
    {
        this->accept();
    });

    ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeMode::Stretch);


    connect(ui->tableView, &QTableView::customContextMenuRequested, [this, &tableName, model] (const QPoint& pos)
    {
        QMenu * contextMenu = new QMenu(tr("Выбор действия со строкой"), ui->tableView);

        QAction* insertAction = new QAction(tr("Добавить"), contextMenu);
        contextMenu->addAction(insertAction);
        connect(insertAction, &QAction::triggered, [this, &model] ()
        {
            model->insertRow(model->rowCount());
        });

        QAction * delAction = new QAction(tr("Удалить"), contextMenu);
        contextMenu->addAction(delAction);
        connect(delAction, &QAction::triggered, [this, &pos, &tableName, model] ()
        {
            QPoint transformPos = pos;
            transformPos.setX(20);
            QModelIndex index = ui->tableView->indexAt(transformPos);
            QString recordId = ui->tableView->model()->data(index).toString();
            QSqlQuery query("DELETE FROM " + tableName + " WHERE UID = " + recordId, m_database);
            query,exec();
            model->submitAll();
        });


        contextMenu->exec(ui->tableView->viewport()->mapToGlobal(pos));
    });
//.........这里部分代码省略.........
开发者ID:BarsukAlexey,项目名称:xelarogi,代码行数:101,代码来源:handbookdialog.cpp


注:本文中的QSqlRelationalTableModel::lastError方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。