<em id="09ttv"></em>
    <sup id="09ttv"><pre id="09ttv"></pre></sup>
    <dd id="09ttv"></dd>

        • QTableView基本用法

          2018-8-17    seo達(dá)人

          如果您想訂閱本博客內(nèi)容,每天自動(dòng)發(fā)到您的郵箱中, 請(qǐng)點(diǎn)這里

          一、添加表頭:

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

          復(fù)制代碼


          二、設(shè)置表格屬性:

          1. ui->tableView->setModel(model); 
          2. //表頭信息顯示居左 
          3. ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignLeft); 
          4. //設(shè)置列寬不可變 
          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);

          復(fù)制代碼


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

          反之則設(shè)置不會(huì)生效。如上述代碼所示。

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

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

          復(fù)制代碼


          四、刪除行:

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

          復(fù)制代碼


          再舉一個(gè)例子:

          在一個(gè)藥品劃價(jià)模塊中有這樣的操作流程:

          檢索處方項(xiàng)目成功后,把該項(xiàng)目顯示到QTableView里,把需要編輯的數(shù)量字段提供給用戶(hù)輸入,用戶(hù)輸入確認(rèn)后,該項(xiàng)目留在列表中,然后開(kāi)始下一項(xiàng)目檢索錄入。

          實(shí)現(xiàn)過(guò)程如下:

          錄入的項(xiàng)目保留在臨時(shí)表tmp中,界面上的QTableView取名為tbList,與tbList關(guān)聯(lián)的Model取名為tb1。檢索成功后,把檢索結(jié)果插入到臨時(shí)表中,把需要編輯的字段提供給用戶(hù)。

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

          復(fù)制代碼


          程序中需要顯示的時(shí)候,

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

          復(fù)制代碼


          程序中需要提供編輯輸入的時(shí)候

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

          復(fù)制代碼


          有一個(gè)問(wèn)題需要注意。向QTableView中添加記錄時(shí),字段一定要完整,不能有空白字段,否則結(jié)果無(wú)法保存。切記。

          如果需要對(duì)用戶(hù)輸入做限制,比如只能在指定的字段輸入指定的數(shù)據(jù)類(lèi)型,可以通過(guò)QItemDelegate來(lái)實(shí)現(xiàn)。

          貼一段代碼,說(shuō)明QTableView基本用法

          1. QStandardItemModel model; 
          2. //設(shè)置大小 
          3. model.setColumnCount(3); //列 
          4. model.setRowCount(musicFound); //行 
          5. //設(shè)置標(biāo)題 
          6. model.setHeaderData(0,Qt::Horizontal,"ID"); 
          7. //添加數(shù)據(jù) 
          8. for(int j=0;j<row;j++)
          9. {
          10.             //寫(xiě)id
          11.             QStandardItem *itemID = new QStandardItem("hello");//QString::number(j)));
          12.             model.setItem(j,0,itemID);
          13. }
          14. //選擇這個(gè)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)模型十分強(qiáng)大,可以利用各種控件來(lái)對(duì)表格的輸入進(jìn)行限制,不過(guò)我以前一直沒(méi)有過(guò),這幾天研究了一下,寫(xiě)個(gè)小例子,希望大家喜歡。
           
          如果看不懂這個(gè)例子,請(qǐng)先看QT的自帶例子:http://qt-project.org/doc/qt-4.8/itemviews-spinboxdelegate.html
           
          思路:
           
          1:為每一列定義委托:
          A:第一列是編號(hào)列,使用只讀委托,令該列的單元格是只讀的
          B:第三列是ID列,只能輸入1-12個(gè)數(shù)字,利用QLineEdit委托和正則表達(dá)式對(duì)輸入進(jìn)行限制
          C:第四年齡列,利用QSpinBox委托進(jìn)行輸入限制,只能輸入1-100之間的數(shù)字
          D:第五列是性別列,利用QComboBox委托對(duì)輸入進(jìn)行限制,該列的單元格只能輸入Male或Female
          E:第六列是頭像列,在該列的單元格中央放置一張頭像
          2:定義代理類(lèi),把所有單元格中的字符居中顯示。
          3:利用QSS,將表格的背景色弄成黃藍(lán)相間。
           
          截圖:
           


          上代碼:
           1.#include <QtGui>   
           2.  
           3.//編號(hào)列,只讀委托   
           4.//這個(gè)方法我還真想不到,呵呵   
           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個(gè)數(shù)字   
           18.//利用QLineEdit委托和正則表達(dá)式對(duì)輸入進(jìn)行限制   
           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委托進(jìn)行輸入限制,只能輸入1-100之間的數(shù)字   
           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委托對(duì)輸入進(jìn)行限制   
           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是在工程目錄中的一張圖片(其實(shí)就是QQ的圖標(biāo)啦,呵呵)   
           133.        QPixmap pixmap = QPixmap("show.bmp").scaled(24, 24);  
           134.        qApp->style()->drawItemPixmap(painter, option.rect,  Qt::AlignCenter, QPixmap(pixmap)); 
           135.    }  
           136.};  
           137.  
           138.//代理類(lèi),把所有單元格中的字符居中顯示   
           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.    //把表格的背景調(diào)成黃藍(lán)相間   
           165.    //這種方法是在網(wǎng)上看到的,用起來(lái)還真方便啊   
           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.} 

          藍(lán)藍(lán)設(shè)計(jì)www.sdgs6788.com )是一家專(zhuān)注而深入的界面設(shè)計(jì)公司,為期望卓越的國(guó)內(nèi)外企業(yè)提供卓越的UI界面設(shè)計(jì)、BS界面設(shè)計(jì) 、 cs界面設(shè)計(jì) 、 ipad界面設(shè)計(jì) 、 包裝設(shè)計(jì) 、 圖標(biāo)定制 、 用戶(hù)體驗(yàn) 、交互設(shè)計(jì)、 網(wǎng)站建設(shè) 平面設(shè)計(jì)服務(wù)

          日歷

          鏈接

          個(gè)人資料

          存檔

          久久精品女人天堂AV麻| 欧美国产成人久久精品| 国产精品女同久久久久电影院| 亚洲国产小视频精品久久久三级 | 国产韩国精品一区二区三区久久| 99久久婷婷国产综合亚洲| 99精品伊人久久久大香线蕉| 久久高清一级毛片| 国内精品综合久久久40p| 久久99久久99小草精品免视看 | 国产色综合久久无码有码| 久久综合综合久久狠狠狠97色88| 亚洲国产精品一区二区三区久久 | 久久久久免费精品国产| 亚洲国产视频久久| 国产一级做a爰片久久毛片| 亚洲国产成人久久精品99| 精品久久777| 亚洲精品无码久久久久去q| 四虎久久影院| 久久国产免费观看精品3| 欧美性猛交xxxx免费看久久久| 久久狠狠高潮亚洲精品| 奇米影视7777久久精品人人爽| 伊人丁香狠狠色综合久久| 国产产无码乱码精品久久鸭| 日本久久中文字幕| 久久精品国产黑森林| 成人免费网站久久久| 久久精品国产亚洲AV香蕉| 亚洲欧美日韩中文久久| 国产香蕉久久精品综合网| 亚洲午夜无码久久久久小说| 久久国产热这里只有精品| 国产成人AV综合久久| 蜜桃麻豆www久久| 91久久福利国产成人精品| 国产激情久久久久影院| 国产精品综合久久第一页 | 人妻少妇久久中文字幕| 久久久久亚洲AV无码网站|