回覆列表
  • 1 # CoreCode

    程式碼如下

    #include <QtGui>#include <QtSql>#include "scooterwindow.h"ScooterWindow::ScooterWindow(){ model = new QSqlTableModel(this); model->setTable("scooter"); model->setSort(Scooter_Name, Qt::AscendingOrder); model->setHeaderData(Scooter_Name, Qt::Horizontal, tr("Name")); model->setHeaderData(Scooter_MaxSpeed, Qt::Horizontal, tr("MPH")); model->setHeaderData(Scooter_MaxRange, Qt::Horizontal, tr("Miles")); model->setHeaderData(Scooter_Weight, Qt::Horizontal, tr("Lbs")); model->setHeaderData(Scooter_Description, Qt::Horizontal, tr("Description")); model->select(); view = new QTableView; view->setModel(model); view->setSelectionMode(QAbstractItemView::SingleSelection); view->setSelectionBehavior(QAbstractItemView::SelectRows); view->setColumnHidden(Scooter_Id, true); view->resizeColumnsToContents(); view->setEditTriggers(QAbstractItemView::NoEditTriggers); QHeaderView *header = view->horizontalHeader(); header->setStretchLastSection(true); QHBoxLayout *mainLayout = new QHBoxLayout; mainLayout->addWidget(view); setLayout(mainLayout); setWindowTitle(tr("Scooters"));}#ifndef SCOOTERWINDOW_H#define SCOOTERWINDOW_H#include <QWidget>class QSqlTableModel;class QTableView;enum { Scooter_Id = 0, Scooter_Name =

    1, Scooter_MaxSpeed =

    2, Scooter_MaxRange =

    3, Scooter_Weight =

    4, Scooter_Description = 5};class ScooterWindow : public QWidget{ Q_OBJECTpublic: ScooterWindow();private: QSqlTableModel *model; QTableView *view;};#endif#include <QtGui>#include <QtSql>#include "scooterwindow.h"bool createConnection(){ QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("scooters.dat"); if (!db.open()) { QMessageBox::warning(0, QObject::tr("Database Error"), db.lastError().text()); return false; } return true;}void createFakeData(){ QSqlQuery query; query.exec("DROP TABLE scooter"); query.exec("CREATE TABLE scooter (" "id INTEGER PRIMARY KEY AUTOINCREMENT, " "name VARCHAR(40) NOT NULL, " "maxspeed INTEGER NOT NULL, " "maxrange INTEGER NOT NULL, " "weight INTEGER NOT NULL, " "description VARCHAR(80) NOT NULL)"); query.exec("INSERT INTO scooter (name, maxspeed, " "maxrange, weight, description) " "VALUES ("Mod-Rad 1500", 40, 35, 298, " ""Speedometer, odometer, battery meter, turn signal " "indicator, locking steering column")"); query.exec("INSERT INTO scooter (name, maxspeed, " "maxrange, weight, description) " "VALUES ("Rad2Go Great White E36", 22, 12, 93, " ""10\" airless tires")"); query.exec("INSERT INTO scooter (name, maxspeed, " "maxrange, weight, description) " "VALUES ("X-Treme X360", 21, 14, 59, " ""Cargo rack, foldable")"); query.exec("INSERT INTO scooter (name, maxspeed, " "maxrange, weight, description) " "VALUES ("Vego SX 600", 20, , 76, " ""Two interchangeable batteries, foldable")"); query.exec("INSERT INTO scooter (name, maxspeed, " "maxrange, weight, description) " "VALUES ("Sunbird E Bike", 18, 30, 118, "")"); query.exec("INSERT INTO scooter (name, maxspeed, " "maxrange, weight, description) " "VALUES ("Leopard Shark", 16, 12, 63, " ""Battery indicator, removable seat, foldable")"); query.exec("INSERT INTO scooter (name, maxspeed, " "maxrange, weight, description) " "VALUES ("Vego iQ 450", 15, 0, 60, " ""OUT OF STOCK")"); query.exec("INSERT INTO scooter (name, maxspeed, " "maxrange, weight, description) " "VALUES ("X-Treme X-11", 15, 11

  • 中秋節和大豐收的關聯?
  • iPhone螢幕突然變暗怎麼辦?