#ifndef MAINWINDOW_H#define MAINWINDOW_H #include <QMainWindow>#include <QMainWindow>#include "QSqlTableModel"#include <QtGui>#include <QtSql>#include <QTextCodec>#include <mainwindow.h>#include "ui_mainwindow.h"#include <QTabWidget>#include <QItemSelection>#include <QtSql>#include <QtSql/QSqlRecord>#include <QtGui/QMainWindow>#include <QtSql/QSqlRelationalTableModel>#include <QtGui/QMessageBox>#include <QtSql/QSqlQuery>#include <QtCore>#include <QtGui>namespace Ui { class MainWindow;} class MainWindow : public QMainWindow{ Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_pushButton_5_clicked(); void on_pushButton_clicked(); void on_pushButton_2_clicked(); void on_pushButton_3_clicked(); void on_pushButton_4_clicked(); void on_pushButton_6_clicked(); void on_pushButton_7_clicked(); void on_pushButton_8_clicked(); void on_pushButton_9_clicked(); void on_pushButton_10_clicked(); void on_pushButton_11_clicked(); void on_pushButton_12_clicked(); void on_pushButton_13_clicked(); void on_pushButton_14_clicked(); void on_pushButton_15_clicked(); void on_pushButton_16_clicked(); void on_pushButton_17_clicked(); void on_pushButton_18_clicked(); void on_lineEdit_textChanged(); void on_lineEdit_2_textChanged(); private: Ui::MainWindow *ui; QSqlTableModel *Kvalif; QSqlTableModel *GosSp; QSqlTableModel *VuzSp; QSqlTableModel *Speciliz; QSqlTableModel *kod; QSqlTableModel *Specvuza; QSqlTableModel * tt; QSqlDatabase db;};#endif // MAINWINDOW_H
#include <QtGui>#include "mainwindow.h"#include "ui_mainwindow.h"#include "QSqlTableModel"#include "QSqlQuery"#include "QSqlRecord"#include <QtGui>#include <QtSql>#include <QTextCodec>#include <mainwindow.h>#include "ui_mainwindow.h"#include <QTabWidget>#include <QItemSelection>#include <QtSql>#include <QtSql/QSqlRecord>#include <QtGui/QMainWindow>#include <QtSql/QSqlRelationalTableModel>#include <QtGui/QMessageBox>#include <QtSql/QSqlQuery>#include <QtCore>#include <QtGui> bool createConnection(){ QTextCodec* codec = QTextCodec::codecForName("cp1251"); QTextCodec::setCodecForTr(codec); QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL"); db.setHostName("localhost"); db.setDatabaseName("var"); db.setUserName("testuser"); db.setPassword("1"); if (!db.open()) { QMessageBox::warning(0, QObject::tr("Ошибка"), QObject::tr("Ошибка подключения к базе!!!")); return true; } else { QMessageBox::warning(0, QObject::tr("Все хорошо"), QObject::tr("Подключился к базе!!!")); QSqlQuery Ex; Ex.exec("Pragma foreign_keys=on"); return true; }} int main(int argc, char *argv[]){ QApplication a(argc, argv); createConnection(); MainWindow w; w.show(); return a.exec();}
#include <QtGui>#include "mainwindow.h"#include "ui_mainwindow.h"#include "QSqlTableModel"#include "QSqlQuery"#include "QSqlRecord"#include <QtGui>#include <QtSql>#include <QTextCodec>#include <mainwindow.h>#include "ui_mainwindow.h"#include <QTabWidget>#include <QItemSelection>#include <QtSql>#include <QtSql/QSqlRecord>#include <QtGui/QMainWindow>#include <QtSql/QSqlRelationalTableModel>#include <QtGui/QMessageBox>#include <QtSql/QSqlQuery>#include <QtCore>#include <QtGui> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); Kvalif = new QSqlTableModel(0,db); Kvalif->setTable("Kvalif"); Kvalif->select(); ui->tableView->setModel(Kvalif); Kvalif->setEditStrategy(QSqlTableModel::OnManualSubmit); Speciliz = new QSqlTableModel(0,db); tt= new QSqlTableModel(0,db); Speciliz->setTable("Speciliz"); Speciliz->select(); ui->tableView_2->setModel(Speciliz); Speciliz->setEditStrategy(QSqlTableModel::OnManualSubmit); GosSp = new QSqlTableModel(0,db); GosSp->setTable("GosSp"); GosSp->select(); ui->tableView_3->setModel(GosSp); GosSp->setEditStrategy(QSqlTableModel::OnManualSubmit); VuzSp = new QSqlTableModel(0,db); VuzSp->setTable("VuzSp"); VuzSp->select(); ui->tableView_4->setModel(VuzSp); VuzSp->setEditStrategy(QSqlTableModel::OnManualSubmit); QSqlQueryModel *query = new QSqlQueryModel(this); query->setQuery("SELECT Namespv,Namespg,Namekrat from VuzSp natural join kod natural join GosSp"); ui->tableView_5->setModel(query);}MainWindow::~MainWindow(){ delete ui;}void MainWindow::on_pushButton_5_clicked(){QSqlQueryModel *VuzSp = new QSqlQueryModel(this);VuzSp->setQuery("SELECT Namespv,kodsv,namekrat from VuzSp WHERE Namespv LIKE '"+ui->lineEdit->text()+"%'");ui->tableView_4->setModel(VuzSp);}void MainWindow::on_pushButton_clicked(){ QSqlRecord rec;// добавление Kvalif->insertRecord(-1,rec);}void MainWindow::on_pushButton_2_clicked(){ QModelIndex _ind;//удаление int _Row=ui->tableView->selectionModel()->currentIndex().row(); Kvalif->removeRow(_Row,_ind);}void MainWindow::on_pushButton_3_clicked(){ Kvalif->submitAll(); if (Kvalif->submitAll()==false) { QSqlError err = Kvalif->lastError(); //QMessageBox::critical(0,"Soobhenie","Nekkorektnoe deistvie. \n Dannie mogut bit poterani"); QMessageBox::critical(0,"Soobhenie",Kvalif->lastError().databaseText()); } QSqlQueryModel *Kafedra_SUB = new QSqlQueryModel(this); Kafedra_SUB->setQuery("select * from Kvalif");}void MainWindow::on_pushButton_4_clicked(){Kvalif->revertAll();}void MainWindow::on_pushButton_6_clicked(){ QSqlRecord rec;// добавление Speciliz->insertRecord(-1,rec);} void MainWindow::on_pushButton_7_clicked(){ QModelIndex _itt;//удаление int _Row=ui->tableView_2->selectionModel()->currentIndex().row(); Speciliz->removeRow(_Row,_itt);} void MainWindow::on_pushButton_8_clicked(){ Speciliz->submitAll(); if (Speciliz->submitAll()==false) { QSqlError err = Speciliz->lastError(); //QMessageBox::critical(0,"Soobhenie","Nekkorektnoe deistvie. \n Dannie mogut bit poterani"); QMessageBox::critical(0,"Soobhenie",Speciliz->lastError().databaseText()); } QSqlQueryModel *GosSpesz = new QSqlQueryModel(this); GosSpesz->setQuery("select * from Speciliz");} void MainWindow::on_pushButton_9_clicked(){ Speciliz->revertAll();}void MainWindow::on_pushButton_10_clicked(){ QSqlRecord rec;// добавление GosSp->insertRecord(-1,rec);} void MainWindow::on_pushButton_11_clicked(){ QModelIndex _int;//удаление int _Row=ui->tableView_3->selectionModel()->currentIndex().row(); GosSp->removeRow(_Row,_int);} void MainWindow::on_pushButton_12_clicked(){ GosSp->submitAll(); if (GosSp->submitAll()==false) { QSqlError err = GosSp->lastError(); //QMessageBox::critical(0,"Soobhenie","Nekkorektnoe deistvie. \n Dannie mogut bit poterani"); QMessageBox::critical(0,"Soobhenie",GosSp->lastError().databaseText()); } QSqlQueryModel *GosSpec = new QSqlQueryModel(this); GosSpec->setQuery("select * from GosSp"); // QMessageBox::information(this, tr("Ogranichenie Tcelostnosti"), // tr("Nezabivaite, chto izmenenie dannih v etoi tablitce trebuet redaktirovaniya tablitci GLAVNAI AND Specialnosti Vuza."));} void MainWindow::on_pushButton_13_clicked(){GosSp->revertAll();}void MainWindow::on_pushButton_14_clicked(){ QSqlRecord rec;// добавление VuzSp->insertRecord(-1,rec);}void MainWindow::on_pushButton_15_clicked(){ QModelIndex _int;//удаление int _Row=ui->tableView_4->selectionModel()->currentIndex().row(); VuzSp->removeRow(_Row,_int);}void MainWindow::on_pushButton_16_clicked(){ VuzSp->submitAll(); if (VuzSp->submitAll()==false) { QSqlError err = VuzSp->lastError(); //QMessageBox::critical(0,"Soobhenie","Nekkorektnoe deistvie. \n Dannie mogut bit poterani"); QMessageBox::critical(0,"Soobhenie",VuzSp->lastError().databaseText()); } QSqlQueryModel *GosSpess = new QSqlQueryModel(this); GosSpess->setQuery("select * from VuzSp");}void MainWindow::on_pushButton_17_clicked(){VuzSp->revertAll();}void MainWindow::on_pushButton_18_clicked(){ QSqlQueryModel *tt = new QSqlTableModel(this); tt->setQuery("SELECT Namespv,kodsv,namekrat from VuzSp WHERE Namespv LIKE '"+ui->lineEdit_2->text()+"%'"); ui->tableView_5->setModel(tt); } void MainWindow::on_lineEdit_2_textChanged(){ QSqlQueryModel *tt = new QSqlTableModel(this); tt->setQuery("SELECT Namespv,Namespg,Namekrat from VuzSp natural join kod natural join GosSp WHERE Namespv LIKE'"+ui->lineEdit_2->text()+"%'"); ui->tableView_5->setModel(tt);} void MainWindow::on_lineEdit_textChanged(){ QSqlQueryModel *tt = new QSqlTableModel(this); tt->setQuery("SELECT Namespv,kodsv,namekrat from VuzSp WHERE Namespv LIKE '"+ui->lineEdit->text()+"%'"); ui->tableView_4->setModel(tt);}