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.
任欣禹 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.