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] Adding text to cells of a QTableWidget by code?

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

Scheduled Pinned Locked Moved General and Desktop
11 Posts 3 Posters 17.9k 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.
  • CAD_codingC Offline
    CAD_codingC Offline
    CAD_coding
    wrote on last edited by
    #1

    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
    0
    • M Offline
      M Offline
      messi
      wrote on last edited by
      #2

      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
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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
        0
        • M Offline
          M Offline
          messi
          wrote on last edited by
          #4

          @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
          0
          • M Offline
            M Offline
            messi
            wrote on last edited by
            #5

            @ 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
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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
              0
              • CAD_codingC Offline
                CAD_codingC Offline
                CAD_coding
                wrote on last edited by
                #7

                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
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  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
                  0
                  • CAD_codingC Offline
                    CAD_codingC Offline
                    CAD_coding
                    wrote on last edited by
                    #9

                    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
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10
                      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
                      0
                      • CAD_codingC Offline
                        CAD_codingC Offline
                        CAD_coding
                        wrote on last edited by
                        #11

                        ohh got it!
                        Thanks

                        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