QListWidget成员函数功能和使用详细说明(二)(文字+用例+代码+效果图)

2.18 QListWidgetItem *item(int row) const

(1)功能说明
获取指定行的对象
(2)效果展示
在这里插入图片描述
(3)调用程序
QtGuiAppTest.ui
在这里插入图片描述
QtGuiAppTest.h

#pragma once
#include <QtWidgets/QWidget>
#include "ui_QtGuiAppTest.h"
class QtGuiAppTest : public QWidget
{
   
    Q_OBJECT
public:
    QtGuiAppTest(QWidget *parent = Q_NULLPTR);
public slots:
    void SlotTestItem();
private:
    Ui::QtGuiAppTestClass ui;
};

添加槽函数SlotTestItem()

QtGuiAppTest.cpp

#include "QtGuiAppTest.h"
QtGuiAppTest::QtGuiAppTest(QWidget *parent)
    : QWidget(parent)
{
   
    ui.setupUi(this);
    //为列表添加数据
    ui.listWidget->addItems(QStringList() <<  QString::fromLocal8Bit("zhansan") <<
                             QString::fromLocal8Bit("lisi") <<
                             QString::fromLocal8Bit("wangwu") <<
                             QString::fromLocal8Bit("mazi"));
    //连接按钮点击信号与SlotTestItem槽函数
    connect(ui.pushButton, &QPushButton::clicked, this,  &QtGuiAppTest::SlotTestItem);
}
void QtGuiAppTest::SlotTestItem()
{
   
    //获取指定行数
    int nRow = ui.lineEdit->text().toInt();
    //获取对应行的对象
    QListWidgetItem* pitm = ui.listWidget->item(nRow);
    //将对象的内容显示到label中
    ui.label->setText(pitm->text());
}

2.19 QListWidgetItem * itemAt(const QPoint &p) const

(1)功能说明
如果行所在区域包含指定的点位置,则返回该行对象
(2)效果展示
在这里插入图片描述
(3)调用程序
QtGuiAppTest.ui
在这里插入图片描述
QtGuiAppTest.h

#pragma once
#include <QtWidgets/QWidget>
#include "ui_QtGuiAppTest.h"
#include<qlistwidget.h>
/*
* @brief 继承QListWidget,用于重写mouseMoveEvent(鼠标移动事件),通过
* 获取鼠标当前位置(在列表中的相对位置),判断鼠标所在的行并在label中显示该行的内容
*/
class MyListWidget : public QListWidget
{
   
public:
    //设置一个label,用于显示指定的行内容
    void setLabel(QLabel* plabel);
protected:
    //获取鼠标当前所在位置,并显示鼠标所在区域的行内容
    void mouseMoveEvent(QMouseEvent* ev) override;
private:
    QLabel* m_pLabel;
};
class QtGuiAppTest : public QWidget
{
   
    Q_OBJECT
public:
    QtGuiAppTest(QWidget *parent = Q_NULLPTR);
private:
    Ui::QtGuiAppTestClass ui;
    MyListWidget m_listWidget;
};

QtGuiAppTest.cpp

#include "QtGuiAppTest.h"
#include<qevent.h>
QtGuiAppTest::QtGuiAppTest(QWidget *parent)
    : QWidget(parent)
{
   
    ui.setupUi(this);
    m_listWidget.setParent(this);
    m_listWidget.move(10, 10);
    //启用鼠标跟踪功能
    m_listWidget.setMouseTracking(true);
    //传入界面的label控件
    m_listWidget.setLabel(ui.label);
    //为列表添加数据
    m_listWidget.addItems(QStringList() <<  QString::fromLocal8Bit("zhansan") <<
                           QString::fromLocal8Bit("lisi") <<
                           QString::fromLocal8Bit("wangwu") <<
                           QString::
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

懒人空想家

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值