#include "dbase.h"#include "ui_dbase.h"dbase::dbase(QWidget *parent) : QDialog(parent), ui(new Ui::dbase){ ui->setupUi(this); QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); db.setHostName("localhost"); db.setDatabaseName("edb"); db.setUserName("mikh"); db.setPassword("111111"); db.setPort(5444); QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(load()));}dbase::~dbase(){ delete ui;}void dbase::changeEvent(QEvent *e){ QDialog::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}void dbase::load(){ QSqlTableModel model; model.setTable("employee"); model.select(); ui->view->setModel(&model);}
#include "dbase.h"#include "ui_dbase.h"dbase::dbase(QWidget *parent) : QDialog(parent), ui(new Ui::dbase){ ui->setupUi(this); QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); db.setHostName("localhost"); db.setDatabaseName("edb"); db.setUserName("mikh"); db.setPassword("111111"); db.setPort(5444); model = new QSqlTableModel(this); model->setTable("employee"); model->select(); QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(load()));}dbase::~dbase(){ delete ui;}void dbase::changeEvent(QEvent *e){ QDialog::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}void dbase::load(){ ui->view->setModel(model);}
ui->view->setModel(model);
C++ (Qt)if(!db.open()){ // обработать ошибку}
#include "dbase.h"#include "ui_dbase.h"dbase::dbase(QWidget *parent) : QDialog(parent), ui(new Ui::dbase){ ui->setupUi(this); QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); db.setHostName("localhost"); db.setDatabaseName("edb"); db.setUserName("mikh"); db.setPassword("111111"); db.setPort(5444); model = new QSqlTableModel(this); if (!db.open()) { QMessageBox::information(this,"Error",db.lastError().text(),QMessageBox::Ok); } else { model->setTable("employee"); ui->view->setModel(model); } QObject::connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(load()));}dbase::~dbase() { delete ui;}void dbase::changeEvent(QEvent *e) { QDialog::changeEvent(e); switch (e->type()) { case QEvent::LanguageChange: ui->retranslateUi(this); break; default: break; }}void dbase::load(){ model->select();}
model->setTable("employee");
model->select();