Important: Please read the Qt Code of Conduct -

[SOLVED] Qt table

  • 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?


  • 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) {
    } 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.

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

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

  • 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.

  • 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''

  • 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) {
    } else {
    // failed to convert the data to a double

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

  • 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": "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.

    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.

  • [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,


    is equivalent to


    (provided neither operators are overloaded)

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

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

Log in to reply