• <center id="sm46c"></center>
  • <dfn id="sm46c"></dfn>
  • <strike id="sm46c"></strike>
  • <cite id="sm46c"><source id="sm46c"></source></cite>
    • <strike id="sm46c"><source id="sm46c"></source></strike>
      <option id="sm46c"></option>
      国产精品天天看天天狠,女高中生强奷系列在线播放,久久无码免费的a毛片大全,国产日韩综合av在线,亚洲国产中文综合专区在,特殊重囗味sm在线观看无码,中文字幕一区二区三区四区在线,无码任你躁久久久久久老妇蜜桃

      QTableView基本用法

      2018-8-17    seo達人

      如果您想訂閱本博客內容,每天自動發到您的郵箱中, 請點這里

      一、添加表頭:

      1. QStandardItemModel *model = new QStandardItemModel(); 
      2. model->setColumnCount(2); 
      3. model->setHeaderData(0,Qt::Horizontal,QString::fromLocal8Bit("卡號")); 
      4. model->setHeaderData(1,Qt::Horizontal,QString::fromLocal8Bit("姓名"));

      復制代碼


      二、設置表格屬性:

      1. ui->tableView->setModel(model); 
      2. //表頭信息顯示居左 
      3. ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft); 
      4. //設置列寬不可變 
      5. ui->tableView->horizontalHeader()->setResizeMode(0,QHeaderView::Fixed); 
      6. ui->tableView->horizontalHeader()->setResizeMode(1,QHeaderView::Fixed); 
      7. ui->tableView->setColumnWidth(0,101); 
      8. ui->tableView->setColumnWidth(1,102);

      復制代碼


      注:在進行表格設置時必須是“ui->tableView->setModel(model);”在前,屬性具體設置在后,

      反之則設置不會生效。如上述代碼所示。

      三、添加行(添加三行一樣的信息):

      1. for(int i = 0; i < 3; i++) 
      2.      model->setItem(i,0,new QStandardItem("2009441676")); 
      3.         //設置字符顏色 
      4.      model->item(i,0)->setForeground(QBrush(QColor(255, 0, 0))); 
      5.         //設置字符位置 
      6.      model->item(i,0)->setTextAlignment(Qt::AlignCenter); 
      7.      model->setItem(i,1,new QStandardItem(QString::fromLocal8Bit("哈哈"))); 
      8. }

      復制代碼


      四、刪除行:

      1. //x是指定刪除哪一行 
      2. model->removeRow(x); 
      3. //刪除所有行 
      4. model->removeRows(0,model->rowCount());

      復制代碼


      再舉一個例子:

      在一個藥品劃價模塊中有這樣的操作流程:

      檢索處方項目成功后,把該項目顯示到QTableView里,把需要編輯的數量字段提供給用戶輸入,用戶輸入確認后,該項目留在列表中,然后開始下一項目檢索錄入。

      實現過程如下:

      錄入的項目保留在臨時表tmp中,界面上的QTableView取名為tbList,與tbList關聯的Model取名為tb1。檢索成功后,把檢索結果插入到臨時表中,把需要編輯的字段提供給用戶。

      1. tb1=newQSqlTableModel(this,*dbR); //dbR是本應用中的數據源 
      2. tb1->setTable("tmp"); //處方臨時表

      復制代碼


      程序中需要顯示的時候,

      1. tbList->setModel(NULL); //清除原先數據集 
      2. tbList->setModel(tb1); //刷新顯示

      復制代碼


      程序中需要提供編輯輸入的時候

      1. QModelIndexmdidx=m_ui->tbList->model()->index(row,column); //獲得需要編輯的單元格的位置 
      2. m_ui->tbList->setFocus(); //把輸入焦點交給tbList 
      3. m_ui->tbList->setCurrentIndex(mdidx); //設定需要編輯的單元格 
      4. m_ui->tbList->edit(mdidx); //開始編輯

      復制代碼


      有一個問題需要注意。向QTableView中添加記錄時,字段一定要完整,不能有空白字段,否則結果無法保存。切記。

      如果需要對用戶輸入做限制,比如只能在指定的字段輸入指定的數據類型,可以通過QItemDelegate來實現。

      貼一段代碼,說明QTableView基本用法

      1. QStandardItemModel model; 
      2. //設置大小 
      3. model.setColumnCount(3); //列 
      4. model.setRowCount(musicFound); //行 
      5. //設置標題 
      6. model.setHeaderData(0,Qt::Horizontal,"ID"); 
      7. //添加數據 
      8. for(int j=0;j<row;j++)
      9. {
      10.             //寫id
      11.             QStandardItem *itemID = new QStandardItem("hello");//QString::number(j)));
      12.             model.setItem(j,0,itemID);
      13. }
      14. //選擇這個model 
      15. m_ui->tableView->setModel(&model); 
      16. //隱藏左邊那列 
      17. m_ui->tableView->verticalHeader()->hide(); 
      18. //列寬 
      19. m_ui->tableView->setColumnWidth(0,30); 
      20. //整行選擇 
      21. m_ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);

       

       

      QT的MVC(View/Delegate)模型十分強大,可以利用各種控件來對表格的輸入進行限制,不過我以前一直沒有過,這幾天研究了一下,寫個小例子,希望大家喜歡。
       
      如果看不懂這個例子,請先看QT的自帶例子:http://qt-project.org/doc/qt-4.8/itemviews-spinboxdelegate.html
       
      思路:
       
      1:為每一列定義委托:
      A:第一列是編號列,使用只讀委托,令該列的單元格是只讀的
      B:第三列是ID列,只能輸入1-12個數字,利用QLineEdit委托和正則表達式對輸入進行限制
      C:第四年齡列,利用QSpinBox委托進行輸入限制,只能輸入1-100之間的數字
      D:第五列是性別列,利用QComboBox委托對輸入進行限制,該列的單元格只能輸入Male或Female
      E:第六列是頭像列,在該列的單元格中央放置一張頭像
      2:定義代理類,把所有單元格中的字符居中顯示。
      3:利用QSS,將表格的背景色弄成黃藍相間。
       
      截圖:
       


      上代碼:
       1.#include <QtGui>   
       2.  
       3.//編號列,只讀委托   
       4.//這個方法我還真想不到,呵呵   
       5.class ReadOnlyDelegate : public QItemDelegate  
       6.{  
       7.    Q_OBJECT  
       8.public:  
       9.    ReadOnlyDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
       10.    QWidget *createEditor(QWidget*parent, const QStyleOptionViewItem &option, 
       11.        const QModelIndex &index) const  
       12.    {  
       13.        return NULL;  
       14.    }  
       15.};  
       16.  
       17.//ID列,只能輸入1-12個數字   
       18.//利用QLineEdit委托和正則表達式對輸入進行限制   
       19.class UserIDDelegate : public QItemDelegate  
       20.{  
       21.    Q_OBJECT  
       22.public:  
       23.    UserIDDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
       24.    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, 
       25.        const QModelIndex &index) const  
       26.    {  
       27.        QLineEdit *editor = new QLineEdit(parent);  
       28.        QRegExp regExp("[0-9]{0,10}");  
       29.        editor->setValidator(new QRegExpValidator(regExp, parent));  
       30.        return editor;  
       31.    }  
       32.    void setEditorData(QWidget *editor, const QModelIndex &index) const  
       33.    {  
       34.        QString text = index.model()->data(index, Qt::EditRole).toString();  
       35.        QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);  
       36.        lineEdit->setText(text);  
       37.    }  
       38.    void setModelData(QWidget *editor, QAbstractItemModel *model,  
       39.        const QModelIndex &index) const  
       40.    {  
       41.        QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);  
       42.        QString text = lineEdit->text();  
       43.        model->setData(index, text, Qt::EditRole);  
       44.    }  
       45.    void updateEditorGeometry(QWidget *editor,  
       46.        const QStyleOptionViewItem &option, const QModelIndex &index) const  
       47.    {  
       48.        editor->setGeometry(option.rect);  
       49.    }  
       50.};  
       51.  
       52.//年齡列,利用QSpinBox委托進行輸入限制,只能輸入1-100之間的數字   
       53.class AgeDelegate : public QItemDelegate  
       54.{  
       55.    Q_OBJECT  
       56.public:  
       57.    AgeDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
       58.    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, 
       59.        const QModelIndex &index) const  
       60.    {  
       61.        QSpinBox *editor = new QSpinBox(parent);  
       62.        editor->setMinimum(1);  
       63.        editor->setMaximum(100);  
       64.        return editor;  
       65.    }  
       66.    void setEditorData(QWidget *editor, const QModelIndex &index) const  
       67.    {  
       68.        int value = index.model()->data(index, Qt::EditRole).toInt();  
       69.        QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  
       70.        spinBox->setValue(value);  
       71.    }  
       72.    void setModelData(QWidget *editor, QAbstractItemModel *model,  
       73.        const QModelIndex &index) const  
       74.    {  
       75.        QSpinBox *spinBox = static_cast<QSpinBox*>(editor);  
       76.        spinBox->interpretText();  
       77.        int value = spinBox->value();  
       78.        model->setData(index, value, Qt::EditRole);  
       79.    }  
       80.    void updateEditorGeometry(QWidget *editor,  
       81.        const QStyleOptionViewItem &option, const QModelIndex &index) const  
       82.    {  
       83.        editor->setGeometry(option.rect);  
       84.    }  
       85.};  
       86.  
       87.//性別列,利用QComboBox委托對輸入進行限制   
       88.//這一列的單元格只能輸入Male或Female   
       89.class SexDelegate : public QItemDelegate  
       90.{  
       91.    Q_OBJECT  
       92.public:  
       93.    SexDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
       94.    QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, 
       95.        const QModelIndex &index) const  
       96.    {  
       97.        QComboBox *editor = new QComboBox(parent);  
       98.        editor->addItem("Female");  
       99.        editor->addItem("Male");  
       100.        return editor;  
       101.    }  
       102.    void setEditorData(QWidget *editor, const QModelIndex &index) const  
       103.    {  
       104.        QString text = index.model()->data(index, Qt::EditRole).toString(); 
       105.        QComboBox *comboBox = static_cast<QComboBox*>(editor);  
       106.        int tindex = comboBox->findText(text);  
       107.        comboBox->setCurrentIndex(tindex);  
       108.    }  
       109.    void setModelData(QWidget *editor, QAbstractItemModel *model,  
       110.        const QModelIndex &index) const  
       111.    {  
       112.        QComboBox *comboBox = static_cast<QComboBox*>(editor);  
       113.        QString text = comboBox->currentText();  
       114.        model->setData(index, text, Qt::EditRole);  
       115.    }  
       116.    void updateEditorGeometry(QWidget *editor,  
       117.        const QStyleOptionViewItem &option, const QModelIndex &index) const 
       118.    {  
       119.        editor->setGeometry(option.rect);  
       120.    }  
       121.};  
       122.  
       123.//頭像列,只是在單元格中央放一張小圖而已   
       124.class IconDelegate : public QItemDelegate  
       125.{  
       126.    Q_OBJECT  
       127.public:  
       128.    IconDelegate(QObject *parent = 0): QItemDelegate(parent) { }  
       129.    void paint(QPainter *painter, const QStyleOptionViewItem &option,  
       130.        const QModelIndex & index ) const  
       131.    {  
       132.        //show.bmp是在工程目錄中的一張圖片(其實就是QQ的圖標啦,呵呵)   
       133.        QPixmap pixmap = QPixmap("show.bmp").scaled(24, 24);  
       134.        qApp->style()->drawItemPixmap(painter, option.rect,  Qt::AlignCenter, QPixmap(pixmap)); 
       135.    }  
       136.};  
       137.  
       138.//代理類,把所有單元格中的字符居中顯示   
       139.class VIPModel : public QStandardItemModel  
       140.{  
       141.    Q_OBJECT  
       142.public:  
       143.    VIPModel(QObject *parent=NULL) : QStandardItemModel(parent) { }  
       144.    VIPModel(int row, int column, QObject *parent=NULL)  
       145.        : QStandardItemModel(row, column, parent) { }  
       146.    QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const 
       147.    {  
       148.        if( Qt::TextAlignmentRole == role )  
       149.            return Qt::AlignCenter;   
       150.        return QStandardItemModel::data(index, role);  
       151.    }  
       152.  
       153.};  
       154.  
       155.#include "main.moc"   
       156.  
       157.int main(int argc, char *argv[])  
       158.{  
       159.    QApplication app(argc, argv);  
       160.  
       161.    VIPModel *model = new VIPModel(5, 5);  
       162.    QTableView *tableView = new QTableView;  
       163.  
       164.    //把表格的背景調成黃藍相間   
       165.    //這種方法是在網上看到的,用起來還真方便啊   
       166.    tableView->setAlternatingRowColors(true);  
       167.    tableView->setStyleSheet("QTableView{background-color: rgb(250, 250, 115);" 
       168.        "alternate-background-color: rgb(141, 163, 215);}");  
       169.  
       170.    tableView->setWindowTitle("VIP List");  
       171.    tableView->resize(700, 400);  
       172.    tableView->setModel(model);  
       173.    QStringList headerList;  
       174.    headerList << "No." << "ID" << "Name" << "Age" << "Sex" << "Show";  
       175.    model->setHorizontalHeaderLabels(headerList);  
       176.    tableView->verticalHeader()->setVisible(false);  
       177.    tableView->horizontalHeader()->setStretchLastSection(true);  
       178.  
       179.    //為每一列加載委托   
       180.    ReadOnlyDelegate readOnlyDelegate;  
       181.    tableView->setItemDelegateForColumn(0, &readOnlyDelegate);  
       182.    UserIDDelegate userIDDelegate;  
       183.    tableView->setItemDelegateForColumn(1, &userIDDelegate);  
       184.    AgeDelegate spinBoxDelegate;  
       185.    tableView->setItemDelegateForColumn(3, &spinBoxDelegate);  
       186.    SexDelegate comboBoxDelegate;  
       187.    tableView->setItemDelegateForColumn(4, &comboBoxDelegate);  
       188.    IconDelegate iconDelegate;  
       189.    tableView->setItemDelegateForColumn(5, &iconDelegate);  
       190.  
       191.    for(int i=0; i<10; i++)  
       192.    {  
       193.        QModelIndex index = model->index(i, 0, QModelIndex());  
       194.        model->setData(index, i);  
       195.    }  
       196.  
       197.    tableView->show();  
       198.    return app.exec();  
       199.} 

      藍藍設計www.li-bodun.cn )是一家專注而深入的界面設計公司,為期望卓越的國內外企業提供卓越的UI界面設計、BS界面設計 、 cs界面設計 、 ipad界面設計 、 包裝設計 、 圖標定制 、 用戶體驗 、交互設計、 網站建設 平面設計服務

      日歷

      鏈接

      個人資料

      藍藍設計的小編 http://www.li-bodun.cn

      存檔

      主站蜘蛛池模板: 国产精品二区中文字幕| 义乌市| 东京热无码av一区二区| 亚洲综合另类小说专区| 在线涩涩免费观看国产精品| 乱人伦中文字幕在线| 日本高清成本人视频一区| 日本无码欧美一区精品久久| 国产亚洲一级特黄大片在线| 国产人成激情视频在线观看 | 亚洲色大成网站www永久男同| 久久久久人妻精品一区二区三区| 亚洲国产精品线观看不卡| 夜夜爽夜夜叫夜夜高潮| 免费观看四虎精品国产地址| 濉溪县| 91精品国产午夜福利| 午夜精品久久久久成人| 麻豆精品一区二区综合av| 无码国产精品一区二区免费i6| 日本视频高清一区二区三区| 日本手机在线| 欧美另类 自拍 亚洲 图区| 国产裸拍裸体视频在线观看| 国产又大又硬又粗| 国产二区三区不卡免费| 久久国产精品国产自线拍| 精品 无码 国产观看| 草久久免费视频| 午夜福利片1000无码免费| 欧美日韩不卡高清在线看| 四虎影视永久无码精品| 精品丝袜美腿国产一区 | 亚洲高清成人av在线| 国产综合自拍| 国产精品普通话国语对白露脸| 日韩区一区二区三区视频| 麻豆人妻少妇精品无码专区| 变态sm天堂无码专区| 日韩一二三无码专区| 久久久久亚洲波多野结衣|