Solved QListWidget do not set currentItem
-
Hi!
Could you help me with a little problem?
I'm trying to set current item to QListWidget in order to avoid selection by mouse (which work fine).I find out different ways:
setCurrentRow or get an item and setCurrentItem, but my programm fall down on such a code. Which way should be used then?QListWidget *m_list;
m_list = new QListWidget;when i create new item:
m_list->insert(m_list->count(), m_name->text());
// here should be a code for setting current item
m_list->setCurrentRow(m_list->count()-1); - isnt workingthanks
-
@SergeyK12
I forgot to write about error:
it is index out of rangebut m_list->count() return 1;
-
Hi,
Sounds a bit strange, but since you are using QListWidget, why not use setCurrentItem with the one you just created ?
-
But there is no method which could return the last element.
I'm trying do the following way
QListWidgetItem *item = m_list->item(0);
m_list->setCurrentItem(item);and nothing changed
-
Did you check that item isn't null ?
In any case, can you just share the complete code where you are having trouble ?
-
I check for null and everything ok with it.
I could share code via bitbucket, but it nedd additional dll. I will try to simplify it then.
Here is a part with problem.
Everything work ok without this string
m_list->setCurrentItem(item);
(i mean ok, but i shoul select an item manually)connect(m_compile, &QPushButton::clicked, [this](){ m_list->addItem(m_name->text()); qDebug() <<m_list->count(); QListWidgetItem *item = m_list->item(0); if(item != nullptr) m_list->setCurrentItem(item); setFunctionCode (m_name->text(), m_editor->toPlainText()); m_func.push_back(m_editor->toPlainText()); });
-
Can you show a stack trace of your crash ?
-
That sounds strange for me, but when i erase all not nessesery parts for example QListWidget began to work correctly.
I suppose i should watch my code once more because it crash only with m_list->setCurrentItem(item);Wow, thanks a lot for your guide and help. I figure out my problem.
I also has a connection with itemSelectionChanged which emmited when some item is set. And when i commit this part the problem vanished. -
@SGaist You've said it, I meet this same problem. The reason is that item is null.