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. [SOLVED] Qt4 - QTableWidget selectedItems QProgressBar
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Qt4 - QTableWidget selectedItems QProgressBar

Scheduled Pinned Locked Moved General and Desktop
8 Posts 2 Posters 3.2k 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.
  • E Offline
    E Offline
    Elfayer
    wrote on last edited by
    #1

    Hello,
    I have a QTableWidget with 6 columns. In the last column, the 6th (index 5, so), I have a QProgressBar.

    I'm trying to change the value of the progress bar with :

    @reinterpret_cast<QProgressBar*>(_table->selectedItems().at(5))->setValue(0);@

    But it throw me an error :
    "ASSERT failure in QList<T>::at: "index out of range", file c:\QtSDK\Desktop\Qt\4.8.1\msvc2010\include\QtCore/qlist.h, line 469"

    Because in fact :

    @std::cout << _table->selectedItems().length() << std::endl;@

    returns me 5.

    How is this possible ? I have a missing element here, that's why I have that error, but why ?

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

      Hi,

      This a traditional counting vs programatic counting problem.

      A size of 5 in standard counting means 1 to 5. But when you are programming, it means counting from 0 to 4.

      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
      • E Offline
        E Offline
        Elfayer
        wrote on last edited by
        #3

        Hi SGaist,
        thanks for replying.

        I have personally never seen a "counting" in programming from 0. When you do an strlen (C language), it counts from 1, not from 0. It begins from 0 when we use indexes.

        But anyway, you're saying the lengh in correct, so why does

        @reinterpret_cast<QProgressBar*>(_table->selectedItems().at(5))->setValue(0);@

        throw me an error ?

        Because this :

        @_table->selectedItems().at(4)@

        is giving me the good item, the one just before the progress bar.

        I think it has something to do with the fact that I had to "setCellWidget" to add the progress bar to the table. But my table is parametered like follow :

        @_table->setSelectionBehavior(QAbstractItemView::SelectRows);
        _table->setSelectionMode(QAbstractItemView::SingleSelection);@

        So it does select the progress the progress bar anyway. And I can see it selected.

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

          In a string the first char is at position 0

          I agree with you about the fact that setting a widget on a cell might change the cell selection reaction. Whether this is the right behavior, I can't tell.

          Check the size of_table->selectedItems(), It's probably 5.

          You can always use the selected row number combined with the known column to retrieve the progress bar

          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
          • E Offline
            E Offline
            Elfayer
            wrote on last edited by
            #5

            I've found the problem. The problem is that I'm using "selectedItems" which returns me correctly the "items" ! But QProgressBar here is a QWidget. And my cast is wrong.

            I'm casting a QTableWidgetItem* instead of a QWidget* to a QProgressBar*. So as you said, I have to use :

            @reinterpret_cast<QProgressBar*>(_table->cellWidget(_table->selectedItems().at(0)->row(), 5))->setValue(0);@

            to get the widget and not the item.

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

              Great you have it working !

              But still, your error was about asking for an out of bound value, which has nothing to do with the faulty cast. So I am still wondering about the size of your selectItems()

              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
              • E Offline
                E Offline
                Elfayer
                wrote on last edited by
                #7

                I guess this is because the progress bar is not an item, but a Widget. And with selectedItems() I probably only get the items. That's why there were one element missing in the length().

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

                  I also think so, but I wonder if this is the correct/intended behavior

                  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

                  • Login

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