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…

知识平台管理系统设计

知识平台管理系统设计是一个综合性的过程&#xff0c;旨在为企业或组织提供一个高效、便捷的知识管理解决方案。以下是知识平台管理系统设计的详细阐述&#xff1a; 一、设计目标和原则 1、设计目标&#xff1a;设计一款功能强大、易于使用、支持多种知识形式分类和搜索的知识管…

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…

Getting started with Kivy

Getting started with Kivy – Kivy Blog This post collates various resources for getting started with the Kivy graphical framework for Python. 这份发表通过 kivy图像框架 来校对不同的资源来开始 Installation 安装 Follow the official installation documentat…

Java零基础-集合:LinkedHashMap

哈喽&#xff0c;各位小伙伴们&#xff0c;你们好呀&#xff0c;我是喵手。运营社区&#xff1a;C站/掘金/腾讯云&#xff1b;欢迎大家常来逛逛 今天我要给大家分享一些自己日常学习到的一些知识点&#xff0c;并以文字的形式跟大家一起交流&#xff0c;互相学习&#xff0c;一…

销毁DisposableBean/@PreDestroy

在Spring框架中&#xff0c;DisposableBean接口和PreDestroy注解都用于处理Bean的销毁逻辑&#xff0c;即在Bean生命周期结束时执行的清理工作。以下是它们各自的使用方式和特点&#xff1a; DisposableBean接口 DisposableBean是一个由Java EE规范定义的接口&#xff0c;Spri…

AI大模型学习路线

阶段1 Python编程基础 主要内容 掌握的核心能力 Python基础语法 Python数据处理 函数 文件读写 异常处理 模块和包1、掌握Python开发环境基本配置&#xff1b; 2、掌握运算符、表达式、流程控制语句、数组等的使用&#xff1b; 3、掌握字符串的基本操作&#xff1b; 4、…

【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;为了方便理…

学习TTS遇到的问题3

学习TTS遇到的问题3 全部chatgpt问答 1. 导出模型的QAT是什么2. src_key_padding_mask3. 模型中的attention_mask是什么参数1. **序列填充**2. **防止信息泄露**attention_mask的形状示例总结 4. 学习率调度器 lr_scheduler为什么需要学习率调度器&#xff1f;常见的学习率调度…