教大家一个在自定义的View里面不写block,delegate,Notification快速刷新UI的方法!

//得到此view 所在的viewController

- (UIViewController *)viewController {

    for (UIView* next = [self superview]; next; next =

         next.superview) {

        UIResponder* nextResponder = [next nextResponder];

        if ([nextResponder isKindOfClass:[UIViewController class]]) {

            return (UIViewController*)nextResponder;

        }

    }

    return nil;

}


然后,你懂的微笑

好的,以下是一个使用自定义模型和委托的Qt代码,用于显示收音频道列表的ListView,并且会自动调整每个项目的大小以适应内容: 首先,我们需要定义一个自定义模型类(ChannelListModel),用于包含我们的音频频道数据。该类继承自QAbstractListModel,并实现了必要的方法来处理数据。 ```cpp class ChannelListModel : public QAbstractListModel { Q_OBJECT public: explicit ChannelListModel(QObject *parent = nullptr); int rowCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; void setChannels(const QList<QString> &channels); private: QList<QString> m_channels; }; ``` 然后我们需要在ChannelListModel类中实现setChannels方法,该方法用于设置音频频道数据。每当此方法被调用时,我们都会发出dataChanged信号,以便通知视图进行更新。 ```cpp void ChannelListModel::setChannels(const QList<QString> &channels) { beginResetModel(); m_channels = channels; endResetModel(); } ``` 接下来,我们需要定义一个自定义委托类(ChannelListDelegate),用于呈现每个音频频道项目的外观。该类继承自QStyledItemDelegate,并覆盖了必要的方法来呈现数据。 ```cpp class ChannelListDelegate : public QStyledItemDelegate { Q_OBJECT public: explicit ChannelListDelegate(QObject *parent = nullptr); QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override; void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override; }; ``` 在ChannelListDelegate类中,我们覆盖了sizeHint方法,以便动态调整每个项目的大小以适应内容,并实现了paint方法来呈现数据。 ```cpp QSize ChannelListDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const { Q_UNUSED(option); const QString text = index.data(Qt::DisplayRole).toString(); const QFontMetrics fontMetrics(option.font); const QSize textSize = fontMetrics.size(Qt::TextSingleLine, text); const QSize size = textSize + QSize(10, 10); return size; } void ChannelListDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const { QStyledItemDelegate::paint(painter, option, index); const QString text = index.data(Qt::DisplayRole).toString(); painter->drawText(option.rect, Qt::AlignLeft | Qt::AlignVCenter, text); } ``` 最后,在我们的主窗口类中,我们需要创建一个ListView,并将其模型设置为我们的自定义模型,将其委托设置为我们的自定义委托: ```cpp ChannelListModel *model = new ChannelListModel(this); ChannelListDelegate *delegate = new ChannelListDelegate(this); QListView *listView = new QListView(this); listView->setModel(model); listView->setItemDelegate(delegate); ``` 最后,我们可以通过调用setChannels方法设置ListView的数据: ```cpp QList<QString> channels = {"Channel 1", "Channel 2", "Channel 3"}; model->setChannels(channels); ``` 以上就是一个使用自定义模型和委托的Qt代码,用于显示收音频道列表的ListView,并且会自动调整每个项目的大小以适应内容。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值