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. Help with setting QTableView's vertical header data
QtWS25 Last Chance

Help with setting QTableView's vertical header data

Scheduled Pinned Locked Moved General and Desktop
24 Posts 3 Posters 25.8k 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.
  • H Offline
    H Offline
    holygirl
    wrote on last edited by
    #1

    Hey everybody!

         I need some help with setting the vertical header data of _QTableView_ to a list of strings in a _QStringList_. I am able to set the horizontal data using
    

    @
    QSqlQueryModel *model = new QSqlQueryModel;
    model->setHeaderData(0, Qt::Horizontal, tr("Age"));
    model->setHeaderData(1, Qt::Horizontal, tr("Salary"));
    @

    but when I try to customize the row header label, the following code returns false.

    @
    QSqlQueryModel *model = new QSqlQueryModel;
    model->setHeaderData(0, Qt::Vertical, tr("Andrew"));
    model->setHeaderData(1, Qt::Vertical, tr("Isobel"));
    @

    I found "this":http://www.qtcentre.org/threads/15328-setHeaderData-Qt-Vertical-on-QSqlTableModel but I didn't really understand how the OP implemented it. Could anybody please help me?

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hpollak
      wrote on last edited by
      #2

      See class-documentation of "QSqlQueryModel":http://qt-project.org/doc/qt-5.0/qtsql/qsqlquerymodel.html#setHeaderData for setHeaderData again:

      Returns true if orientation is Qt::Horizontal and the section refers to a valid section; otherwise returns false.

      1 Reply Last reply
      0
      • H Offline
        H Offline
        holygirl
        wrote on last edited by
        #3

        I did read the documentation before and I did so again. My question is, since it returns false for anything other than Qt::Horizontal, is it possible to set the label for the vertical headers? If yes, how?
        Thanks.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          IIRC for the QSqlQueryModel, the vertical headers text are based on what the select statement contains. In that case you can write your select statement with the column name you want to have in you headers.

          @SELECT a.columnName AS MyColumnName FROM a@

          BEWARE this is not tested.

          Out of curiosity, why do you want the vertical headers to contain people names ?
          Shouldn't you have something like Name, Age, Salary, and in your horizontal data "Andrew", "50", "2500" ?

          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
          • H Offline
            H Offline
            holygirl
            wrote on last edited by
            #5

            The solution you suggested is not an option because I want to generate the vertical header labels using the values in a QStringList.

            I gave names as vertical headers just as an example. I am coding a TV guide which will have channel names as the vertical header, shows of each channel as the rows and timings of the shows as the horizontal header. I was too lazy to explain all this which is why I gave names as an example :)

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Then, how does your query return the data for each separated channel ?

              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
              • H Offline
                H Offline
                holygirl
                wrote on last edited by
                #7

                The query is within a channels loop where each loop returns all the shows for a particular channel.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Is the loop in SQL or in c++ ?

                  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
                  • H Offline
                    H Offline
                    holygirl
                    wrote on last edited by
                    #9

                    c++

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      In that case, how do you setup the grid ? The QSqlQueryModel will only contain the data for the current channel not all

                      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
                      • H Offline
                        H Offline
                        holygirl
                        wrote on last edited by
                        #11

                        Yes. I do something like this

                        @Loop for channel {

                          For each channel, get all the show details
                          Loop for all the shows {
                                
                                   Display all the shows for channel n
                          }
                        
                          n++;
                        

                        }
                        @

                        So the result is

                        @
                        1 | Show Title | Show Title | Show Title | Show Title .....
                        2 | Show Title | Show Title | Show Title | Show Title ....
                        3 | Show Title | Show Title | Show Title | Show Title ....
                        @

                        But instead of the 1, 2, 3 .... in the column header, I want

                        @
                        HBO | Show Title | Show Title | Show Title | Show Title .....
                        CBS | Show Title | Show Title | Show Title | Show Title ....
                        Fox | Show Title | Show Title | Show Title | Show Title ....
                        @

                        I hope I'm able to put across my problem clearly. I'm struggling to find words to explain it to you. Sorry!

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #12

                          Hum... Just to be sure:
                          Do you mean:
                          @
                          HBO | CBS | Fox | <- horizontal headers
                          vertical headers->1 ShowTitle | ShowTitle |ShowTitle |
                          2 ShowTitle | ShowTitle |ShowTitle |
                          3 ShowTitle | ShowTitle |ShowTitle |
                          @

                          ?

                          EDITED:
                          Corrected horizontal/vertical headers

                          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
                          • H Offline
                            H Offline
                            holygirl
                            wrote on last edited by
                            #13

                            No no! I mean

                            @
                            2:00 | 2:30 | 3:00 | <- horizontal headers
                            vertical headers-> HBO ShowTitle | ShowTitle |ShowTitle |
                            CBS ShowTitle | ShowTitle |ShowTitle |
                            Fox ShowTitle | ShowTitle |ShowTitle |
                            @

                            I know it's weird but using the below code changes the row headers and not the column headers. Which basically means that the horizontal header is the row header and the vertical header is the column header.

                            @
                            QSqlQueryModel *model = new QSqlQueryModel;
                            model->setQuery("SELECT name FROM channel");
                            model->setHeaderData(0, Qt::Horizontal, tr("2:00"));
                            model->setHeaderData(1, Qt::Horizontal, tr("2:30"));
                            model->setHeaderData(1, Qt::Horizontal, tr("3:00"));
                            @

                            Thanks for being patient :)

                            1 Reply Last reply
                            0
                            • SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              Right, might be confusing: The horizontal header view is for the column name and the vertical is for the row name. It's more a positional point of view: The header view "deploys" horizontally so it shows the column name.

                              Anyway, the QSqlQueryModel doesn't allow to set the vertical header data. But what intrigues me is how to you make your grid from your SQL ?

                              With your last query you will only get one column with the channel names.

                              How does your database look like ?
                              A table with the channels and a table with the shows and their time ?

                              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
                              • H Offline
                                H Offline
                                holygirl
                                wrote on last edited by
                                #15

                                [quote author="SGaist" date="1366987686"]With your last query you will only get one column with the channel names.

                                How does your database look like ?
                                A table with the channels and a table with the shows and their time ?[/quote]

                                Yes, I will get only one column with the channel name which I want to display as the vertical headers.

                                Within the loop where I get the channel names, I use another SQL query to get all the show titles and timings for that particular channel which is active in the current loop.

                                For example, if my channels are HBO, Fox and CBS, I run a loop while query.next() to get each channel. Inside this loop, when HBO is my current channel query.value(0), I use the channel name to get the show titles and then display them. Since the loop will run thrice, I get all the show titles for all three channels one after the other.

                                1 Reply Last reply
                                0
                                • SGaistS Offline
                                  SGaistS Offline
                                  SGaist
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  Then... Why do you use a QTableView with a QSqlQueryModel ? You won't get all shows data since you change the query on each loop

                                  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
                                  • H Offline
                                    H Offline
                                    holygirl
                                    wrote on last edited by
                                    #17

                                    OMG! I'm so sorry. I'm confusing you and myself here. When I couldn't set the QTableView's vertical header, I moved on to generating the grid using just QLabels and that's when I used the looping technique. I'm so so sorry for the confusion.
                                    With QSqlQueryModel, i simply used a single query to get the show details. However, I couldn't display the channel details as the vertical header which is why I posted this question originally.
                                    Once again, I'm really very sorry :(

                                    1 Reply Last reply
                                    0
                                    • SGaistS Offline
                                      SGaistS Offline
                                      SGaist
                                      Lifetime Qt Champion
                                      wrote on last edited by
                                      #18

                                      If you are now running the query for each channel individually and pulling the data. You could simply use a QTableWidget and make the grid as you wanted before.

                                      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
                                      • H Offline
                                        H Offline
                                        holygirl
                                        wrote on last edited by
                                        #19

                                        I could. But there's another problem. Please have a look at "this question":http://qt-project.org/forums/viewthread/27263/ I posted.
                                        What I actually have to do is this:

                                        @

                                        | Label1 | Label2 | Label3 | Label4 |

                                        | Label5 | Label6 | Label7 | Label8 |

                                        | Label9 | Label10 | Label11 | Label12 |

                                        @

                                        So I not only have to generate these labels on the GUI, but also access them using indices so I can manipulate their properties like

                                        @
                                        label [0][2]->setText("Changed label");
                                        @

                                        I'm struggling to find the right data structure to use to solve my issue. Please please help.

                                        1 Reply Last reply
                                        0
                                        • H Offline
                                          H Offline
                                          holygirl
                                          wrote on last edited by
                                          #20

                                          Hi again!

                                              I couldn't solve my problem using _QTableWidget_ or similar widgets so I used a different "approach":http://qt-project.org/forums/viewthread/27263/ instead. I'd love to learn new ways of solving the same problem though :)
                                          
                                          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