void UserForm:: createTableModels() { QSqlRelationalTableModel* model = new QSqlRelationalTableModel(); model->setTable("good"); model->setRelation(Producer, QSqlRelation("company", "companyid", "name")); model->setRelation(Catalog, QSqlRelation("catalog", "id", "name")); model->setHeaderData(Producer, Qt::Horizontal, "Producer"); model->setHeaderData(Name, Qt::Horizontal, "Name"); model->setHeaderData(Price, Qt::Horizontal, "Price, $"); model->setHeaderData(Description, Qt::Horizontal, "Description"); model->setHeaderData(Catalog, Qt::Horizontal, "Catalog"); model->setFilter("catalog_id IS NOT NULL"); // model->removeColumn(5); model->select(); productsModel = model; model = new QSqlRelationalTableModel(); model->setTable("`order`"); model->setRelation(goodId, QSqlRelation("good", "goodid", "name")); model->setFilter("user_login='******'"); model->select(); model->setEditStrategy(QSqlTableModel::OnRowChange); model->removeColumn(3); model->removeColumn(2); model->setHeaderData(orderId, Qt::Horizontal, "Order #"); model->setHeaderData(goodId, Qt::Horizontal, "Product"); model->setHeaderData(date, Qt::Horizontal, "Date"); model->setHeaderData(address, Qt::Horizontal, "Address to deliver"); ordersModel = model; }
QSqlTableModel *EmployeeDataModel::initializeViewModel() { //TODO: id als locale Konstante QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this); model->setTable(QLatin1String("employee_view")); model->setEditStrategy(QSqlTableModel::OnFieldChange); model->setHeaderData(POS_EMPLOYEE_FIRSTNAME, Qt::Horizontal, QObject::tr("Firstname")); model->setHeaderData(POS_EMPLOYEE_LASTNAME, Qt::Horizontal, QObject::tr("Lastname")); model->select(); return model; }
void BadgeWindow::timer() { DialogTimer dialogTimer; bool saveRequest; QString taskName; QTime elapsedTime; saveRequest = dialogTimer.getTaskInfo(taskName, elapsedTime); if (saveRequest) { DialogTask dialog; QSqlRelationalTableModel *taskModel; taskModel = new QSqlRelationalTableModel(); taskModel->setEditStrategy(QSqlTableModel::OnManualSubmit); taskModel->setTable("task"); qDebug() << "Day" << QDate::currentDate().toString("yyyy-MM-dd") << endl; taskModel->setFilter("day='" + QDate::currentDate().toString("yyyy-MM-dd") + "'"); //taskModel.setEditStrategy(QSqlTableModel::OnFieldChange); taskModel->setHeaderData(0, Qt::Horizontal, QObject::tr("ID")); taskModel->setHeaderData(1, Qt::Horizontal, QObject::tr("Date")); taskModel->setHeaderData(2, Qt::Horizontal, QObject::tr("Elapsed")); //taskModel->setHeaderData(3, Qt::Horizontal, QObject::tr("Attivita'")); taskModel->setRelation(3, QSqlRelation("attivita", "id", "attivita")); taskModel->select(); // TODO: if current date don't exist create it dialog.openTask("Task", "Elapsed", taskModel, QDate::currentDate(), "task", elapsedTime, taskName); delete taskModel; } }
SQLOpenProjectDialog::SQLOpenProjectDialog(const QString & host, unsigned int port, const QString & dbname, const QString & user, const QString & password,QWidget *parent) : QDialog(parent), ui(new Ui::SQLOpenProjectDialog) { ui->setupUi(this); QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL"); m_Host=host; m_Port=port; m_DBName=dbname; m_User=user; m_Password=password; db.setHostName(m_Host); db.setPort(m_Port); db.setDatabaseName(m_DBName); db.setUserName(m_User); db.setPassword(m_Password); bool ok=db.open(); std::cout << db.lastError().databaseText().toStdString() << std::endl; if(ok){ QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this,db); model->setTable("Project"); model->setEditStrategy(QSqlTableModel::OnRowChange); model->select(); ui->existingProjectsTableView->setModel(model); }else{ } m_Accepted=false; m_Project=0; }
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)); }); // connect(ui->label_2, &QLabel::linkActivated, [this]() // { // WebReportDialog dlg(this->ui->label_2->text(), this); // dlg.exec(); // }); }
QSqlRelationalTableModel *EmployeeDataModel::initializeRelationalModel() { //TODO: id als locale Konstante QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this); model->setTable(QLatin1String("employee")); model->setEditStrategy(QSqlTableModel::OnFieldChange); model->setRelation(POS_EMPLOYEE_TITLE_ID, QSqlRelation(QLatin1String("title"), QLatin1String("title_id"), QLatin1String("name"))); model->setRelation(POS_EMPLOYEE_ADDRESS_ID, QSqlRelation(QLatin1String("address"), QLatin1String("address_id"), QLatin1String("address"))); model->setRelation(POS_EMPLOYEE_DEPARTMENT_ID, QSqlRelation(QLatin1String("department"), QLatin1String("department_id"), QLatin1String("name"))); model->setRelation(POS_EMPLOYEE_FUNCTION_ID, QSqlRelation(QLatin1String("function"), QLatin1String("function_id"), QLatin1String("name"))); model->setRelation(POS_EMPLOYEE_COMPUTER_ID, QSqlRelation(QLatin1String("computer"), QLatin1String("computer_id"), QLatin1String("network_name"))); model->setRelation(POS_EMPLOYEE_PRINTER_ID, QSqlRelation(QLatin1String("printer"), QLatin1String("printer_id"), QLatin1String("network_name"))); model->setRelation(POS_EMPLOYEE_PHONE_ID, QSqlRelation(QLatin1String("phone"), QLatin1String("phone_id"), QLatin1String("number"))); model->setRelation(POS_EMPLOYEE_MOBILE_ID, QSqlRelation(QLatin1String("mobile"), QLatin1String("mobile_id"), QLatin1String("number"))); model->setRelation(POS_EMPLOYEE_FAX_ID, QSqlRelation(QLatin1String("fax"), QLatin1String("fax_id"), QLatin1String("number"))); model->setHeaderData(POS_EMPLOYEE_ID, Qt::Horizontal, QObject::tr("ID")); model->setHeaderData(POS_EMPLOYEE_NR, Qt::Horizontal, QObject::tr("Emp.Number")); model->setHeaderData(POS_EMPLOYEE_GENDER, Qt::Horizontal, QObject::tr("Gender")); model->setHeaderData(POS_EMPLOYEE_TITLE_ID, Qt::Horizontal, QObject::tr("Title")); model->setHeaderData(POS_EMPLOYEE_FIRSTNAME, Qt::Horizontal, QObject::tr("Firstname")); model->setHeaderData(POS_EMPLOYEE_LASTNAME, Qt::Horizontal, QObject::tr("Lastname")); model->setHeaderData(POS_EMPLOYEE_ADDRESS_ID, Qt::Horizontal, QObject::tr("address")); model->setHeaderData(POS_EMPLOYEE_BIRTHDAY, Qt::Horizontal, QObject::tr("Birthday")); model->setHeaderData(POS_EMPLOYEE_EMAIL, Qt::Horizontal, QObject::tr("EMail")); model->setHeaderData(POS_EMPLOYEE_DATACARE, Qt::Horizontal, QObject::tr("Datacare")); model->setHeaderData(POS_EMPLOYEE_ACTIVE, Qt::Horizontal, QObject::tr("Active")); model->setHeaderData(POS_EMPLOYEE_STARTDATE, Qt::Horizontal, QObject::tr("StartDate")); model->setHeaderData(POS_EMPLOYEE_ENDDATE, Qt::Horizontal, QObject::tr("EndDate")); model->setHeaderData(POS_EMPLOYEE_DEPARTMENT_ID, Qt::Horizontal, QObject::tr("Department")); model->setHeaderData(POS_EMPLOYEE_FUNCTION_ID, Qt::Horizontal, QObject::tr("Function")); model->setHeaderData(POS_EMPLOYEE_COMPUTER_ID, Qt::Horizontal, QObject::tr("Computer")); model->setHeaderData(POS_EMPLOYEE_PRINTER_ID, Qt::Horizontal, QObject::tr("Printer")); model->setHeaderData(POS_EMPLOYEE_PHONE_ID, Qt::Horizontal, QObject::tr("Phone")); model->setHeaderData(POS_EMPLOYEE_MOBILE_ID, Qt::Horizontal, QObject::tr("Mobile")); model->setHeaderData(POS_EMPLOYEE_FAX_ID, Qt::Horizontal, QObject::tr("Fax")); model->setHeaderData(POS_EMPLOYEE_EMPLOYEE_ACCOUNT_ID, Qt::Horizontal, QObject::tr("Accounts")); model->setHeaderData(POS_EMPLOYEE_EMPLOYEE_DOCUMENT_ID, Qt::Horizontal, QObject::tr("Documents")); model->setHeaderData(POS_EMPLOYEE_LAST_UPDATE, Qt::Horizontal, QObject::tr("Last Update")); model->select(); return model; }