Qt 学习(一) addressbook

Qt Demo: addressbook
(1)创建项目:选择不创建界面,即UI,此时会自动生成的文件如图所示:
在这里插入图片描述
QApplication:
MainWindow 继承自 QMainWindow,根据需要设计的界面样式。

(2)确定MainWindow 的成员变量
在这里插入图片描述
首先,从此张界面图推断出 MainWindow 的组成:

  1. 一个QTabWidget;
  2. 两个QMenu: Files Tools;点击之后弹出的选项为:QAction;QAction 绑定槽函数
    MainWindow 包含的成员对象:
class MainWindow : public QMainWindow
{Q_OBJECT
public:MainWindow();
private slots:void updateActions(const QItemSelection &selection);void openFile();void saveFile();
private:void createMenus();AddressWidget *addressWidget;QMenu *fileMenu;QMenu *toolMenu;QAction *openAct;QAction *saveAct;QAction *exitAct;QAction *addAct;QAction *editAct;QAction *removeAct;
};

(3)在MainWindow中设置 centralwidget 以及 title, 并添加menu 以及 action,再绑定槽函数

setCentralWidget
setWindowTitle
fileMenu = menuBar()->addMenu(tr("&File"));
openAct = new QAction(tr("&Open"),this);
fileMenu->addAction(openAct);
connect(openAct,&QAction::triggered,this,&MainWindow::open_file);

为两个menu 添加了三个可点击的控件,对应的槽函数需要根据代码架构来设计。

// Open file
QString file_name = QFileDialog().getOpenFileName(this);
// Save file
QString file_name = QFileDialog().getSaveFileName(this);

(4)QTabWidget
添加tab: QtableView、 QWidget

QTableView* tableview = new QtableView();
// QTableView 设置一系列属性
// int addTab(QWidget* widget, const QString&);
addTab(tableview, str); 

model-view
QAbstractListModel QAbstractTableModel
索引:QModelIndex
role: ItemDataRole
实现自己的model类:

class tableModel : public QAbstractTableModel
{
public:tableModel(QObject* parent = nullptr);// virtual function
//  must override this 3 funciton when use QAbstractTableModelint rowCount(const QModelIndex &parent) const override;int columnCount(const QModelIndex &parent) const override;QVariant data(const QModelIndex &index, int role) const override;//  must override this 2 funciton when edit dataQt::ItemFlags flags(const QModelIndex &index) const override;bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override;//  must override this 2 funciton when change rowsbool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()) override;bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex()) override;//  must override this 1 funciton when use table headerQVariant headerData(int section, Qt::Orientation orientation, int role) const override;const QVector<Contact> &getContacts() const;private:QVector<Contact> _contacts;
};

(5) 槽函数:添加,修改,删除
每一个{name, address} 都是通过一个 QDialog 添加;
此界面包含的控件:QLineEdit;QTextEdit;QLabel;QPushButton
layout:QGridLayout;QHBoxLayout;QVBoxLayout
在这里插入图片描述

addDialog::addDialog(QWidget* parent)
{_name = new QLineEdit();_address = new QTextEdit();auto nameLabel = new QLabel("Name");auto addressLabel = new QLabel("Address");auto layout = new QGridLayout();layout->setColumnStretch(1,2);layout->addWidget(nameLabel,0,0);layout->addWidget(_name,0,1);layout->addWidget(addressLabel,1,0,Qt::AlignLeft|Qt::AlignTop);layout->addWidget(_address,1,1,Qt::AlignLeft);auto okBtn = new QPushButton("OK");auto cancelBtn = new QPushButton("Cancel");auto btnLayout = new QHBoxLayout;btnLayout->addWidget(okBtn);btnLayout->addWidget(cancelBtn);layout->addLayout(btnLayout,2,1,Qt::AlignRight);auto mainLayout = new QVBoxLayout;mainLayout->addLayout(layout);setLayout(mainLayout);setWindowTitle("Add a Contact");connect(okBtn,&QAbstractButton::clicked,this,&QDialog::accept);connect(cancelBtn, &QAbstractButton::clicked, this, &QDialog::reject);
}

添加:

void addressWidget::show_add_entry_dialog()
{qDebug()<<"show_add_entry_dialog"<<endl;addDialog add_dialog;if(add_dialog.exec()){qDebug()<<"add_dialog.exec()"<<endl;add_entry(add_dialog.name(),add_dialog.address());}
}
void addressWidget::add_entry(const QString& name, const QString& address)
{qDebug()<<"add_entry"<<"name:"<<name<<"address:"<<address<<endl;if(!_table_model->getContacts().contains({name,address})){_table_model->insertRows(0,1,QModelIndex());QModelIndex index = _table_model->index(0,0,QModelIndex());_table_model->setData(index,name,Qt::EditRole);index = _table_model->index(0,1,QModelIndex());_table_model->setData(index,address);removeTab(indexOf(_new_address_tab));}else{qDebug()<<"duplicate name"<<endl;}
}

修改

void AddressWidget::editEntry()
{QTableView *temp = static_cast<QTableView*>(currentWidget());QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());QItemSelectionModel *selectionModel = temp->selectionModel();const QModelIndexList indexes = selectionModel->selectedRows();QString name;QString address;int row = -1;for (const QModelIndex &index : indexes) {row = proxy->mapToSource(index).row();QModelIndex nameIndex = table->index(row, 0, QModelIndex());QVariant varName = table->data(nameIndex, Qt::DisplayRole);name = varName.toString();QModelIndex addressIndex = table->index(row, 1, QModelIndex());QVariant varAddr = table->data(addressIndex, Qt::DisplayRole);address = varAddr.toString();}AddDialog aDialog;aDialog.setWindowTitle(tr("Edit a Contact"));aDialog.editAddress(name, address);if (aDialog.exec()) {const QString newAddress = aDialog.address();if (newAddress != address) {const QModelIndex index = table->index(row, 1, QModelIndex());table->setData(index, newAddress, Qt::EditRole);}}
}

删除

void AddressWidget::removeEntry()
{QTableView *temp = static_cast<QTableView*>(currentWidget());QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());QItemSelectionModel *selectionModel = temp->selectionModel();const QModelIndexList indexes = selectionModel->selectedRows();for (QModelIndex index : indexes) {int row = proxy->mapToSource(index).row();table->removeRows(row, 1, QModelIndex());}if (table->rowCount(QModelIndex()) == 0)insertTab(0, newAddressTab, tr("Address Book"));
}

(6)存储数据

void AddressWidget::writeToFile(const QString &fileName)
{QFile file(fileName);if (!file.open(QIODevice::WriteOnly)) {QMessageBox::information(this, tr("Unable to open file"), file.errorString());return;}QDataStream out(&file);out << table->getContacts();
}

读取数据:

void AddressWidget::readFromFile(const QString &fileName)
{QFile file(fileName);if (!file.open(QIODevice::ReadOnly)) {QMessageBox::information(this, tr("Unable to open file"),file.errorString());return;}QVector<Contact> contacts;QDataStream in(&file);in >> contacts;if (contacts.isEmpty()) {QMessageBox::information(this, tr("No contacts in file"),tr("The file you are attempting to open contains no contacts."));} else {for (const auto &contact: qAsConst(contacts))addEntry(contact.name, contact.address);}
}

Contacts 需要重载流运算符

inline QDataStream &operator<<(QDataStream &stream, const Contact &contact)
{return stream << contact.name << contact.address;
}inline QDataStream &operator>>(QDataStream &stream, Contact &contact)
{return stream >> contact.name >> contact.address;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/34925.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Jetpack架构组件_Navigaiton组件_1.Navigaiton切换Fragment

1.Navigation主要作用 方便管理Fragment &#xff08;1&#xff09;方便我们管理Fragment页面的切换 &#xff08;2&#xff09;可视化的页面导航图&#xff0c;便于理清页面间的关系。 &#xff08;3&#xff09;通过destination和action完成页面间的导航 &#xff08;4&a…

算尽天下财,铸就大明梦 —— 大明钱算子夏元吉的传奇一生

仕途生涯开始&#xff1a;洪武二十三年&#xff08;1390年&#xff09;&#xff0c;夏元吉因精通《诗经》&#xff0c;由湖广乡荐参加礼部组织的会试&#xff0c;虽未中举&#xff0c;但他并未气馁&#xff0c;反而更加努力地学习。洪武二十四年&#xff08;1391年&#xff09;…

通讯:单片机串口和电脑通讯

目录 1.串口输出数据到电脑 硬件部分 串口输出数据到电脑的软件软件部分&#xff1a; 相关问题&#xff1a; 2.单片机串口--485--485转USB--电脑 串口&#xff0c;芯片&#xff0c;转换器&#xff0c;设备之间的通讯的接线&#xff0c;都是要TX--RX, RX--TX 交叉连接。 单…

充电桩现场安装施工流程

摘要&#xff1a;在化石能源日益紧缩的今天&#xff0c;新能源车辆逐渐走进了千家万户&#xff0c;与传统化石能源车辆相比&#xff0c;以电驱动的新能源车辆能源利用率高&#xff0c;缩短了能源传递过程&#xff0c;实现了能源有效利用。根据能量守恒定律可知&#xff0c;任何…

V-Series Avalon-MM DMA Interface for PCIE IP核

目录 1. IP概述 2. Avalon-MM DMA Ports 3. 参数设置 3.1 系统设置 3.2 基址寄存器 (BAR) 设置 3.3 设备识别寄存器 3.4 PCI Express和PCI功能参数 3.4.1 Device Capabilities 3.4.2 Error Reporting 3.4.3 Link Capabilities 3.4.4 MSI and MSI-X Capabilities …

机器发货到菲律宾的完整流程 广东智慧物流

机器发货到菲律宾的完整流程 广东智慧物流 &#x1f31f;【机器发货到菲律宾完全攻略】&#x1f31f; 机器发货到菲律宾的完整流程 广东智慧物流 为你整理了一份超详细的机器发货到菲律宾的攻略&#xff01;海运14天轻松到达&#xff0c;让你无后顾之忧&#xff01;&#x1f…

uniapp地图点击获取位置

主页面 <view class"right-content" click.stop"kilometer(item)"><view class"km">{{item.distance||0}}km</view><image src"../../static/map.png" mode""style"width: 32rpx; height: 32rpx…

【Java Web】Servlet控制器

目录 一、Servlet简介 二、Servlet运行流程 三、Servlet开发流程 四、Servlet-api.jar包导入和Content-Type问题 4.1 Servlet-api.jar导入问题 4.2 Http报文头中的Content-Type属性 五、Servlet_url-pattern请求映射路径设置 5.1 url-pattern方式 5.2 注解方式配置servlet 六、…

QCC51XX---开启手机log日志

QCC51XX---系统学习目录_trbi200软件-CSDN博客 目录 1.Vivo 2.华为 3.小米 4.三星 5.oppo 1.Vivo *#*#112#*#* 输入命令后会进入log日志系统(由于版本原因,界面可能不同),打开log开关,log就会在后台自动录制。 点击设置,则可进入图1(右边)的界面,可以导出log,导出…

Golang | Leetcode Golang题解之第200题岛屿数量

题目&#xff1a; 题解&#xff1a; func numIslands(grid [][]byte) int {res : 0for i : 0; i < len(grid); i {for j : 0; j < len(grid[i]); j {if grid[i][j] 1 {resdfs(grid, i, j)}}}return res }func dfs(grid [][]byte, r, c int) {h, w : len(grid), len(gri…

面试题-CAS(compare and swap)

1.CAS机制 悲观锁&#xff1a;始终假定会发生并发冲突&#xff0c;因此会屏蔽一切可能违反数据完整性的操作。 乐观锁&#xff1a;假设不会发生并发冲突&#xff0c;因此只在提交操作时检查是否违反数据完整性。 执行CAS操作时&#xff0c;比较内存位置的值(主内存的值)与预期…

Labview_映射表

1.创建映射表 创建映射表时&#xff0c;该映射表内的所有键为同一类型、键为同一类型。映射表键名可以为任意类型。 PS:生成映射表在使用时请保证唯一键名&#xff0c;如使用同一键名&#xff0c;则在最终输出时只能搜索到最新插入的键值对信息。 2.插入映射表 按照已创建的映…

数据采集Selenium中的弹窗处理

在爬虫技术中&#xff0c;弹窗处理是一个常见但具有挑战性的问题。Selenium作为一个强大的网页自动化工具&#xff0c;可以帮助我们有效地处理网页中的各种弹窗。本文将概述如何使用Selenium处理弹窗&#xff0c;并提供实现代码&#xff0c;代码中将使用代理IP技术。 概述 弹…

网络编程基础知识拾遗:用大白话解释什么是交换机、路由器、光猫、IP地址和子网掩码、公网和内网IP、端口和域名

二层交换机 在没有二层交换机的环境中&#xff0c;两台电脑或多个电脑之间的通信主要依赖于直连方式或共享介质。假如你和你的舍友都有一台电脑&#xff0c;当你们之间想要进行通信的时候&#xff0c;在没有二层交换机的情况下&#xff0c;可以使用网线&#xff08;为了方便理…

btrace使用记录

关于作者&#xff1a;CSDN内容合伙人、技术专家&#xff0c; 从零开始做日活千万级APP。 专注于分享各领域原创系列文章 &#xff0c;擅长java后端、移动开发、商业变现、人工智能等&#xff0c;希望大家多多支持。 未经允许不得转载 目录 一、导读二、使用三、 推荐阅读 一、导…

线性代数基础概念:向量空间

目录 线性代数基础概念&#xff1a;向量空间 1. 向量空间的定义 2. 向量空间的性质 3. 基底和维数 4. 子空间 5. 向量空间的例子 总结 线性代数基础概念&#xff1a;向量空间 向量空间是线性代数中最基本的概念之一&#xff0c;它为我们提供了一个抽象的框架&#xff0c…

MySQL表解锁

查看锁信息 show full processlist 如果一个表被锁定了&#xff0c;会有一个 “Waiting for table metadata lock” 的提示&#xff0c;表明该表正在等待锁定。 解锁表 删除state上有值的事务 kill query 事务id 表解锁完成

迅为iTOP-2K1000开发板龙芯中科国产64位Loognix主板

硬件配置 国产龙芯处理器&#xff0c;双核64位系统&#xff0c;板载2G DDR3内存&#xff0c;流畅运行Busybox、Buildroot、Loognix、QT5.12 系统! 接口全板载4路USB HOST、2路千兆以太网、2路UART、2路CAN总线、Mini PCIE、SATA固态盘接口、4G接口、GPS接口WIF1、蓝牙、Mini H…

获取个人免费版Ubuntu Pro

首先上官网地址&#xff1a;Ubuntu Pro | Ubuntu 点击页面中的"Get Ubuntu Pro now" 将用途选为“Myself”&#xff0c;在此页面中Ubuntu说明了该版本只面向个人开发者&#xff0c;且最终只允许5台设备免费使用&#xff1b;因而部署设备的抉择就不得不慎重考虑了&am…

基于springboot实现机动车号牌管理系统项目【项目源码+论文说明】计算机毕业设计

基于springboot实现机动车号牌管理系统演示 摘要 在如今社会上&#xff0c;关于信息上面的处理&#xff0c;没有任何一个企业或者个人会忽视&#xff0c;如何让信息急速传递&#xff0c;并且归档储存查询&#xff0c;采用之前的纸张记录模式已经不符合当前使用要求了。所以&am…