|
hello,不好意思,刚看到这里还有专门的插入代码工具,之前的那个帖子代码没有用这个工具,太乱了。重复一下我的问题,是这样的:有一个数据源,假设我用vectcor容器来盛放它。数据源中的每一个数据项包括三个类容:一张图片,一个图片名字,一个复选框用于记录用户是否选择这张图片。这三项类容我打算放到一个frame上,分别用一个qlabel, 一个qlabel,一个qcheckbox来呈现。现在,我需要把每一个数据项都呈现到tableview的单元格上,也就是说我想把frame放到单元格中,并且单元格可以编辑。比如说,我点击某个单元格,然后单元格的图片就被选中了,复选框自动勾上勾。我的问题是:当tableview第一次呈现出来的时候,我需要它能够加载数据源中的数据到每个单元格中,那么我该怎么做?是重写model的data()函数来实现这个功能,还是重写delegate的setEditData()函数?如果热心的你不嫌麻烦的话,我贴出了我的代码,大致展示一下我的思路(我的代码还是有问题的,只能显示一列单元格,而且frame不显示):这是自定义frame的头文件:- class thumbnailFrame : public QFrame
- {
- public:
- explicit thumbnailFrame(DataSource *datasource, QWidget *parent = 0);
- ~thumbnailFrame();
- void setThumbnailSize();
- QSize getThumbnailSize();
- void setDataSource(DataSource *dataSource);
- DataSource* getDataSource();
- void setCheckBox(bool save);
- QCheckBox * getCheckBox();
- void setImgShowLabel(QPixmap pixmap);
- QLabel *getImgShowLabel();
- void setNameLable(QString name);
- QLabel *getNameLabel();
- private:
- Ui::thumbnailFrame *ui;
- QSize thumbnailSize; //等于数据源中的iconsize
- DataSource *dataSource; //数据源规格
- };
这是自定义frame实现: - thumbnailFrame::thumbnailFrame(DataSource *datasource, QWidget *parent) :
- QFrame(parent),
- ui(new Ui::thumbnailFrame)
- {
- ui->setupUi(this);
- ui->imgNameLabel->setAlignment(Qt::AlignCenter); //图片名居
- setDataSource(datasource);
- }
- thumbnailFrame::~thumbnailFrame(){
- delete ui;
- delete dataSource;
- }
- void thumbnailFrame::setThumbnailSize(){
- int icon_width = this->dataSource->getIconSize().width();
- int icon_height = this->dataSource->getIconSize().height();
- ui->imgShowLabel->resize(icon_width, icon_height);
- int name_width = this->dataSource->getNameLabelSize().width();
- int name_height = this->dataSource->getNameLabelSize().height();
- ui->imgNameLabel->resize(name_width, name_height);
- int space_width = this->dataSource->getSpace().width();
- int space_height = this->dataSource->getSpace().height();
- int frame_width = icon_width + space_width;
- int frame_height = icon_height + name_height + space_height;
- this->thumbnailSize = QSize(frame_width, frame_height);
- }
- QSize thumbnailFrame::getThumbnailSize(){
- return thumbnailSize;
- }
- void thumbnailFrame::setDataSource(DataSource* dataSource){
- this->dataSource = dataSource;
- }
- DataSource* thumbnailFrame::getDataSource(){
- return this->dataSource;
- }
- void thumbnailFrame::setCheckBox(bool save){
- Qt::CheckState state;
- if(save == true){
- state = Qt::Checked;
- }else{
- state = Qt::Unchecked;
- }
- this->ui->checkBox->setCheckState(state);
- }
- QCheckBox *thumbnailFrame::getCheckBox(){
- return this->ui->checkBox;
- }
- void thumbnailFrame::setNameLable(QString name){
- this->ui->imgNameLabel->setText(name);
- }
- QLabel * thumbnailFrame::getNameLabel(){
- return this->ui->imgNameLabel;
- }
- void thumbnailFrame::setImgShowLabel(QPixmap pixmap){
- this->ui->imgShowLabel->setPixmap(pixmap);
- }
这是数据源头文件: - class DataSource : public QObject
- {
- Q_OBJECT
- public:
- DataSource(QObject *parent = 0);
- ~ DataSource();
- void setName(QString name);
- QString getName();
- void setPixMap(QPixmap *pixMap);
- QPixmap *getPixMap();
- void setIconSize(QSize size);
- QSize getIconSize();
- void setNameLabelSize(QSize size);
- QSize getNameLabelSize();
- void setCheckboxSize(QSize size);
- QSize getCheckboxSize();
- void setSpace(QSize size);
- QSize getSpace();
- void setSave(bool save);
- bool getSave();
- private:
- QString name;
- QPixmap *pixMap;
- bool save;
- QSize iconSize; //缩略图尺寸
- QSize nameLabelSize; //展现文件名的lable尺寸,宽度最好和缩略图宽度相等
- QSize checkboxSize; //复选框尺寸
- QSize space; //间距
- };
这是数据源实现: - DataSource::DataSource(QObject *parent):QObject(parent)
- {
- this->name = "";
- this->pixMap = NULL;
- this->save = false;
- }
- DataSource::~DataSource(){
- delete pixMap;
- }
- void DataSource::setName(QString name){
- this->name = name;
- }
- QString DataSource::getName(){
- return this->name;
- }
- void DataSource::setPixMap(QPixmap *pixMap){
- this->pixMap = pixMap;
- }
- QPixmap *DataSource::getPixMap(){
- return this->pixMap;
- }
- void DataSource::setIconSize(QSize size){
- this->iconSize = size;
- }
- QSize DataSource::getIconSize(){
- return this->iconSize;
- }
- void DataSource::setNameLabelSize(QSize size){
- this->nameLabelSize = size;
- }
- QSize DataSource::getNameLabelSize(){
- return this->nameLabelSize;
- }
- void DataSource::setCheckboxSize(QSize size){
- this->checkboxSize = size;
- }
- QSize DataSource::getCheckboxSize(){
- return this->checkboxSize;
- }
- void DataSource::setSpace(QSize size){
- this->space = size;
- }
- QSize DataSource::getSpace(){
- return this->space;
- }
- void DataSource::setSave(bool save){
- this->save = save;
- }
- bool DataSource::getSave(){
- return this->save;
- }
这是我实现的自定义delegate,继承自qitemdelegate - class ThumbnailDelegate: public QItemDelegate
- {
- Q_OBJECT
- public:
- explicit ThumbnailDelegate(DataSource *dataSource, QObject *parent = 0);
- ~ThumbnailDelegate();
- void setDataSource(DataSource *datasource);
- DataSource* getDataSource();
- void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index)const;
- // bool editorEvent(QEvent *event,
- // QAbstractItemModel *model,
- // const QStyleOptionViewItem &option,
- // const QModelIndex &index);
- //返回一个编辑控件,用来编辑指定项的数据
- virtual QWidget *createEditor(QWidget *parent,
- const QStyleOptionViewItem &option,
- const QModelIndex &index)const;
- //将Model中数据赋值到控件上
- virtual void setEditorData(QWidget *editor, const QModelIndex &index) const;
- //设定模型数据,根据指定项中对应编辑控件的数据
- virtual void setModelData(QWidget *editor, QAbstractItemModel *model,
- const QModelIndex &index) const;
- //更新编辑框几何形状
- virtual void updateEditorGeometry(QWidget *editor,
- const QStyleOptionViewItem &option, const QModelIndex &index) const;
- private:
- QPixmap pixMap;
- thumbnailFrame *frame;
- DataSource *datasource; //数据源样例
- };
- ThumbnailDelegate::ThumbnailDelegate(DataSource *dataSource, QObject *parent):
- QItemDelegate(parent){
- setDataSource(dataSource);
- }
- ThumbnailDelegate::~ThumbnailDelegate(){
- delete datasource;
- }
- void ThumbnailDelegate::setDataSource(DataSource *datasource){
- this->datasource = datasource;
- }
- DataSource* ThumbnailDelegate::getDataSource(){
- return this->datasource;
- }
- QWidget * ThumbnailDelegate::createEditor(QWidget *parent,
- const QStyleOptionViewItem &option,
- const QModelIndex &index)const{
- thumbnailFrame *frame = new thumbnailFrame(this->datasource, parent);
- frame->setThumbnailSize();
- return frame;
- }
- //将Model中数据赋值到控件上
- void ThumbnailDelegate::setEditorData(QWidget *editor,
- const QModelIndex &index) const{
- //返回该索引的模型,继而返回该模型中此索引的编辑角色数据
- QVariant var = index.model()->data(index);
- DataSource *source = var.value<DataSource*>();
- QString name = source->getName();
- QPixmap *pixMap = source->getPixMap();
- bool save = source->getSave();
- //给控件赋值
- thumbnailFrame *frame = static_cast<thumbnailFrame*>(editor);
- frame->setDataSource(this->datasource);
- frame->setThumbnailSize(); //设置frame上的各个控价大小
- frame->setCheckBox(save);
- frame->setImgShowLabel(*pixMap);
- frame->setNameLable(name);
- }
- void ThumbnailDelegate::setModelData(QWidget *editor,
- QAbstractItemModel *model,
- const QModelIndex &index) const
- {
- thumbnailFrame *frame = static_cast<thumbnailFrame*>(editor);
- bool save;
- if(frame->getCheckBox()->checkState() == Qt::Checked){
- save = true;
- }else{
- save = false;
- }
- QString name = frame->getDataSource()->getName();
- QPixmap *pixMap = frame->getDataSource()->getPixMap();
- QSize iconSize = frame->getDataSource()->getIconSize();
- QSize nameLabelSize = frame->getDataSource()->getNameLabelSize();
- QSize checkboxSize = frame->getDataSource()->getCheckboxSize();
- QSize space = frame->getDataSource()->getSpace();
- DataSource *data = new DataSource();
- data->setPixMap(pixMap);
- data->setName(name);
- data->setIconSize(iconSize);
- data->setCheckboxSize(checkboxSize);
- data->setSpace(space);
- data->setNameLabelSize(nameLabelSize);
- data->setSave(save);
- //设置模型的数据
- QVariant var;
- var.setValue(data);
- model->setData(index, var);
- }
- void ThumbnailDelegate::updateEditorGeometry(QWidget *editor,
- const QStyleOptionViewItem &option,
- const QModelIndex &index) const{
- editor->setGeometry(option.rect);
- }
- void ThumbnailDelegate::paint(QPainter *painter,
- const QStyleOptionViewItem &option,
- const QModelIndex &index) const{
- const QAbstractItemModel *model = index.model();
- QVariant var = model->data(index);
- if(var.isNull()){
- var = false;
- return;
- }
- //画图
- QPixmap *pixMap = var.value<DataSource*>()->getPixMap();
- QSize icon_size = var.value<DataSource*>()->getIconSize(); //图片尺寸
- QSize space_size = var.value<DataSource*>()->getSpace();
- QSize checkbox_size = var.value<DataSource*>()->getCheckboxSize();
- QRect rect = option.rect;
- int x = rect.x() + space_size.width() / 2;
- int y = rect.y() + space_size.height() / 2;
- painter->drawPixmap(x, y, pixMap->scaled((var.value<DataSource*>()->getIconSize())));
- //画图片名称
- QString name = var.value<DataSource*>()->getName();
- int x2 = x;
- int y2 = y + icon_size.height();
- painter->drawText(x2, y2, name);
- painter->end();
- //画复选框
- bool checked = var.value<DataSource*>()->getSave();
- //按钮风格选项
- QStyleOptionButton *checkBoxOption = new QStyleOptionButton();
- checkBoxOption->state |= QStyle::State_Enabled;
- if(checked){
- checkBoxOption->state |= QStyle::State_On;
- }else{
- checkBoxOption->state |= QStyle::State_Off;
- }
- int x3 = x + icon_size.width() - checkbox_size.width();
- int y3 = y;
- QRect rect2(x3, y3, checkbox_size.width(), checkbox_size.height());
- checkBoxOption->rect = rect2;
- QApplication::style()->drawControl(QStyle::CE_CheckBox,checkBoxOption,painter);
- }
这是我自定义的model,继承自tablemodel: - class ThumbnailModel: public QAbstractTableModel
- {
- public:
- ThumbnailModel(DataSource *dataSource, int tableView_h, int tableView_w, QObject *parent = 0);
- ~ThumbnailModel();
- void setVectorDataSource(QVector<DataSource*> vectorDataSource);
- int rowCount(const QModelIndex &parent) const;
- int columnCount(const QModelIndex &parent) const;
- QVariant data(const QModelIndex &index, int role) const;
- QVariant headerData(int section, Qt::Orientation orientation, int role) const;
- void setTableView_width(int width);
- int getTableView_width();
- void setTableView_height(int height);
- int getTableView_height();
- void setDataSource(DataSource *dataSource);
- DataSource *getDataSource();
- private:
- QVector<DataSource*> vectorDataSource; //底层数据
- int tableView_width;
- int tableView_heigth;
- DataSource *dataSource; //底层数据单个范例
- };
- ThumbnailModel::ThumbnailModel(DataSource *dataSource, int tableview_h, int tableview_w, QObject *parent):
- QAbstractTableModel(parent)
- {
- setDataSource(dataSource);
- setTableView_height(tableview_h);
- setTableView_width(tableview_w);
- }
- ThumbnailModel::~ThumbnailModel(){
- delete dataSource;
- }
- void ThumbnailModel::setVectorDataSource(QVector<DataSource*> vectorDataSource){
- this->vectorDataSource = vectorDataSource;
- }
- void ThumbnailModel::setTableView_width(int width){
- this->tableView_width = width;
- }
- int ThumbnailModel::getTableView_width(){
- return this->tableView_width;
- }
- void ThumbnailModel::setTableView_height(int height){
- this->tableView_heigth = height;
- }
- int ThumbnailModel::getTableView_height(){
- return this->tableView_heigth;
- }
- void ThumbnailModel::setDataSource(DataSource *datasource){
- this->dataSource = datasource;
- }
- DataSource* ThumbnailModel::getDataSource(){
- return this->dataSource;
- }
- int ThumbnailModel::rowCount(const QModelIndex &/*parent*/) const{
- int frame_height = this->dataSource->getIconSize().height() +
- this->dataSource->getNameLabelSize().height() +
- this->dataSource->getSpace().height();
- int rowCount = this->tableView_heigth / frame_height;
- return rowCount;
- }
- int ThumbnailModel::columnCount(const QModelIndex &/*parent*/) const{
- int space_width = this->dataSource->getSpace().width();
- int other_width = this->dataSource->getIconSize().width() > this->dataSource->getNameLabelSize().width()?
- this->dataSource->getIconSize().width():
- this->dataSource->getNameLabelSize().width();
- int frame_width = space_width + other_width;
- int columnCount = this->tableView_width / frame_width;
- return columnCount;
- }
- QVariant ThumbnailModel::data(const QModelIndex &index, int role) const{
- if(!index.isValid()){
- return QVariant();
- }
- int row = index.row();
- int col = index.column();
- int index_ = row * this->columnCount(QModelIndex()) + col - 1;
- QVariant var;
- var.setValue((void *)this->vectorDataSource.at(index_));
- return var;
- }
- QVariant ThumbnailModel::headerData(int section, Qt::Orientation /* orientation */, int role) const{
- if(role != Qt::DisplayRole)
- return QVariant();
- return section;
- }
|