#include "c.h"
c::c(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
this->setWindowTitle(QString::fromLocal8Bit("通讯录"));
this->setFixedSize(300,600);
//初始化对象.
m_dialog = new PeopleMesDialog(this);
//初始化容器和布局.
m_listWidget = new QListWidget();
QHBoxLayout *layout = new QHBoxLayout(this);
layout->addWidget(m_listWidget);
addPeoMes("st", "15158047646", QString::fromLocal8Bit("杭州滨江xxx号"), "");
addPeoMes(QString::fromLocal8Bit("成龙"), "15158047647", QString::fromLocal8Bit("北京xxx号"), "");
addPeoMes(QString::fromLocal8Bit("刘德华"), "15158047648", QString::fromLocal8Bit("香港xxx号"), "");
addPeoMes(QString::fromLocal8Bit("古天乐"), "15158047649", QString::fromLocal8Bit("湖南滨江xxx号"), "");
addPeoMes(QString::fromLocal8Bit("张家辉"), "15158047650", QString::fromLocal8Bit("贪玩蓝月xxx号"), "");
m_listWidget->setItemDelegate(new MyItemDrawer(m_listWidget));
m_listWidget->setContextMenuPolicy(Qt::CustomContextMenu);
m_addAction = new QAction(QString::fromLocal8Bit("新增"), this);
m_delAction = new QAction(QString::fromLocal8Bit("删除"), this);
m_insertAction = new QAction(QString::fromLocal8Bit("插入"), this);
connect(m_listWidget, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(showContextMenuSlot(const QPoint&)));
connect(m_addAction, SIGNAL(triggered()), this, SLOT(addItemSlot()));
connect(m_insertAction, SIGNAL(triggered()), this, SLOT(insertItemSlot()));
connect(m_delAction, SIGNAL(triggered()), this, SLOT(delItemSlot()));
}
c::~c()
{
delete m_listWidget;
}
void c::showContextMenuSlot(const QPoint& pos)
{
m_menu = new QMenu;
//如果鼠标在项上.
if (m_listWidget->itemAt(pos))
{
//记录.
m_selectedItem = m_listWidget->itemAt(pos);
m_insertIndex = m_listWidget->row(m_selectedItem);
m_menu->addAction(m_insertAction);
m_menu->addAction(m_delAction);
}
else
{
m_menu->addAction(m_addAction);
}
m_menu->exec(QCursor::pos());
}
void c::addItemSlot()
{
int status = m_dialog->exec();
//如果用户选择的是保存.
if (status == QDialog::Accepted)
{
QString name = m_dialog->getName();
QString phone = m_dialog->getPhone();
QString address = m_dialog->getAddress();
QString image = m_dialog->getImage();
auto item = setItemValue(name, phone, address, image);
m_listWidget->addItem(item);
}
//进行清空处理.
m_dialog->clear();
}
void c::insertItemSlot()
{
int status = m_dialog->exec();
//如果用户选择的是保存.
if (status == QDialog::Accepted)
{
QString name = m_dialog->getName();
QString phone = m_dialog->getPhone();
QString address = m_dialog->getAddress();
QString image = m_dialog->getImage();
auto item = setItemValue(name, phone, address, image);
m_listWidget->insertItem(m_insertIndex - 1, item);
}
//进行清空处理.
m_dialog->clear();
}
void c::delItemSlot()
{
//删除选中项.
int index = m_listWidget->row(m_selectedItem);
QListWidgetItem *tempItem = m_listWidget->takeItem(index);
delete tempItem;
m_selectedItem = NULL;
}
QListWidgetItem* c::setItemValue(QString name, QString phone, QString address, QString pixmap)
{
QListWidgetItem *item = new QListWidgetItem();
item->setData(Qt::UserRole, name);
item->setData(Qt::UserRole + 1, phone);
item->setData(Qt::UserRole + 2, address);
item->setData(Qt::UserRole + 3, pixmap);
return item;
}
void c::insertPeoMes(QString name, QString phone, QString address, QString pixmap)
{
auto item = setItemValue(name, phone, address, pixmap);
m_listWidget->insertItem(m_insertIndex, item);
}
void c::addPeoMes(QString name, QString phone, QString address, QString pixmap)
{
auto item = setItemValue(name, phone, address, pixmap);
m_listWidget->addItem(item);
}
评论0
最新资源