Ahmed Abdellatif
9th October 2017, 19:14
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
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
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