MVC model programming based on the QAbstractListModel,setEditorData function did not refresh correctly, results in the displayed data incorrect.”

this is my wlanmode.cpp

#include "wlanmodel.h"
#include <networkinterface.h>
#include <QImage>
#include "union-dbus-network-interface.h"


const int NM_DEVICE_TYPE_WIFI = 2;
const int CONNECT_TYPE_WIRELESS = 14;

WlanModel::WlanModel(QObject *parent)
    : QAbstractListModel(parent)
{
    //m_data = UnionNetworkDBusInterface::Instance()->networkInterface()->wifiList();
    //m_data<<"redflag_5G1"<<"redflag_5G2"<<"redflag_5G3"<<"redflag_5G4"<<"redflag_5G5"<<"redflag_5G6";
    m_data.insert("redflag_5G1",":/strongwlansignal.svg");
    m_data.insert("redflag_5G2",":/strongwlansignal.svg");
    m_data.insert("redflag_5G3",":/strongwlansignal.svg");
    m_data.insert("redflag_5G4",":/strongwlansignal.svg");
    m_data.insert("redflag_5G5",":/strongwlansignal.svg");
    m_data.insert("redflag_5G6",":/strongwlansignal.svg");
    m_data.insert("redflag_5G7",":/strongwlansignal.svg");
    qDebug()<<"--------variantmap"<<m_data;

}

int WlanModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : m_data.size();
}

QVariant WlanModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
        return QVariant();
    if (role == Qt::DisplayRole) {
        return m_data.keys().at(index.row());
    } else if (role == Qt::DecorationRole) {
        return QImage(m_data.values().at(index.row()).toString());
    }
    return QVariant();
}

QString WlanModel::getKey(int index) const
{
    QStringList keys = m_data.keys();
    qDebug()<<"keys ===="<<keys;
    return index >= 0 && index < keys.size() ? keys.at(index) : QString();
}

QVariant WlanModel::getValueByKey(const QString &key) const
{
    return m_data.value(key, QVariant());
}

this is my wlandelegate.cpp

#include "wlandelegate.h"
#include <QPainter>
#include <QImage>
#include <QApplication>
#include <QPushButton>
#include <QBoxLayout>
#include <QDebug>
#include <rf-widget/rlabel.h>
#include <rf-widget/rbackgroundframe.h>
#include <rf-widget/rpushbutton.h>

#define TableControllerSpacing 20

WlanDelegate::WlanDelegate(QListView *listView,QObject *parent)
    : QStyledItemDelegate(parent),
      m_listView(listView)
{
}

void WlanDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    painter->save();
    // 获取项的矩形区域
    QRect rect = option.rect;

    // 如果需要选中效果,设置不同的背景颜色
    if (option.state & QStyle::State_Selected) {
        painter->fillRect(rect, option.palette.color(QPalette::Window).lighter());
        //p.setBrush(opt.palette.color(QPalette::Window).lighter());
    } else {
        painter->fillRect(rect, option.palette.base());
    }

    QStyleOptionViewItem opt = option;
    // 禁用焦点框的绘制
    opt.state &= ~QStyle::State_HasFocus;

    // 调用父类的绘制行为
    QStyledItemDelegate::paint(painter, opt, index);

    // 恢复绘制状态
    painter->restore();
}

QSize WlanDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    return QSize(300, 82);  // 设置每一项的大小
}

QWidget *WlanDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QWidget *editor = new QWidget(parent);
    editor->setLayout(new QHBoxLayout(editor));  // 设置布局,具体的控件将在setEditorData中添加
    return editor;
}

void WlanDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{

    // 清除既有布局中的所有控件
    QLayout *pLayout = editor->layout();
    QLayoutItem *item;
    while ((item = pLayout->takeAt(0)) != nullptr)
    {
        delete item->widget();
        delete item;
    }

    // 创建并添加新的控件
//    QLabel *iconLabel = new QLabel(editor);
//    QLabel *textLabel = new QLabel(editor);
//    QImage wlanImage;
//    wlanImage.load(index.data(Qt::DecorationRole).toString());
//    iconLabel->setPixmap(QPixmap::fromImage(wlanImage));
    QString key = index.data(Qt::DisplayRole).toString();
//    textLabel->setText(key);
    QPushButton *pConnectAndDisconnectButton = new QPushButton(editor);
    pConnectAndDisconnectButton->setText(key);
    QHBoxLayout *pHlayout = new QHBoxLayout(editor);
    QSpacerItem* tabelControllerspacing = new QSpacerItem(TableControllerSpacing, 0, QSizePolicy::Minimum, QSizePolicy::Minimum);
    pHlayout->addSpacerItem(tabelControllerspacing);
    pHlayout->setContentsMargins(100,0,30,0);
//    pHlayout->addWidget(iconLabel);
//    pHlayout->addWidget(textLabel);
    pHlayout->addWidget(pConnectAndDisconnectButton);
    editor->setLayout(pHlayout);
}

void WlanDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
}

void WlanDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{

}

in setEditorData function i init my layout then

this is my using of wlanmode mvc in my mainwindow

 WlanDelegate *delegate = new WlanDelegate(m_pListView, this);
 m_pListView->setItemDelegate(delegate);
 m_pMainLayout->addWidget(m_pTitlebackgroundFrame);
 m_pMainLayout->addWidget(m_pListView);
 setLayout(m_pMainLayout);

then i get a image

enter image description here

then no matter how i change setEditorData function,i always get the picture up,why my setEditorData function doesn’t work later.

i change my code here

void WlanDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{

    editor->setFixedSize(300,82);
    QLayout *pLayout = editor->layout();
    QLayoutItem *item;
    while ((item = pLayout->takeAt(0)) != nullptr)
    {
        delete item->widget();
        delete item;
    }

    // 创建并添加新的控件
    QLabel *iconLabel = new QLabel(editor);
    QLabel *textLabel = new QLabel(editor);
    QImage wlanImage;
    wlanImage.load(index.data(Qt::DecorationRole).toString());
    iconLabel->setPixmap(QPixmap::fromImage(wlanImage));
    QString key = index.data(Qt::DisplayRole).toString();
    textLabel->setText(key);
    QPushButton *pConnectAndDisconnectButton = new QPushButton(editor);
    pConnectAndDisconnectButton->setText(key);
    QHBoxLayout *pHlayout = new QHBoxLayout(editor);
    QSpacerItem* tabelControllerspacing = new QSpacerItem(TableControllerSpacing, 0, QSizePolicy::Minimum, QSizePolicy::Minimum);
    pHlayout->addSpacerItem(tabelControllerspacing);
    pHlayout->setContentsMargins(100,0,30,0);
    pHlayout->addWidget(iconLabel);
    pHlayout->addWidget(textLabel);
    pHlayout->addWidget(pConnectAndDisconnectButton);
    editor->setLayout(pHlayout);
}

and addSpacindg,setContetMargin doesn’t work,i expect that they can work and i hope i can add new controllers.

New contributor

任欣禹 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật