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] Qt table
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] Qt table

Scheduled Pinned Locked Moved General and Desktop
9 Posts 3 Posters 4.7k 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.
  • B Offline
    B Offline
    bareil76
    wrote on last edited by
    #1

    Hi,

    I have setup a QtableWidget. I then want to put all the numbers the user will enter into a buffer( vector) and I cannot seems to find how to put all the number entered into a vector.

    Could someone point me to an example or litterature?

    Thanks

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jmelbye
      wrote on last edited by
      #2

      You should be able to loop over all of the items. I haven't used the table widget class before. It looks like all of its data is stored inside QTableWidgetItem objects. You can retrieve data from a QTableWidgetItem by calling it's data() member which returns a QVariant. Finally, we'll call toDouble() to cast the QVariant to a double (and pass a pointer to a bool to check for successful conversion to double).

      If your numbers are say doubles, you could loop over it like this:
      @QVector<double> buffer;
      for (int r = 0; r < tableWidget.rowCount(); ++r) {
      bool ok;
      double number = tableWidget.item(r, 0).data().toDouble(&ok);
      if (ok) {
      buffer.append(number);
      } else {
      // failed to convert the data to a double
      }
      }
      @

      Note that item() takes a row and column. I've used column 0, but you may have to change that depending on how your table is structured.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goblincoding
        wrote on last edited by
        #3

        You'd need to call text() on the QTableWidgetItem and not data():

        @
        double number = tableWidget.item(r, 0).text().toDouble(&ok);
        @

        http://www.goblincoding.com

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jmelbye
          wrote on last edited by
          #4

          Ah, I didn't see text when I scanned over the class members. I also have to make a correction to my earlier post.

          The data() member takes a role argument. Qt::DisplayRole should return text. My code should be updated to be:
          @double number = tableWidget.item(r, 0).data(Qt::DisplayRole).toDouble(&ok);@

          I suspect text() is simply a convenience function that does the same thing as calling data() with the role specified as Qt::DisplayRole. If that's not true, please do correct me!

          By passing a role to data(), you can get other view-related information like formatting about a particular item in the table. That is might be more important if you are using QTableView or another view class. Whichever you use is up to you. text() saves you a handful of keystrokes.

          1 Reply Last reply
          0
          • B Offline
            B Offline
            bareil76
            wrote on last edited by
            #5

            Thanks for your help!

            I tried it and your example helped me to understand the class information.

            However, I don't understand how to set up the header files. I get the following errors.

            error: C2065: 'tableWidget' : undeclared identifier
            C2228: left of '.rowCount' must have class/struct/union type is ''unknown-type''

            1 Reply Last reply
            0
            • B Offline
              B Offline
              bareil76
              wrote on last edited by
              #6

              Ok.. I found it ... but I don't understand why it works like that.

              I had to do the following to get rid of the error message

              @QVector<double> buffer;
              for (int r = 0; r < ui->tableWidget->rowCount(); ++r) {
              bool ok;
              double number = ui->tableWidget->item(r, 0)->data(Qt::DisplayRole).toDouble(&ok);
              if (ok) {
              buffer.append(number);
              } else {
              // failed to convert the data to a double
              }
              }@

              So the question is why -> and ui-> instead of .

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jmelbye
                wrote on last edited by
                #7

                It sounds like you created your window/widget with Qt Creator's designer. When you do that, it creates a class to hold all of your graphical widgets, and defines a function setupUi() to set them all up. It then wraps this class up in the "namespace":http://www.cplusplus.com/doc/tutorial/namespaces/ "Ui".

                So, if you are making a main window, you'll likely have a class called MainWindow, and you'll see that in its private members section, there is a member: @Ui::MainWindow *ui;@

                So ui is the name of the object that keeps track of your table widget, and other widgets. If you want to see what the Ui::MainWindow class looks like, it is generated for you and the header file will be called ui_mainwindow.h. If your class isn't named MainWindow, the convention is still the same. For any window/widget class Foo, look for a header file ui_foo.h.

                edit:
                To add to my comments because I didn't explain that the best: "ui" is just a convention. When you are using the Designer, it provides a way to isolate the generated code from the code you write. All you need to know is that after calling setupUi(), all your widgets are properly configured as specified in the Designer. Then to access any widget, you can find it at ui->myWidget.

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

                  [quote author="bareil76" date="1358784525"]So the question is why -> and ui-> instead of .[/quote]

                  -> and . are called "member access operators".

                  When working with the value of a struct or class, you use the "." to access member functions/public member variables. However, when you're working with a pointer to an object, you need to dereference the pointer (get the value) in order to access its members and this is where -> comes in since -> both dereferences the pointer AND provides access to the accessible members.

                  In other words,

                  @ui->yourFunction()@

                  is equivalent to

                  @(*ui).yourFunction()@

                  (provided neither operators are overloaded)

                  http://www.goblincoding.com

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    bareil76
                    wrote on last edited by
                    #9

                    You are right about ui.. I used qt creator!

                    Thanks a lot... I should be able to take it from here!

                    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