Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
M
Mytest_QT
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Qiu Chuntao
Mytest_QT
Commits
bd6a768f
Commit
bd6a768f
authored
Sep 22, 2020
by
Qiu Chuntao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add new file
parent
23efe094
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
96 additions
and
0 deletions
+96
-0
Mainwind.cpp
Mainwind.cpp
+96
-0
No files found.
Mainwind.cpp
0 → 100644
View file @
bd6a768f
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
MainWindow
::
MainWindow
(
QWidget
*
parent
)
:
QMainWindow
(
parent
),
ui
(
new
Ui
::
MainWindow
)
{
ui
->
setupUi
(
this
);
registwind
=
new
Regist
(
this
);
userwind
=
new
UserWindow
(
this
);
init_database
();
connect
(
this
,
SIGNAL
(
sendinfo
(
QSqlDatabase
,
QString
)),
registwind
,
SLOT
(
getdata
(
QSqlDatabase
,
QString
)));
}
MainWindow
::~
MainWindow
()
{
db
.
close
();
delete
ui
;
}
void
MainWindow
::
init_database
()
{
//数据库是否存在
if
(
QSqlDatabase
::
contains
(
"mysqlite"
))
{
//存在
db
=
QSqlDatabase
::
database
(
"mysqlite"
);
}
else
{
//不存在,则创建
db
=
QSqlDatabase
::
addDatabase
(
"QSQLITE"
);
db
.
setDatabaseName
(
"user.db"
);
}
//打开数据库
if
(
db
.
open
())
{
qDebug
()
<<
"open db ok"
;
}
else
{
qDebug
()
<<
"open error"
;
}
QSqlQuery
sql_query
(
db
);
QString
sql
=
"create table if not exists "
"user(id integer primary key autoincrement,"
"username text not NULL ,password text not NULL);"
;
if
(
!
sql_query
.
exec
(
sql
))
{
qDebug
()
<<
"creat table ok"
;
return
;
}
qDebug
()
<<
"creat ok"
;
}
//注册
void
MainWindow
::
on_pushButton_2_clicked
()
{
emit
sendinfo
(
db
,
"user"
);
registwind
->
show
();
this
->
hide
();
}
//登录
void
MainWindow
::
on_pushButton_clicked
()
{
//查询数据库
QString
username
=
ui
->
username
->
text
();
QString
password
=
ui
->
password
->
text
();
QString
sql
=
QString
(
"select * from user where username = %1 "
"and password = %2;"
).
arg
(
username
,
password
);
QSqlQuery
sql_query
(
db
);
if
(
!
sql_query
.
exec
(
sql
))
{
qDebug
()
<<
"查询失败"
<<
sql_query
.
lastError
();
return
;
}
qDebug
()
<<
"查询成功"
;
QString
usname
,
psword
;
while
(
sql_query
.
next
())
{
usname
=
sql_query
.
value
(
1
).
toString
();
psword
=
sql_query
.
value
(
2
).
toString
();
}
qDebug
()
<<
usname
<<
psword
;
if
(
usname
==
username
&&
psword
==
password
)
{
qDebug
()
<<
"登录成功"
;
emit
sendata
(
db
,
"user"
);
userwind
->
show
();
this
->
hide
();
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment