We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

QListWidget editing problems

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;
}
2
Contributors
6
Replies
22 Hours
Discussion Span
1 Year Ago
Last Updated
7
Views
nine9hours
Newbie Poster
14 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

What are the errors?

Also, QListWidgettItem::setBackgroundColor() is deprecated, you should probably use QListWidgettItem::setBackground() instead

ravenous
Practically a Master Poster
681 posts since Jul 2005
Reputation Points: 286
Solved Threads: 111
Skill Endorsements: 9

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++)

nine9hours
Newbie Poster
14 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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.

nine9hours
Newbie Poster
14 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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 :)

ravenous
Practically a Master Poster
681 posts since Jul 2005
Reputation Points: 286
Solved Threads: 111
Skill Endorsements: 9

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?

nine9hours
Newbie Poster
14 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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?

nine9hours
Newbie Poster
14 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0757 seconds using 2.67MB