• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            posts - 10,  comments - 2,  trackbacks - 0

            此筆記在此不再提供修改,有變更,以CSDN上的為準(zhǔn),此處為鏈接:http://blog.csdn.net/reborntercel/article/details/7004278

                 QDataWidgetMapper將一個數(shù)據(jù)庫記錄字段反映到其映射的窗口部件中,同時將窗口部件中所做出的更改反映回數(shù)據(jù)庫,關(guān)鍵是關(guān)聯(lián)一個model和一組widget
            一、步驟

            1、創(chuàng)建 QDataWidgetMapper 對象
            2、關(guān)聯(lián) model
            3、關(guān)聯(lián) widgets,并創(chuàng)建其與model中section的映射
            4、定位到某個record

            1. QDataWidgetMapper *mapper = new QDataWidgetMapper;  
            2. mapper->setModel(model);  
            3. mapper->addMapping(mySpinBox, 0);  
            4. mapper->addMapping(myLineEdit, 1);  
            5. mapper->toFirst();  


            提交方式可以設(shè)為手動:

            1. mapper->setSubmitPolicy(QDataWidgetMapper::ManualSubmit);  


             QComboBox組件的mapper比較特殊

            第一種、在關(guān)系模型中實現(xiàn)mapper到QComboBox組件

            1. QSqlRelationalTableModel *model = QSqlRelationalTableModel(this);  
            2.   
            3. model->setTable("員工表");  
            4. model->setRelation(dep_id,QSqlRelation("部門表","id","name"));  
            5.  // ... 其它代碼   
            6.   
            7. //QComboBox與QListWidget很相擬,因為它有一個內(nèi)部模型去保存它的數(shù)據(jù)條目,所以我們用自己建的模型代替那個自帶的模型。給出QSqlRelationalTableModel使用的關(guān)系模型,這個模型有兩列,必須指出組合框應(yīng)該顯示哪一列   
            8. QSqlTableModel *relationModel = model->relationModel(dep_id); // 部門ID   
            9. comboBox->setMode(relationModel);  
            10. comboBox->setModelColumn(relationModel->fieldIndex("name")); // 使用字段名得到正確的標(biāo)題索引,以使組合框顯示部門名  

             

            第二種、使用代理的方式

            1、實現(xiàn)自定義代理類,實現(xiàn)setEditorData()和setModelData()

            2、給模型添加我們自己的代理類對象

            1. MapDelegate *delegate = new MapDelegate(this); // 生成自定義的代理類對象   
            2. mapper->setItemDelegate(delegate); // 給模型添加我們自己的代理類  
            1. void MapDelegate::setEditorData(QWidget *editor, const QModelIndex& index) const{  
            2.     if(index.column() == 0) // 假如模型的第0列為公司名    
            3.     {  
            4.         QComboBox *comboEditor = qobject_cast<QComboBox *>(editor);  
            5.         if (comboEditor)  
            6.         {  
            7.             int i = comboEditor->findText(index.model()->data(index, Qt::EditRole).toString()); // 在comboBox組件中查找model中的當(dāng)前公司名   
            8.        comboEditor->setCurrentIndex(i); // 設(shè)成model中的當(dāng)前公司名   
            9.         }         
            10.     }  
            11.     else  
            12.     {  
            13.         return QItemDelegate::setEditorData(editor, index);  
            14.     }  
            15. }  
            16.   
            17. void MapDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex& index) const  
            18. {  
            19.     if(index.column() == 0)  
            20.     {  
            21.         QComboBox *comboBox = qobject_cast<QComboBox *>(editor);  
            22.         if(comboBox)  
            23.         {  
            24.             model->setData(index, comboBox->currentText());  
            25.         }  
            26.     }  
            27.     else  
            28.     {  
            29.         return QItemDelegate::setModelData(editor, model, index);  
            30.     }  
            31. }  
            posted on 2011-11-23 16:32 再生的雄鷹 閱讀(4693) 評論(0)  編輯 收藏 引用

            <2011年11月>
            303112345
            6789101112
            13141516171819
            20212223242526
            27282930123
            45678910

            常用鏈接

            留言簿(1)

            隨筆檔案

            文章分類

            文章檔案

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

            少妇久久久久久久久久| 99久久亚洲综合精品网站| 丁香久久婷婷国产午夜视频| 色偷偷久久一区二区三区| 精品久久久久久亚洲精品 | 久久亚洲中文字幕精品有坂深雪| 久久久久久久久久久| 久久99精品国产| av色综合久久天堂av色综合在 | 久久婷婷成人综合色综合| 色综合久久天天综合| 亚洲AV无码久久精品成人| 亚洲国产小视频精品久久久三级| 久久综合亚洲欧美成人| 久久黄视频| 精品乱码久久久久久久| 亚洲女久久久噜噜噜熟女| 91精品国产91久久久久久青草| 久久精品国产亚洲7777| 高清免费久久午夜精品| 99久久国产亚洲高清观看2024| 久久亚洲中文字幕精品一区四| 国产精品一久久香蕉国产线看观看| 欧美va久久久噜噜噜久久| 伊人色综合九久久天天蜜桃 | 一本大道久久a久久精品综合| 伊人久久大香线蕉成人| 久久久久人妻一区精品 | av色综合久久天堂av色综合在| 亚洲国产成人精品女人久久久 | 国产精品美女久久久久av爽 | 国产91久久精品一区二区| 久久久女人与动物群交毛片| 成人免费网站久久久| 久久久久亚洲精品中文字幕| 精产国品久久一二三产区区别| 日韩精品久久久久久免费| 国产精品日韩欧美久久综合| 精品国产日韩久久亚洲| 国产精品久久久久天天影视 | 人人狠狠综合88综合久久|