Qt QListView

来源:互联网 发布:瑜伽网络课程 编辑:程序博客网 时间:2024/06/05 07:26

1) dialogimpl.h

#ifndef DIALOGIMPL_H
#define DIALOGIMPL_H
//
#include <QDialog>
#include "ui_dialog.h"
#include "QListView"
#include "QStringListModel"
#include "QHBoxLayout"
#include "QVBoxLayout"
#include "QPushButton"
#include "QInputDialog"
#include "QMessageBox"
//
class DialogImpl : public QDialog, public Ui::Dialog
{
Q_OBJECT
public:
 DialogImpl( QWidget * parent = 0, Qt::WFlags f = 0 );
private:
 QListView *listview;
 QStringListModel *listmodel;
 QHBoxLayout *buttonLayout;
 QVBoxLayout *mainLayout;
 QPushButton *btnAdd;
 QPushButton *btnUpdate;
 QPushButton *btnDelete;
 QPushButton *btnShow;
private slots:
 void AddItem();
 void UpdateItem();
 void DeleteItem();
 void ShowItem();
};
#endif

2) dialogimpl.cpp

#include "dialogimpl.h"
//
DialogImpl::DialogImpl( QWidget * parent, Qt::WFlags f)
 : QDialog(parent, f)
{
 setupUi(this);
 this->setWindowTitle("QListView Demo");
 mainLayout=new QVBoxLayout;
 listview=new QListView;
 QStringList list;
 list<<"one"<<"two"<<"three";
 listmodel=new QStringListModel;
 listmodel->setStringList(list);
 listview->setModel(listmodel);
 mainLayout->addWidget(listview);
 
 buttonLayout=new QHBoxLayout; 
 btnAdd=new QPushButton;
 btnAdd->setText("&Add");
 btnUpdate=new QPushButton;
 btnUpdate->setText("&Update");
 btnDelete=new QPushButton;
 btnDelete->setText("&Delete");
 btnShow=new QPushButton;
 btnShow->setText("&Show");
 buttonLayout->addWidget(btnAdd);
 buttonLayout->addWidget(btnUpdate);
 buttonLayout->addWidget(btnDelete);
 buttonLayout->addWidget(btnShow);
 mainLayout->addLayout(buttonLayout);
 this->setLayout(mainLayout);
 
 connect(btnAdd,SIGNAL(clicked()),this,SLOT(AddItem()));
 connect(btnUpdate,SIGNAL(clicked()),this,SLOT(UpdateItem()));
 connect(btnDelete,SIGNAL(clicked()),this,SLOT(DeleteItem()));
 connect(btnShow,SIGNAL(clicked()),this,SLOT(ShowItem()));
}
void DialogImpl::AddItem()
{
 bool ok;
 QString text = QInputDialog::getText(this, tr("add item"),
          tr("please add item"), QLineEdit::Normal,
          "", &ok);
 if (ok && !text.isEmpty())
 {
  int row=listview->currentIndex().row();//get current row by current index
  if(row==-1)//no row in QListview
  {
   row=0;
  }
  listmodel->insertRows(row,1);//insert en empty row
  QModelIndex index=listmodel->index(row);//get current row's index
  listmodel->setData(index,text,Qt::EditRole);//set data
  listview->setCurrentIndex(index);//set current index in listview
 }
}
void DialogImpl::UpdateItem()
{
 int row=listview->currentIndex().row();
 if(row!=-1)
 {
  QModelIndex index=listmodel->index(row);
  QString str=listmodel->data(index,Qt::DisplayRole).toString();//get data
  bool ok;
  QString text = QInputDialog::getText(this, tr("edit item"),
          tr("please edit item"), QLineEdit::Normal,
          str, &ok);
  if (ok && !text.isEmpty())
  {
   listmodel->setData(index,text,Qt::EditRole);
   listview->setCurrentIndex(index);
  }
  
 }
 else
 {
  QMessageBox::information(NULL,tr("information"),tr("please select a item"));
 }
}
void DialogImpl::DeleteItem()
{
 int row=listview->currentIndex().row();
 if(row!=-1)
 {
  int ret = QMessageBox::question(this, tr("question"),
        tr("are sure to delete?"),
        QMessageBox::Yes | QMessageBox::No,
        QMessageBox::No);
  if(ret==QMessageBox::Yes)
  {
   listmodel->removeRows(row,1);//remove row
  }
 }
 else//no row in QListView
 {
  QMessageBox::information(NULL,tr("information"),tr("please select a item"));
 }
}
void DialogImpl::ShowItem(){
 int row=listview->currentIndex().row();
 if(row!=-1)
 {
  QModelIndex index=listmodel->index(row);
  QString str=listmodel->data(index,Qt::DisplayRole).toString();
  QMessageBox::information(NULL,tr("information"),str);
 }
 else
 {
  QMessageBox::information(NULL,tr("information"),tr("please select a item"));
 }
}
//

原创粉丝点击