Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    [Solved] Adding text to cells of a QTableWidget by code?

    General and Desktop
    3
    11
    16519
    Loading More Posts
    • 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.
    • CAD_coding
      CAD_coding last edited by

      Hi,
      I added a QTableWidget in my form class. I did not modify anything in form, just added the widget with all default properties.
      Then in a function I called setColumnCount() & setRowCount() so that I got the required no of cells.
      I can add text to the cells at run time by double clicking.
      But I cannot find a function to add text to a particular cell in a QTableWidget by code.

      How do I add the contents of a QString to a cell by code something like this: table->setCellText(QString text, int row, int column)?

      1 Reply Last reply Reply Quote 0
      • M
        messi last edited by

        Hi CAD_coding,

        you can add any kind of widgets to your cells with the following:
        yourTableWidget->setCellWidget(cellWidget, row, col);

        e.g. Create a QLabel and pass it to setCellWidget().

        You can also use QTableWidgetItem
        there you can use the function setText().
        with yourtableWidget->setItem(row, col, yourItem) you can add it to the QTableWidget.

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          Update the "item":http://qt-project.org/doc/qt-4.8/qtablewidget.html#item of the cell

          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 Reply Quote 0
          • M
            messi last edited by

            @SGaist

            Is the QTableWidget automatically filled with QTableWidgetItem?
            If this is true, yes the he can modify it with your suggeston .
            Otherwise he first has to create an item.

            1 Reply Last reply Reply Quote 0
            • M
              messi last edited by

              @ SGaist
              Items are created ouside the table (with no parent widget) and inserted into the table with setItem():

              Sorry my mistake. Have only read the the half question.
              You were totally right.

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                So it's working like you wanted ?

                If that's the case don't forget to update the thread's title to solved so other forum users may know that a solution has been found.

                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 Reply Quote 0
                • CAD_coding
                  CAD_coding last edited by

                  I am a bit confused here!
                  Do I have to create a QTableWidgetItem then add text to it & then setItem?
                  or directly call setText on QTableWidgetItem with correspondin row & column?

                  1 Reply Last reply Reply Quote 0
                  • SGaist
                    SGaist Lifetime Qt Champion last edited by

                    If you don't have an item already, create a new one with the text you want and set it.
                    If the item already exists update it's text.

                    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 Reply Quote 0
                    • CAD_coding
                      CAD_coding last edited by

                      Hi SGaist,
                      I had to create a QTableWidgetItem in heap memory & set it as an item in my table widget.
                      Now I have some questions:

                      1. Is the item automatically deleted when the table is deleted?
                        According to "this":http://qt-project.org/doc/qt-4.8/qtablewidget.html#setItem :

                      bq. The table takes ownership of the item.

                      So I think it should be deleting the items while deleting the table. Correct?

                      1. I am having some problem in setting the alignment of text.
                        According to "this":http://qt-project.org/doc/qt-4.8/qtablewidgetitem.html#setTextAlignment it takes only 1 int arg. Now I want HCENTRE & VCENTRE Alignment both. I tried by passing (Qt::AlignHCentre && Qt::AlignVCentre). It doesnt work.
                      1 Reply Last reply Reply Quote 0
                      • SGaist
                        SGaist Lifetime Qt Champion last edited by

                        1. Correct

                        2. Sure it doesn't work, your are doing a logical AND, that's not the way to set flags you should either use Qt::AlignCenter or Qt::AlignHCenter | Qt::AlignVCenter, have a look at the "Alignment enum":http://qt-project.org/doc/qt-4.8/qt.html#AlignmentFlag-enum

                        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 Reply Quote 0
                        • CAD_coding
                          CAD_coding last edited by

                          ohh got it!
                          Thanks

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post