自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

QT學習 向數(shù)據(jù)庫中添加Button

移動開發(fā)
QT學習 向數(shù)據(jù)庫中添加Button 是本文介紹內(nèi)容,數(shù)據(jù)庫的操作,友們應該不陌生,那我們先來看內(nèi)容。

本文介紹的是QT學習 向數(shù)據(jù)庫中添加Button,如果需要對數(shù)據(jù)庫操作,必須在.pro文件中加入:

  1. QT +=sql 

代碼如下:

  1. #include <QString>   
  2. #include <QSqlDatabase>   
  3. #include <QSqlQuery>   
  4. #include <QSqlError>   
  5. #include <QSqlDriver>   
  6. #include <QDateTime>   
  7. #include <QDebug>   
  8. #include <QPushButton>   
  9. #include <QVBoxLayout>   
  10. #include <QToolBox>   
  11.  
  12. #include "mainwindow.h"   
  13. #include "ui_mainwindow.h"   
  14.  
  15. MainWindow::MainWindow(QWidget *parent) :   
  16.     QMainWindow(parent),   
  17.     ui(new Ui::MainWindow)   
  18. {   
  19.     ui->setupUi(this);   
  20.     //下面進行數(shù)據(jù)庫的設置   
  21.    int i;   
  22.    QVBoxLayout *layInstruct = new QVBoxLayout;//ToolBox的Layout   
  23.    QToolBox *tbInstruct=new QToolBox();   
  24.    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");  //使用sqlite數(shù)據(jù)庫驅(qū)動   
  25.    db.setDatabaseName("G:\\SerialInstruct.db3");  //我們之前建立的數(shù)據(jù)庫   
  26.    if (db.open())//嘗試連接數(shù)據(jù)庫   
  27.    {  //已經(jīng)成功連上數(shù)據(jù)庫   
  28.        QSqlQuery query;  //新建一個查詢的實例   
  29.        if (query.exec("select * from UnitData"))   //嘗試列出 student 表的所有記錄   
  30.        {  //本次查詢成功   
  31.            int numRows = 0;  //詢問數(shù)據(jù)庫驅(qū)動,是否驅(qū)動含有某種特性   
  32.            if (db.driver()->hasFeature(QSqlDriver::QuerySize))   
  33.                {   
  34.                    numRows = query.size();  //如果支持結(jié)果影響的行數(shù),那么直接記錄下來   
  35.                }   
  36.            else   
  37.            {   
  38.                query.last(); //否則定位到結(jié)果***,qt 文檔說,這個方法非常慢   
  39.                numRows = query.at() + 1;   
  40.                query.seek(-1);   
  41.            }   
  42.            QString name, age;   
  43.            i=0;   
  44.            while(query.next())   
  45.            {  //定位結(jié)果到下一條記錄   
  46.                name = query.value(0).toString();   
  47.                age = query.value(1).toString();   
  48.                QString result = name + " " + age;   
  49.                QWidget *pages=new QWidget();   
  50.                tbInstruct->addItem(pages,age);   
  51.                QSqlQuery qryInstruct;//查詢子表內(nèi)容,用于創(chuàng)建Button   
  52.                if (qryInstruct.exec(QString("Select ID,Description from InstructList Where UnitID=%1").arg(i+1)))   
  53.                {   
  54.                    tbInstruct->setCurrentIndex(i);   
  55.                    QVBoxLayout *layout = new QVBoxLayout;//建立一個堅排的規(guī)則   
  56.  
  57.                    while(qryInstruct.next())   
  58.                    {   
  59.                        QString sID,sCaption;   
  60.                        sID=qryInstruct.value(0).toString();   
  61.                        sCaption=qryInstruct.value(1).toString();   
  62.                        QPushButton *button = new QPushButton(sCaption);   
  63.                        button->setAccessibleDescription(sID);   
  64.                        button->setCheckable(true);   
  65.                        button->setAutoExclusive(true);   
  66.                        layout->addWidget(button);//把Button放入Layout中   
  67.                    }   
  68.                    layout->setSpacing(0);   
  69.                    pages->setLayout(layout);   
  70.                }   
  71.                ii=i+1;   
  72.            }   
  73.            //ui->toolBox->removeItem(0);   
  74.            tbInstruct->setCurrentIndex(0);   
  75.            layInstruct->addWidget(tbInstruct);   
  76.            layInstruct->setSpacing(0);   
  77.            ui->centralWidget->setLayout(layInstruct);   
  78.        }   
  79.        else   
  80.        {  //如果查詢失敗,用下面的方法得到具體數(shù)據(jù)庫返回的原因   
  81.            QSqlError error = query.lastError();   
  82.            //display.append("From mysql database: " + error.databaseText());   
  83.        }   
  84.    }   
  85.    else   
  86.    {  //打開數(shù)據(jù)庫失敗,顯示數(shù)據(jù)庫返回的失敗描述   
  87.        //display.append("cannot open database.");   
  88.        //display.append("Reason: " + db.lastError().databaseText());   
  89.    }   
  90. }   
  91.  
  92. MainWindow::~MainWindow()   
  93. {   
  94.     delete ui;   
  95. }   
  96.  
  97. void MainWindow::changeEvent(QEvent *e)   
  98. {   
  99.     QMainWindow::changeEvent(e);   
  100.     switch (e->type()) {   
  101.     case QEvent::LanguageChange:   
  102.         ui->retranslateUi(this);   
  103.         break;   
  104.     default:   
  105.         break;   
  106.     }   

小結(jié):關于QT學習 向數(shù)據(jù)庫中添加Button 誰的內(nèi)容介紹完了,希望本文對你有所幫助!更多關于數(shù)據(jù)庫資料請參考請閱讀編輯推薦。

責任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關推薦

2011-08-30 12:59:52

Qt數(shù)據(jù)庫

2011-07-04 18:02:29

QT Sqlite 數(shù)據(jù)庫

2011-08-30 13:33:29

Qt數(shù)據(jù)庫

2011-06-10 10:00:16

Qt 數(shù)據(jù)庫 MySQL

2011-07-01 13:42:24

QT 數(shù)據(jù)庫

2011-08-30 14:25:06

QT數(shù)據(jù)庫

2011-08-30 14:15:34

QTSQLite數(shù)據(jù)庫

2011-06-27 12:56:28

2011-06-24 15:16:33

Qt 插件

2011-07-05 14:46:34

2011-08-30 13:49:57

Qt數(shù)據(jù)庫QTableView

2009-07-20 17:03:55

批量插入數(shù)據(jù)ASP.NET

2010-07-25 10:35:24

Exchange Se

2011-06-23 18:37:02

Qt 數(shù)據(jù)庫

2011-06-21 15:11:04

QT 數(shù)據(jù)庫

2011-06-21 15:31:04

Qt 數(shù)據(jù)庫 SQL

2011-06-28 14:27:38

Qt Qt For Sym

2011-04-12 14:48:38

MySQL數(shù)據(jù)庫

2011-07-05 10:16:16

Qt 數(shù)據(jù)庫 SQLite

2011-07-05 16:08:10

點贊
收藏

51CTO技術棧公眾號