Qt QListView

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"));
 }
}
//

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值