C++ (Qt)dbWork::dbWork(QString host,QString user,QString passwd){ QSqlError err = addConnection("QMYSQL","srv",host,user,passwd,3306); Dialog dd; if (err.type() != QSqlError::NoError) dd.warningMessage(tr("An error occurred while opening the connection: ") + err.text()); } QSqlError dbWork::addConnection(const QString &driver, const QString &dbName, const QString &host,const QString &user, const QString &passwd, int port){ static int cCount = 0; QSqlError err; db = QSqlDatabase::addDatabase(driver, QString("Browser%1").arg(++cCount)); db.setDatabaseName(dbName); db.setHostName(host); db.setPort(port); if (!db.open(user, passwd)) { err = db.lastError(); db = QSqlDatabase(); QSqlDatabase::removeDatabase(QString("Browser%1").arg(cCount)); } return err; }
C++ (Qt)void Browser::showTable(const QString &t){ QSqlRelationalTableModel * model = new QSqlRelationalTableModel(table,db->db); model->setEditStrategy(QSqlTableModel::OnRowChange); model->setTable(t); model->setRelation(6, QSqlRelation("users", "id", "name")); model->setRelation(8, QSqlRelation("status", "id", "name")); if(this->enterDialog->userAccess=="1") model->setFilter("relTblAl_6.name='"+this->enterDialog->userName+"'"); model->select(); if (model->lastError().type() != QSqlError::NoError) emit statusMessage(model->lastError().text()); table->setModel(model); table->setItemDelegate(new QSqlRelationalDelegate(table)); //FtpUploadDelegate *itD=; table->setItemDelegateForColumn(12,new FtpUploadDelegate(table)); table->setSortingEnabled(true); table->setGridStyle(Qt::DashLine); table->setColumnHidden(0, true); if(this->enterDialog->userAccess=="1"){ table->setColumnHidden(6, true); table->setColumnHidden(8, true); } table->resizeColumnsToContents(); //table->horizontalHeader()->setResizeMode(QHeaderView::Stretch); table->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed); connect(table->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),this, SLOT(currentChanged())); table->setEditTriggers(QAbstractItemView::DoubleClicked|QAbstractItemView::EditKeyPressed); // connect(table->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),this, SLOT(currentChanged())); connect(table->selectionModel(), SIGNAL(currentRowChanged(QModelIndex,QModelIndex)),model, SLOT(submit())); connect(table->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),model, SLOT(submit())); if(this->enterDialog->userAccess=="3") table->setEditTriggers(QAbstractItemView::NoEditTriggers); updateActions();}