检测 QlistWidget 中某行是否单击了某个项目
我被赋予了这个简单的任务,
我有这个列表,每当单击 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如下:
Something as below :
QListWidgetItem 将其文本存储为 QString,因此如果您想操作它,可能需要将其转换为其他内容。 QListWidgetItem 本身不包含有关其位置的信息,但 QListWidget 包含。
如果您查看信号下的 QListWidget 文档,您会发现有几种不同的状态可以在其中执行函数。我个人使用 currentItemChanged。
http://qt-project.org/doc/qt-4.8/QListWidget .html#signals
更新您的构造函数以包括将 listWidget 连接到 myFunc:
并将此函数添加到您的类中:
这应该可以让您获得 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:
And add this function to your class:
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.
您需要将 itemClicked(QListWidgetItem * item) 信号连接到某个插槽来处理项目上的点击。
You need to connect the itemClicked(QListWidgetItem * item) signal to some slot to handle clicks on an item.