PDA

View Full Version : Stupid list widget won't clear



scwizard
24th March 2007, 20:58
When I say MyListWidget->clear() it crashes if there's an item selected (if it's fresh and an item hasn't been clicked yet it works).

When I create a command that goes MyListWidget->takeItem(0) it removes the top item (like it should) and then when there's one item left it crashes when it tries to remove it.

If I don't select anything in the listwidget at all during the course of the program, the command is able to remove the last item safely.

What's going on here? I think clear() worked in my program a short time ago, and now it seems to have this strange issue. How do I fix this issue and make the listwidget clearable?

wysota
24th March 2007, 21:01
Are you by any chance using a Qt version lower than 4.2?

scwizard
24th March 2007, 21:04
Nope, I'm not. Going into the all programs menu I see Qt 4.2.2 (open source edition) by Trolltech.

Kumosan
24th March 2007, 21:14
Could you give us some code? Could it be that you delete your item in the QListWidget somewhere else illegally?

jacek
24th March 2007, 21:16
Could you post the backtrace?

wysota
24th March 2007, 22:15
If the thing happens when you delete the last item only, then I was experiencing a simmilar issue some time ago. Do you try to delete that item from within a slot? If so, what is the slot connected to?

scwizard
24th March 2007, 22:37
If the thing happens when you delete the last item only, then I was experiencing a simmilar issue some time ago. Do you try to delete that item from within a slot? If so, what is the slot connected to?

I seems we're getting somewhere now.

The slot is connected to MainWindow. The signal is emitted from a keyboard shortcut.

The hierarchy goes:
MainWindow->Sidebar->MyListWidget

The function is:

void MainWindow::NewFile() {
Sidebar->MyListWidget->clear();
// Or a while loop with the following:
// Sidebar->MyListWidget->takeItem(0);
return;
}

wysota
24th March 2007, 22:39
Does the application still crash if you implement it as follows:

void MainWindow::NewFile(){
QTimer::singleShot(0, Sidebar->MyListWidget, SLOT(clear()));
}

scwizard
24th March 2007, 22:49
Yes it does :(

It doesn't change anything. Still crashes when something has been selected and works when nothing has.

wysota
24th March 2007, 22:55
Then it means it's not because of clear() but because of some other signal from the list widget being connected. It probably calls a slot that takes a QListWidgetItem pointer and it doesn't check for a null value and that causes a crash.

In my situation (Qt bug, AFAIR) the trick above helped. If it didn't help you, the app has to be crashing because of a null pointer in one of the slots. You can try debugging as Jacek suggested, but first I'd check the signals connected.

scwizard
24th March 2007, 23:08
Very good work!

I have a signal being emitted upon items changing and forgot to take into account that the item could change without the user changing it.

rajeshs
4th July 2007, 04:46
I faced Same problem, actual problem in currentRowChanged() signal's slot

I have taken one slot for currentRowChanged() signal of listwidget there i am trying to access element like following code, if we clear the list that time also that slot will be get called,so memory crash occurs,

My old code,

connect(myistWidget,SIGNAL(currentRowChanged(int)) ,this,SLOT(changeLabel(int)));

void QZugNummerDlgAction::changeLabel(int position)
{
QString labelText = myistWidget.at(position);//if we clear list memory crash occurs heremyLabel->setText(labelText);
}

New Code, i put the following condition it works properly,

void QZugNummerDlgAction::changeLabel(int position)
{
if(myistWidget->currentRow()!=-1)
{QString labelText = myistWidget.at(position);
myLabel->setText(labelText);
}
}


Thanks,
Rajesh.S

jacek
4th July 2007, 16:25
QString labelText = myistWidget.at(position);//if we clear list memory crash occurs here
This shouldn't even compile, as "myistWidget" is a pointer.

What is that "at" method? QListWidget doesn't have such method.

rajeshs
5th July 2007, 04:09
Sorry ,one mistake in my code

That is not listwidget, there i am taking one corresponding
Qlist so if we access -1 position from QList crash occurs,

that line is myqlist->at(position).

Thanks,
Rajesh.S

jacek
5th July 2007, 16:03
That is not listwidget, there i am taking one corresponding
Qlist so if we access -1 position from QList crash occurs,
OK, that explains everything. Qt docs warn about accessing inexistent items.