Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QListWidget do not set currentItem

QListWidget do not set currentItem

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SergeyK12
    wrote on last edited by
    #1

    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 working

    thanks

    S 1 Reply Last reply
    0
    • S SergeyK12

      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 working

      thanks

      S Offline
      S Offline
      SergeyK12
      wrote on last edited by
      #2

      @SergeyK12
      I forgot to write about error:
      it is index out of range

      but m_list->count() return 1;

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi,

        Sounds a bit strange, but since you are using QListWidget, why not use setCurrentItem with the one you just created ?

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SergeyK12
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Did you check that item isn't null ?

            In any case, can you just share the complete code where you are having trouble ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply
            0
            • S Offline
              S Offline
              SergeyK12
              wrote on last edited by
              #6

              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());
                  });
              
              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Can you show a stack trace of your crash ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SergeyK12
                  wrote on last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Did you check that item isn't null ?

                    In any case, can you just share the complete code where you are having trouble ?

                    S Offline
                    S Offline
                    StephenQin
                    wrote on last edited by
                    #9

                    @SGaist You've said it, I meet this same problem. The reason is that item is null.

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved