Error in changing the colour of the item of a listwidget
I am trying to do some operation of the selected item in a list widget if a pushbutton is clicked. this is done using the following code
Code:
void Dialog::on_pushButton_clicked()
{
if(MyListWidget->currentItem()->isSelected())
{
MyListWidget->currentItem()->setText("YOU");
QString my_text
= MyListWidget
->currentItem
()->text
();
qDebug()<<my_text;
MyListWidget->currentItem()->setBackgroundColor(Qt::green);
}
else return;
}
The program works well if we select an item before pressing the pushbutton,the problem occurs if no item is selected before clicking the button, the programme is crashed
Re: Error in changing the colour of the item of a listwidget
What happens if you just remove the else statement?
Re: Error in changing the colour of the item of a listwidget
Hi, just guessing, but I think MyListWidget->currentItem() will return NULL.
A debugger will help you to determine the position and reason of the crash.
Ginsengelf
Re: Error in changing the colour of the item of a listwidget
Try this:
Code:
void Dialog::on_pushButton_clicked()
{
if(pCurrItem && pCurrItem->isSelected())
{
pCurrItem->setText("YOU");
QString my_text
= pCurrItem
->text
();
qDebug()<<my_text;
pCurrItem->setBackgroundColor(Qt::green);
}
}