-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
104 lines (89 loc) · 3.31 KB
/
mainwindow.cpp
File metadata and controls
104 lines (89 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QSqlRelation>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
db= QSqlDatabase::addDatabase("QPSQL");
db.setHostName("127.0.0.1");
db.setDatabaseName("postgres");
db.setPort(5432);
db.setUserName("postgres");
db.setPassword("560047");
if (!db.open())
{
QMessageBox::warning(this,"lol",db.lastError().text());
qDebug()<<db.lastError().text();
qDebug()<<"ffff";
}
model = new QSqlRelationalTableModel(this,db);
model->setTable("smartphone");
model->setRelation(model->fieldIndex("ID"),QSqlRelation("smart_photo","smart_id","url"));
model->select();
ui->tableView->setModel(model);
ui->comboBox->setModel(model);
ui->comboBox->setModelColumn(1);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_tableView_customContextMenuRequested(const QPoint &pos)
{
QMenu * menu = new QMenu(this);
QAction * remove= new QAction(trUtf8("удалить"),this);
connect(remove,SIGNAL(triggered()),this,SLOT(slotRemove()));
menu->addAction(remove);
menu->popup(ui->tableView->viewport()->mapToGlobal(pos));
}
void MainWindow::on_submit_clicked()
{
QString brand = ui->linebrand->text();
QString modely = ui->linemodel->text();
QString capacity = ui->linecapacity->text();
QString diagonal = ui->linediagonal->text();
QString price = ui->lineprice->text();
query.prepare("insert into smartphone(brand,model,capacity,diagonal,price) "
"values (:brand,:modely,:capacity,:diagonal,:price);
query.bindValue(":brand",brand);
query.bindValue(":model",modely);
query.bindValue(":capacity",capacity);
query.bindValue(":diagonal",diagonal);
query.bindValue(":price",price);
query.exec();
model->select();
}
void MainWindow::slotRemove()
{
int row = ui->tableView ->selectionModel()->currentIndex().row();
if (QMessageBox::warning(this,
trUtf8("Удаление записи"),
trUtf8("Вы уверены, что хотите удалить эту запись?"),
QMessageBox::Yes | QMessageBox::No) == QMessageBox::No)
{
db.rollback();
} else
{
if(!model->removeRow(row)){
QMessageBox::warning(this,trUtf8("Уведомление"),
trUtf8("Не удалось удалить запись\n"
"Возможно она используется другими таблицами\n"
"Проверьте все зависимости и повторите попытку"));
}
model->select();
}
}
void MainWindow::on_pushButton_clicked()
{
QSqlQuery query;
QString urlstr= ui->lineUrl->text();
QString idurl=ui->lineEdit->text();
qDebug()<<"insert into smart_photo(url,smart_id) values ('"+urlstr+"',"+idurl+");";
query.prepare("insert into smart_photo(url,smart_id) values (:urlstr,:idurl);
query.bindValue(":urlstr",urlstr);
query.bindValue(":idurl",idurl);
query.exec();
model->select();
}