zoukankan      html  css  js  c++  java
  • 转载:QT QTableView用法小结

    出自:

      http://blog.chinaunix.net/uid-20382483-id-3518513.html

    QTableView常用于实现数据的表格显示。下面我们如何按步骤实现学生信息表格:

    一 添加表头

        //准备数据模型
        QStandardItemModel *student_model = new QStandardItemModel();
        student_model->setHorizontalHeaderItem(0, new QStandardItem(QObject::tr("Name")));
        student_model->setHorizontalHeaderItem(1, new QStandardItem(QObject::tr("NO.")));
        student_model->setHorizontalHeaderItem(2, new QStandardItem(QObject::tr("Sex")));
        student_model->setHorizontalHeaderItem(3, new QStandardItem(QObject::tr("Age")));
        student_model->setHorizontalHeaderItem(4, new QStandardItem(QObject::tr("College")));
        //利用setModel()方法将数据模型与QTableView绑定
        ui->student_tableview->setModel(student_model);


    二 设置表格属性

        //设置列宽不可变动,即不能通过鼠标拖动增加列宽        
        ui->student_tableview->horizontalHeader()->setResizeMode(0, QHeaderView::Fixed);   
        ui->student_tableview->horizontalHeader()->setResizeMode(1, QHeaderView::Fixed);   
        ui->student_tableview->horizontalHeader()->setResizeMode(2, QHeaderView::Fixed);   
        ui->student_tableview->horizontalHeader()->setResizeMode(3, QHeaderView::Fixed);   
        ui->student_tableview->horizontalHeader()->setResizeMode(4, QHeaderView::Fixed);   

        //设置表格的各列的宽度值        
        ui->student_tableview->setColumnWidth(0,100);    
        ui->student_tableview->setColumnWidth(1,100);    
        ui->student_tableview->setColumnWidth(2,100);    
        ui->student_tableview->setColumnWidth(3,100);    
        ui->student_tableview->setColumnWidth(4,100);       

        //默认显示行头,如果你觉得不美观的话,我们可以将隐藏        
        ui->student_tableview->verticalHeader()->hide();      

        //设置选中时为整行选中        
        ui->student_tableview->setSelectionBehavior(QAbstractItemView::SelectRows);         
          
        //设置表格的单元为只读属性,即不能编辑        
        ui->student_tableview->setEditTriggers(QAbstractItemView::NoEditTriggers);          

        //如果你用在QTableView中使用右键菜单,需启用该属性        
        ui->tstudent_tableview->setContextMenuPolicy(Qt::CustomContextMenu);

     

    三 动态添加行

        在表格中添加行时,我们只需要在model中插入数据即可,一旦model中的数据发生变化,QTabelView显示就会做相应的变动

        //在第一行添加学生张三的个人信息(setItem函数的第一个参数表示行号,第二个表示列号,第三个为要显示的数据)
        student_model->setItem(0, 0, new QStandardItem(“张三"));
        student_model->setItem(0, 1, new QStandardItem("20120202"));
        student_model->setItem(0, 2, new QStandardItem("男"));
        student_model->setItem(0, 3, new QStandardItem("18"));
        student_model->setItem(0, 4, new QStandardItem("土木学院"));


    四 设置数据显示的样式

        //设置单元格文本居中,张三的数据设置为居中显示
        student_model->item(0, 0)->setTextAlignment(Qt::AlignCenter);
        student_model->item(0, 1)->setTextAlignment(Qt::AlignCenter);
        student_model->item(0, 2)->setTextAlignment(Qt::AlignCenter);
        student_model->item(0, 3)->setTextAlignment(Qt::AlignCenter);
        student_model->item(0, 4)->setTextAlignment(Qt::AlignCenter);

        //设置单元格文本颜色,张三的数据设置为红色
        student_model->item(0, 0)->setForeground(QBrush(QColor(255, 0, 0))); 
        student_model->item(0, 1)->setForeground(QBrush(QColor(255, 0, 0))); 
        student_model->item(0, 2)->setForeground(QBrush(QColor(255, 0, 0))); 
        student_model->item(0, 3)->setForeground(QBrush(QColor(255, 0, 0))); 
        student_model->item(0, 4)->setForeground(QBrush(QColor(255, 0, 0)));

        //将字体加粗
        student_model->item(0, 0)->setFont( QFont( "Times", 10, QFont::Black ) );
        student_model->item(0, 1)->setFont( QFont( "Times", 10, QFont::Black ) );
        student_model->item(0, 2)->setFont( QFont( "Times", 10, QFont::Black ) );
        student_model->item(0, 3)->setFont( QFont( "Times", 10, QFont::Black ) );
        student_model->item(0, 4)->setFont( QFont( "Times", 10, QFont::Black ) );

        //设置排序方式,按年龄降序显示
        student_model->sort(3, Qt::DescendingOrder);

     

     

    ①、

    设置选中单个元素、整行、整列。

    ui->student_tableview->setSelectionBehavior(QAbstractItemView::SelectRows);       

    enum QAbstractItemView::SelectionBehavior

    ConstantValueDescription
    QAbstractItemView::SelectItems 0 Selecting single items.
    QAbstractItemView::SelectRows 1 Selecting only rows.
    QAbstractItemView::SelectColumns 2 Selecting only columns.

     

     

    ②、设置是否允许多选:

    ui->student_tableview->setSelectionMode(QAbstractItemView::SingleSelection); 

    enum QAbstractItemView::SelectionMode

    This enum indicates how the view responds to user selections:

    ConstantValueDescription
    QAbstractItemView::SingleSelection 1 When the user selects an item, any already-selected item becomes unselected, and the user cannot unselect the selected item by clicking on it.
    QAbstractItemView::ContiguousSelection 4 When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item.
    QAbstractItemView::ExtendedSelection 3 When the user selects an item in the usual way, the selection is cleared and the new item selected. However, if the user presses the Ctrl key when clicking on an item, the clicked item gets toggled and all other items are left untouched. If the user presses the Shift key while clicking on an item, all items between the current item and the clicked item are selected or unselected, depending on the state of the clicked item. Multiple items can be selected by dragging the mouse over them.
    QAbstractItemView::MultiSelection 2 When the user selects an item in the usual way, the selection status of that item is toggled and the other items are left alone. Multiple items can be toggled by dragging the mouse over them.
    QAbstractItemView::NoSelection 0 Items cannot be selected.

    The most commonly used modes are SingleSelection and ExtendedSelection.

    ③、设置表的单元格的编辑模式

    ui->student_tableview->setEditTriggers(QAbstractItemView.NoEditTriggers)

    enum QAbstractItemView::EditTrigger
    flags QAbstractItemView::EditTriggers

    This enum describes actions which will initiate item editing.

    ConstantValueDescription
    QAbstractItemView::NoEditTriggers 0 No editing possible.
    QAbstractItemView::CurrentChanged 1 Editing start whenever current item changes.
    QAbstractItemView::DoubleClicked 2 Editing starts when an item is double clicked.
    QAbstractItemView::SelectedClicked 4 Editing starts when clicking on an already selected item.
    QAbstractItemView::EditKeyPressed 8 Editing starts when the platform edit key has been pressed over an item.
    QAbstractItemView::AnyKeyPressed 16 Editing starts when any key is pressed over an item.
    QAbstractItemView::AllEditTriggers 31 Editing starts for all above actions.

    The EditTriggers type is a typedef for QFlags<EditTrigger>. It stores an OR combination of EditTrigger values

     QTableView中嵌入组件:http://qimo601.iteye.com/blog/1538364

     

  • 相关阅读:
    android开发:退出程序(对话框、两次返回键退出)
    【转】将HTML5封装成android应用APK 文件若干方法
    Linux语言修改
    Oracle用户常用数据字典
    成本控制:Oracle 优化器内幕
    [转]oraclemerge用法详解
    Show [SQL*Plus]
    【转】cron
    修改Linux主机名
    表空间删除
  • 原文地址:https://www.cnblogs.com/ribavnu/p/4791393.html
Copyright © 2011-2022 走看看