Hi. I have made a simple testing program which I'll use in another program later. I create the items of the QListWidget through looping and now I want to make changes in one of the items of the list, which the user clicks. But when I call a fucntion to edit the item, i recieve errors. How can i edit an item of the QListWidget?

Here is the code:

#include "itemdialog.h"
#include "ui_itemdialog.h"
#include <QDebug>
#include <QListWidget>
#include <QListWidgetItem>

ItemDialog::ItemDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ItemDialog)
{
    ui->setupUi(this);
    item=new QListWidget(this);


    for(int i=0;i<10;i++)
    {
        QListWidgetItem *itm= new QListWidgetItem;
    itm->setText(QString::number(i));

    item->addItem(itm);
    connect(item,SIGNAL(clicked(QModelIndex)),this,SLOT(dis(QModelIndex)));
    }
}
void ItemDialog::dis(QModelIndex a)
{
    item->itemFromIndex(&a)->setBackgroundColor(Qt::red); // [B]HERE IS THE ERROR[/B]
}

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

Recommended Answers

All 6 Replies

OK. If i want to change the text of that QListWidget and other stuff while moving through the list like we do in arrays, what do i do? And if i want to edit 5 element of a QListWidget how can i move through the list? any easy way to move through the list like we do in arrays or linkedlists(of simple C++)

The error is ;
error: no matching function for call to 'QListWidget::itemFromIndex(QModelIndex*)'

candidates are: QListWidgetItem* QListWidget::itemFromIndex(const QModelIndex&) const


I think the second error says the functions is constant so i can't modify it. But i have to modify it. any way to do it.

I think the second error says the functions is constant so i can't modify it. But i have to modify it. any way to do it.

No, the problem is that you're doing item->itemFromIndex(&a) when you should do item->itemFromIndex(a) .

Actually, I think that you will get another error when you fix this, since QListWidget::itemFromIndex is a protected function. If you want to want to get all the items in a QListWidget you can just do:

QList< QListWidgetItem* > items = listWidget->items();

and then iterate over the list like any other, doing things to the items as you go. If you want to get the currently selected item, then you can just do:

QListWidgetItem* pCurrentItem = listWidget->currentItem();

Hope that helps :)

Basically I am making a software in which user enter the name of the product provides a little description about that and then other users comment on that product. Similarly one can add comments and products in the program.

How can copy the the text from the QListWidget item and Label and save that into a file?
How can i provide a little close button (as it is on the label in Qt file explorer)?
What can I do so that Every time the program starts, it loads the pics of the products with it?

I have made a little program and I want to add a pushbutton in the QListWidget item. Now the problem is:
1)I cant set the parent of this close button to be that specific Item in the List, so that when the user presses it, only this item is removed from the list.
2) How can I delete the item on which the button is pressed?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.