0

I have this slot and I want to be able to use the string that is at the index being passed through. How can I get to it?

void Dialog::on_list_Favorites_2_clicked(const QModelIndex &index)
{

}

2 Answers 2

3

Since you're using QListWidget instead of QListView you should also use the signal itemClicked(QListWidgetItem*) instead of clicked(const QModelIndex &).

void Dialog::on_list_Favorites_2_itemClicked(QListWidgetItem* item)
{
    qDebug() << item->text();
}
Sign up to request clarification or add additional context in comments.

3 Comments

When I do that I get errors: C2061: syntax error: identifier 'QListWidgetItem' error: C2511: 'void Dialog::on_listWidget_Fav_1_itemClicked(QListWidgetItem *)' : overloaded member function not found in 'Dialog'
I get the same errors if I just make a new project with a list widget and create the slot "itemclicked" and try to run.
I am idiot. I needed to include QListWidgetItem in dialog.h
1

You can use below function for this case.

QListWidgetItem * QListWidget::itemFromIndex(const QModelIndex & index) const

And then, the text of item can get using by QString QListWidgetItem::text() const

void Dialog::on_list_Favorites_2_clicked(const QModelIndex &index)
{
    QListWidgetItem* pItem = m_listWidget->itemFromIndex( index );
    Q_ASSERT( pItem );
    if ( pItem )
    {
        QString itemName = pItem->text();
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.