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. QTableWidget segmentation fault with setTextAlignment
Qt 6.11 is out! See what's new in the release blog

QTableWidget segmentation fault with setTextAlignment

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.0k Views
  • 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.
  • P Offline
    P Offline
    Publicnamer
    wrote on last edited by
    #1

    I really hope someone is fixing QTableWidget bugs, because this simply shouldn't happen IMHO:

                QModelIndex index = table->model()->index(row, 0);
                table->model()->setData (index, "?", Qt::DisplayRole);
                QTableWidgetItem *item = table->item (row, 0);
                item->setTextAlignment (Qt::AlignRight);
                                 ^-- crash here
    

    The table has 100 rows and 100 columns.

    JonBJ 1 Reply Last reply
    0
    • JoeCFDJ Offline
      JoeCFDJ Offline
      JoeCFD
      wrote on last edited by
      #2

      do you check if item is nullptr?

      1 Reply Last reply
      2
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        @Publicnamer said in QTableWidget segmentation fault with setTextAlignment:

        ^-- crash here

        It should not. What version of Qt are you using? What does table->model()->setData return?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        2
        • P Publicnamer

          I really hope someone is fixing QTableWidget bugs, because this simply shouldn't happen IMHO:

                      QModelIndex index = table->model()->index(row, 0);
                      table->model()->setData (index, "?", Qt::DisplayRole);
                      QTableWidgetItem *item = table->item (row, 0);
                      item->setTextAlignment (Qt::AlignRight);
                                       ^-- crash here
          

          The table has 100 rows and 100 columns.

          JonBJ Online
          JonBJ Online
          JonB
          wrote on last edited by JonB
          #4

          @Publicnamer
          As @VRonin has asked you.

          I would assume QModelIndex index = table->model()->index(row, 0); returns an invalid index if out of range, but QTableWidgetItem *item = table->item (row, 0); would have to return a nullptr....

          Alternatively, you may have a valid index(row, 0). But that does not mean there is a QTableWidgetItem *item at table->item (row, 0), cells start out with no QTableWidgetItem in them, you have to put one in, IIRC.

          Which would explain behaviour you see without there being any " fixing QTableWidget bugs" ?

          VRoninV 1 Reply Last reply
          0
          • JonBJ JonB

            @Publicnamer
            As @VRonin has asked you.

            I would assume QModelIndex index = table->model()->index(row, 0); returns an invalid index if out of range, but QTableWidgetItem *item = table->item (row, 0); would have to return a nullptr....

            Alternatively, you may have a valid index(row, 0). But that does not mean there is a QTableWidgetItem *item at table->item (row, 0), cells start out with no QTableWidgetItem in them, you have to put one in, IIRC.

            Which would explain behaviour you see without there being any " fixing QTableWidget bugs" ?

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @JonB said in QTableWidget segmentation fault with setTextAlignment:

            you have to put one in

            setData puts the item in so it should be available afterwards

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            JonBJ 1 Reply Last reply
            1
            • VRoninV VRonin

              @JonB said in QTableWidget segmentation fault with setTextAlignment:

              you have to put one in

              setData puts the item in so it should be available afterwards

              JonBJ Online
              JonBJ Online
              JonB
              wrote on last edited by JonB
              #6

              @VRonin
              [Totally out of my head, not tested.] My understanding was that if you use the setData() level, that's fine, but I thought that did not create a QTableWidgetItem item for it. QTableWidget does not reimplement setData(), so you're saying it would have to do it on dataChanged() signal? (And that doesn't get emitted if you're not actually changing the value.)

              EDIT OK looked on woboq, agreed, it has internal QTableModel::setData() which does as you say about creating the item.

              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