简体   繁体   English

单击按钮时的QListWidgetItem执行操作

[英]QListWidgetItem when button clicked do actions

I have a QListWidget with some items. 我有一个带有一些项目的QListWidget。 I did this to select an item from QListWidget: 我这样做是为了从QListWidget中选择一个项目:

connect(ui->mylist,SIGNAL(itemClicked(QListWidgetItem*)),this,SLOT(onMyListItem(QListWidgetItem*)));

Now in onMyListItem function I did this to check the index of the item: 现在在onMyListItem函数中,我这样做是为了检查项目的索引:

int index=ui->mylist->currentRow();
    qDebug()<<"index item: "<<index;

How can I do some actions when I select an item and click a showBtn button? 选择项目并单击showBtn按钮时,如何执行某些操作?

If you are adding a button to your QListWidget , create an slot and connect your button signal to slot. 如果要向QListWidget添加按钮,请创建一个插槽并将按钮信号连接到插槽。 by clicking the button , you can check witch item is selected and you can do what you want 通过单击按钮,您可以选中女巫项目,您可以做你想要的

I'll assume the button you're adding is a QAction , then you just need to connect its triggered() signal to a slot you create, say makeAction() : 我假设您添加的按钮是QAction ,那么您只需要将其triggered()信号连接到您创建的插槽,比如makeAction()

connect(someAction, SIGNAL(triggered()), this, SLOT(makeAction());

Inside this slot, assuming it has access to the list, you can get the current item in the list and do the appropriate thing: 在此插槽中,假设它可以访问列表,您可以获取列表中的当前项并执行相应的操作:

void makeAction()
{
    auto item = ui->mylist->currentItem();
    if (item)
    {
        // Do something
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM