检测 QlistWidget 中某行是否单击了某个项目

发布于 2024-11-28 23:31:38 字数 914 浏览 2 评论 0原文

我被赋予了这个简单的任务,

我有这个列表,每当单击 ok 时我都会插入项目,void Form::ok() 处理该事件应该将新的列表项目添加到列表中。

现在我无法做的是检测是否在某行单击了某个项目,然后据此执行某些操作,这是我的代码。.

#include "form1.h"
#include "form.h"
#include "ui_form.h"
#include "ui_form1.h"
#include<QScrollArea>
#include<QScrollBar>

//#include <QgeoPositioninfo.h>

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);

}
Form::~Form()
{
    delete ui;
}

void Form::ok()
{
    QIcon  mypix  (":/karim/test.png");

    QListWidgetItem* newItem = new QListWidgetItem;
    newItem->setText("pixmix");
    newItem->setIcon(mypix);

    int row = ui->listWidget->row(ui->listWidget->currentItem());
    this->ui->listWidget->insertItem(row, newItem);

    //if(item at row x is clicked)
     {
     //do something
     }
}

请在您的回答中具体说明,我将不胜感激

I Have been given this simple task ,

I have this list where i instert items whenever ok is clicked,void Form::ok() handle that event is supposed to add new list items to the list.

Now what am not able to do is to detect the if an item is clicked at at some row then do something according to that, this is my code..

#include "form1.h"
#include "form.h"
#include "ui_form.h"
#include "ui_form1.h"
#include<QScrollArea>
#include<QScrollBar>

//#include <QgeoPositioninfo.h>

Form::Form(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Form)
{
    ui->setupUi(this);

}
Form::~Form()
{
    delete ui;
}

void Form::ok()
{
    QIcon  mypix  (":/karim/test.png");

    QListWidgetItem* newItem = new QListWidgetItem;
    newItem->setText("pixmix");
    newItem->setIcon(mypix);

    int row = ui->listWidget->row(ui->listWidget->currentItem());
    this->ui->listWidget->insertItem(row, newItem);

    //if(item at row x is clicked)
     {
     //do something
     }
}

Please be specific in yout answer i will appreciate that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

避讳 2024-12-05 23:31:38

如下:

connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(itemClickedSlot(QListWidgetItem *)));

void Form::itemClickedSlot (QListWidgetItem * itemClicked)
{
//Do something with clicked item
}

Something as below :

connect(ui->listWidget, SIGNAL(itemClicked(QListWidgetItem *)), this, SLOT(itemClickedSlot(QListWidgetItem *)));

void Form::itemClickedSlot (QListWidgetItem * itemClicked)
{
//Do something with clicked item
}
牛↙奶布丁 2024-12-05 23:31:38

QListWidgetItem 将其文本存储为 QString,因此如果您想操作它,可能需要将其转换为其他内容。 QListWidgetItem 本身不包含有关其位置的信息,但 QListWidget 包含。

如果您查看信号下的 QListWidget 文档,您会发现有几种不同的状态可以在其中执行函数。我个人使用 currentItemChanged。

http://qt-project.org/doc/qt-4.8/QListWidget .html#signals

更新您的构造函数以包括将 listWidget 连接到 myFunc:

Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form) {
ui->setupUi(this);
connect(ui->listWidget, 
    SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this,
    SLOT(myFunc(QListWidgetItem *)));
}

并将此函数添加到您的类中:

void Form::myFunc(QListWidget *item) {
    int currentRow = ui->listWidget->currentRow();
    std::cout << (item->text()).toStdString() << std::endl;
}

这应该可以让您获得 QListWidgetItem 在列表中的当前位置及其文本。使用项目->然后您可以更改它的文本并更改其他一些内容:

http://qt-project .org/doc/qt-4.8/qlistwidgetitem.html

快乐编码。

The QListWidgetItem stores its text as a QString so you may need to cast it to something else if you want to manipulate it. The QListWidgetItem itself holds no information about it's position, but QListWidget does.

If you look at the documentation for QListWidget under signals you can see that there are a couple different states that you can execute a function during. I personally use currentItemChanged.

http://qt-project.org/doc/qt-4.8/QListWidget.html#signals

Update your constructor to include connecting your listWidget to myFunc:

Form::Form(QWidget *parent) : QWidget(parent), ui(new Ui::Form) {
ui->setupUi(this);
connect(ui->listWidget, 
    SIGNAL(currentItemChanged(QListWidgetItem*, QListWidgetItem*)), this,
    SLOT(myFunc(QListWidgetItem *)));
}

And add this function to your class:

void Form::myFunc(QListWidget *item) {
    int currentRow = ui->listWidget->currentRow();
    std::cout << (item->text()).toStdString() << std::endl;
}

That should get you the current position of the QListWidgetItem in the list and its text. Using item-> you can then change it's text and change some other things:

http://qt-project.org/doc/qt-4.8/qlistwidgetitem.html

Happy coding.

雪落纷纷 2024-12-05 23:31:38

您需要将 itemClicked(QListWidgetItem * item) 信号连接到某个插槽来处理项目上的点击。

You need to connect the itemClicked(QListWidgetItem * item) signal to some slot to handle clicks on an item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文