Skip to content

Commit acfa2ef

Browse files
author
YuTinCH
committed
Qt入門篇(3):連結SQLITE資料庫
http://chenglearning.blogspot.tw/search/label/QTBasic
1 parent e94246d commit acfa2ef

File tree

9 files changed

+429
-0
lines changed

9 files changed

+429
-0
lines changed

Qt Sqlite/Taiwan

5 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "DBConnection.h"
2+
3+
DBConnection::DBConnection()
4+
{
5+
}

Qt Sqlite/firstqtproject/Taiwan

5 KB
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#-------------------------------------------------
2+
#
3+
# Project created by QtCreator 2013-11-03T22:12:55
4+
#
5+
#-------------------------------------------------
6+
7+
QT += core gui sql
8+
9+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10+
11+
TARGET = firstqtproject
12+
TEMPLATE = app
13+
14+
15+
SOURCES += main.cpp\
16+
mainwindow.cpp
17+
18+
HEADERS += mainwindow.h
19+
20+
FORMS += mainwindow.ui

Qt Sqlite/firstqtproject/firstqtproject.pro.user

+244
Large diffs are not rendered by default.

Qt Sqlite/firstqtproject/main.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "mainwindow.h"
2+
#include <QApplication>
3+
4+
5+
int main(int argc, char *argv[])
6+
{
7+
8+
QApplication a(argc, argv);
9+
MainWindow w;
10+
w.show();
11+
12+
return a.exec();
13+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "mainwindow.h"
2+
#include "ui_mainwindow.h"
3+
#include <QTextCodec>
4+
#include <QtSql/QtSql>
5+
6+
MainWindow::MainWindow(QWidget *parent) :
7+
QMainWindow(parent),
8+
ui(new Ui::MainWindow)
9+
{
10+
ui->setupUi(this);
11+
ui->comboBox->setEnabled(false);
12+
QStringList cityname;
13+
if(DBConnection(cityname))
14+
ui->comboBox->addItems(cityname);
15+
16+
}
17+
MainWindow::~MainWindow()
18+
{
19+
delete ui;
20+
}
21+
void MainWindow::on_checkBox_clicked(bool checked)
22+
{
23+
if(checked) ui->comboBox->setEnabled(true);
24+
else ui->comboBox->setEnabled(false);
25+
}
26+
bool MainWindow::DBConnection(QStringList &citylist)
27+
{
28+
QSqlDatabase db;
29+
db=QSqlDatabase::addDatabase("QSQLITE");
30+
db.setDatabaseName("Taiwan");
31+
if(db.open())
32+
{
33+
QSqlQuery query("Select City_cname from Taiwan_City");
34+
while(query.next())
35+
{
36+
citylist.append(query.value(0).toString().trimmed());
37+
}
38+
qDebug()<<"Success";
39+
return true;
40+
}
41+
else return false;
42+
}

Qt Sqlite/firstqtproject/mainwindow.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#ifndef MAINWINDOW_H
2+
#define MAINWINDOW_H
3+
4+
#include <QMainWindow>
5+
#include <QStringList>
6+
7+
namespace Ui {
8+
class MainWindow;
9+
}
10+
11+
class MainWindow : public QMainWindow
12+
{
13+
Q_OBJECT
14+
15+
public:
16+
explicit MainWindow(QWidget *parent = 0);
17+
~MainWindow();
18+
19+
private slots:
20+
void on_checkBox_clicked(bool checked);
21+
22+
private:
23+
Ui::MainWindow *ui;
24+
bool DBConnection(QStringList &citylist);
25+
};
26+
27+
#endif // MAINWINDOW_H
+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>MainWindow</class>
4+
<widget class="QMainWindow" name="MainWindow">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>368</width>
10+
<height>91</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>MainWindow</string>
15+
</property>
16+
<widget class="QWidget" name="centralWidget">
17+
<layout class="QGridLayout" name="gridLayout">
18+
<item row="0" column="0">
19+
<widget class="QCheckBox" name="checkBox">
20+
<property name="sizePolicy">
21+
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
22+
<horstretch>0</horstretch>
23+
<verstretch>0</verstretch>
24+
</sizepolicy>
25+
</property>
26+
<property name="minimumSize">
27+
<size>
28+
<width>120</width>
29+
<height>0</height>
30+
</size>
31+
</property>
32+
<property name="maximumSize">
33+
<size>
34+
<width>120</width>
35+
<height>16777215</height>
36+
</size>
37+
</property>
38+
<property name="text">
39+
<string>選擇台灣城市</string>
40+
</property>
41+
</widget>
42+
</item>
43+
<item row="0" column="1">
44+
<widget class="QComboBox" name="comboBox">
45+
<property name="sizePolicy">
46+
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
47+
<horstretch>0</horstretch>
48+
<verstretch>0</verstretch>
49+
</sizepolicy>
50+
</property>
51+
</widget>
52+
</item>
53+
</layout>
54+
</widget>
55+
<widget class="QMenuBar" name="menuBar">
56+
<property name="geometry">
57+
<rect>
58+
<x>0</x>
59+
<y>0</y>
60+
<width>368</width>
61+
<height>21</height>
62+
</rect>
63+
</property>
64+
</widget>
65+
<widget class="QToolBar" name="mainToolBar">
66+
<attribute name="toolBarArea">
67+
<enum>TopToolBarArea</enum>
68+
</attribute>
69+
<attribute name="toolBarBreak">
70+
<bool>false</bool>
71+
</attribute>
72+
</widget>
73+
<widget class="QStatusBar" name="statusBar"/>
74+
</widget>
75+
<layoutdefault spacing="6" margin="11"/>
76+
<resources/>
77+
<connections/>
78+
</ui>

0 commit comments

Comments
 (0)