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. Get text from table cell
QtWS25 Last Chance

Get text from table cell

Scheduled Pinned Locked Moved General and Desktop
9 Posts 4 Posters 15.3k 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.
  • I Offline
    I Offline
    Indrajeet
    wrote on last edited by
    #1

    Hi All

    I have a QTableWidget & I am trying to get the content of the cell of this table.

    @QString str = ui.tableWidget->item( row, col )->text();@

    Through the above code I am able to get the content of required cell but if the cell is empty this line of code is crashing.
    So how to handle for empty cell

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi Ravjerr,

      you are querying first for an object (item) and access it without checking if the element exists. What you could do is using the model instead:

      @
      QAbstractItemModel* model = ui.tableWidget->model();
      QModelIndex idx = model->index(row, col);
      QString str = model->data(idx)->toString();
      @

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #3

        or, alternatively, you actually check the pointers:

        @
        QString str;
        QTableWidgetItem* itm = ui.tableWidget->item( row, col );
        if (itm)
        str = itm->text();

        @

        1 Reply Last reply
        0
        • I Offline
          I Offline
          Indrajeet
          wrote on last edited by
          #4

          Thanks Gerolf.

          But as we are doing 3 steps to get the data,wether it will affect the performance if we do for all items in table.

          1 Reply Last reply
          0
          • G Offline
            G Offline
            giesbert
            wrote on last edited by
            #5

            These steps should all be fast.
            On the other side, you could use your version and always check the result of item to 0, like Andre suggested.

            Nokia Certified Qt Specialist.
            Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

            1 Reply Last reply
            0
            • G Offline
              G Offline
              goetz
              wrote on last edited by
              #6

              [quote author="Rajveer" date="1315815091"]Thanks Gerolf.

              But as we are doing 3 steps to get the data,wether it will affect the performance if we do for all items in table.[/quote]

              Think of performance issues after you are bitten by those, not before. Premature optimization leads to errors which are hard to find.

              An BTW: the code André posted is basically what's done behind the scenes. Gerolf's solution is a bit more elegant regarding reading your own code though.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #7

                [quote author="Volker" date="1315897394"]An BTW: the code André posted is basically what's done behind the scenes. Gerolf's solution is a bit more elegant regarding reading your own code though.[/quote]
                Actually, it is not. My solution uses the QTableWidget* API, not the QAIM API. Personally, I find the latter a bit of a hack to use in this case, as you are using the QTableWidget for a reason, I presume. Behind the scenes, Gerolfs solution obviously does not fall back to using the QTableWidget API. All it does is just make a direct call into the QTableWidget's model, which should by all rights be private anyway.

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  giesbert
                  wrote on last edited by
                  #8

                  My method uses the QAIM API, that is right, and will also work with MVC concepts.
                  Yes I use the model of QTableWidget, but the model itself is not private. So why not use it?

                  Nokia Certified Qt Specialist.
                  Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #9

                    I am not saying that you can not use it. You proved that you can. However, I think that the fact that QTableWidget has an accessible model() method is actually an oversight. It exposes an implementation detail of the class. It might have been better if the widget versions of the item views would have used encapsulation instead of inheritance.

                    Furthermore, in the general case, I am of the opinion that you should not use the QAIM API for other purposes than to provide an interface for views and delegates (and to create classes like QDataWidgetMapper). The API is not designed for that, and you should use application specific API to interact with the data store object that is underlying the model. The QAIM is here only an adaptor class between the data store and the Qt view classes to visualize data from that store.

                    In this case, the QTableWidget is the class that offers the API to manipulate the data, so it plays the role of both the data store and the view, and the actual model used is obscured from view. Following my reasoning, you should use the API of the data store to manipulate the data, that is, the QTableWidget and QTableWidgetItem API.

                    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