Qt 二进制文件的读写
开发工具:VS2013 +QT5.8.0
实例功能概述
1、新建项目“sample7_2binFile”
完成以上步骤后,生成以下文件:
2、界面设计
如何添加资源文件:
鼠标双击“***.qrc”文件 弹出以下界面:
点击 “Add File” 找到要添加的 icons 所在位置,全选,“打开” 即添加上,别忘了点 “保存”
如何在工具栏添加QAction
先在菜单栏添加需要添加的QAction,然后把它拖放到工具栏上
可视化UI设计,完成后的界面如下图所示:
添加 Qt Class “QWComboBoxDelegate”
QWComboBoxDelegate.h 文件
#pragma once
#include <QItemDelegate>
class QWComboBoxDelegate : public QItemDelegate
{
Q_OBJECT
public:
QWComboBoxDelegate(QObject *parent=0);
~QWComboBoxDelegate();
//自定义代理组件必须继承以下4个函数
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const Q_DECL_OVERRIDE;
void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const Q_DECL_OVERRIDE;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &index) const Q_DECL_OVERRIDE;
};
QWComboBoxDelegate.cpp 文件
#include "QWComboBoxDelegate.h"
#include <QComboBox>
//解决QT中中文显示乱码问题
#pragma execution_character_set("utf-8")
QWComboBoxDelegate::QWComboBoxDelegate(QObject *parent)
: QItemDelegate(parent)
{
}
QWComboBoxDelegate::~QWComboBoxDelegate()
{
}
QWidget *QWComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index);
QComboBox *editor = new QComboBox(parent);
editor->addItem("优");
editor->addItem("良");
editor->addItem("一般");
editor->addItem("不合格");
return editor;
}
void QWComboBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
QString str = index.model()->data(index, Qt::EditRole).toString();
QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->setCurrentText(str);
}
void QWComboBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
QComboBox *comboBox = static_cast<QComboBox*>(editor);
QString str = comboBox->currentText();
model->setData(index, str, Qt::EditRole);
}
void QWComboBoxDelegate::updateEditorGeometry(QWidget *editor,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(index);
editor->setGeometry(option.rect);
}
添加 Qt Class “QWFloatSpinDelegate”
QWFloatSpinDelegate.h 文件
#pragma once
#include <QStyledItemDelegate>
class QWFloatSpinDelegate : public QStyledItemDelegate
{
Q_OBJECT
public:
QWFloatSpinDelegate(QObject *parent=0);
~QWFloatSpinDelegate();
//自定义代理组件必须继承以下4个函数
//创建编辑组件
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const Q_DECL_OVERRIDE;
void setEditorData(QWidget *editor, const QModelIndex &index) const Q_DECL_OVERRIDE;
void setModelData(QWidget *editor, QAbstractItemModel *model,
const QModelIndex &index) const Q_DECL_OVERRIDE;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option,
const QModelIndex &index) const Q_DECL_OVERRIDE;
};
QWFloatSpinDelegate.cpp 文件
#include "QWFloatSpinDelegate.h"
#include <QDoubleSpinBox>
//解决QT中中文显示乱码问题
#pragma execution_character_set("utf-8")
QWFloatSpinDelegate::QWFloatSpinDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
QWFloatSpinDelegate::~QWFloatSpinDelegate()
{
}
QWidget *QWFloatSpinDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option, const QModelIndex &index) const
{
Q_UNUSED(option);
Q_UNUSED(index);
QDoubleSpinBox *editor = new QDoubleSpinBox(parent);
editor->setFrame(false);
editor->se