Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4235841
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-07-31 09:46:44

工程代码:  42.rar  

豆子:http://devbean.blog.51cto.com/448512/265057


  1. QVBoxLayout *mainLayout = new QVBoxLayout(this);//添加 主布局
  2. mainLayout->addWidget(listview); //添加 listview
  3. mainLayout->addLayout(btnLayout);//添加 水平布局到主布局中
  4. this->setLayout(mainLayout);


  1. #ifndef MYLISTVIEW_H
  2. #define MYLISTVIEW_H

  3. #include <QtGui>

  4. class MyListView : public QWidget
  5. {
  6.     Q_OBJECT
  7. public:
  8.     MyListView();
  9. private:
  10.     QStringListModel *model;
  11.     QListView *listview;

  12. private slots:
  13.     void insertData();
  14.     void deleteData();
  15.     void showData();
  16. };

  17. #endif // MYLISTVIEW_H

  1. #include "mylistview.h"

  2. MyListView::MyListView()
  3. {
  4.     //QStringListModel就是封装了QStringList的model
  5.     model = new QStringListModel(this);
  6.     QStringList data;
  7.     data <<"Letter A" << "Letter B" << "Leccter C";
  8.     model->setStringList(data);

  9.     listview = new QListView(this);
  10.     listview->setModel(model);

  11.     QHBoxLayout *btnLayout = new QHBoxLayout;//水平布局

  12.     QPushButton *insertBtn = new QPushButton(tr("intsert"),this);
  13.     QPushButton *delBtn = new QPushButton(tr("delete"),this);
  14.     QPushButton *showBtn = new QPushButton(tr("show"),this);

  15.     btnLayout->addWidget(insertBtn); //添加部件到布局中
  16.     btnLayout->addWidget(delBtn);
  17.     btnLayout->addWidget(showBtn);

  18.     QVBoxLayout *mainLayout = new QVBoxLayout(this);//添加主布局
  19.     mainLayout->addWidget(listview); //添加 listview
  20.     mainLayout->addLayout(btnLayout);//添加 水平布局到主布局中
  21.     this->setLayout(mainLayout);

  22.     connect(insertBtn,SIGNAL(clicked()),this,SLOT(insertData()));
  23.     connect(delBtn,SIGNAL(clicked()),this,SLOT(deleteData()));
  24.     connect(showBtn,SIGNAL(clicked()),this,SLOT(showData()));
  25. }

  26. void MyListView::insertData()
  27. {
  28.     bool isOK;
  29.     QString text = QInputDialog::getText(NULL,"insert","Please input new data:",QLineEdit::Normal,"You are inseting new data.",&isOK,0);
  30.     if(isOK)
  31.     {
  32.         // 1. row 2. model 3. data

  33.         //获取哪一行
  34.         int row = listview->currentIndex().row();//listView()->currentIndex()函数,获取QListView当前行
  35.         //插入一行
  36.         model->insertRows(row,1);
  37.         //使用model的index()函数获取当前行的QModelIndex对象,

  38.         QModelIndex index = model->index(row);
  39.         //使用setData()函数把我们用QInputDialog接受的数据插入
  40.         model->setData(index,text);

  41.         listview->setCurrentIndex(index);
  42.         //调用edit()函数,这个函数使得这一行可以被编辑
  43.         listview->edit(index);
  44.     }
  45. }

  46. //在构造函数中,我们只想删除当前行,所以可以调用
  47. //当前索引位置调用removeRows,并且把要删除的行数置为1
  48. //我们需要依赖与模型才能够相应的更新这个视图
  49. void MyListView::deleteData()
  50. {
  51.         if(model->rowCount() > 1) {
  52.                 model->removeRows(listview->currentIndex().row(), 1);
  53.         }
  54. }

  55. void MyListView::showData()
  56. {
  57.         QStringList data = model->stringList();
  58.         QString str;
  59.         foreach(QString s, data) { //重复使用data
  60.                 str += s + "\n";
  61.         }

  62.         QMessageBox::information(this, "Data", str);
  63. }





阅读(1627) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~