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. QTableView, adding headers and content
Forum Updated to NodeBB v4.3 + New Features

QTableView, adding headers and content

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 2 Posters 5.0k Views 2 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 9 May 2021, 14:33 last edited by
    #1

    I have created a table in MariaDB that has 2 fields in each record:

    Primary key (BIGINT) auto numbered
    Params (JSON array)

    Each element of the JSON array is a JSON object where the name is going to be the table column header and the value the row data for that column.

    So far I've created an instance of QTableView, I'm not sure how I add rows to the table and set-up column headers. I've been looking around for examples.

    Should I be using QTableView or QTableWidget ?

    Kind Regards,
    Sy

    1 Reply Last reply
    0
    • S SPlatten
      9 May 2021, 14:48

      @mrjj , each record has a JSON array which I've done to ensure the order is the same. Is there an example which I can reference?

      M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 9 May 2021, 14:56 last edited by mrjj 5 Sept 2021, 14:56
      #4

      @SPlatten
      Hi
      well you must then parse the json and loop the array to get each values to use for the item.

      Using a view + QStandardItemModel.

      int r=10;
      int c=5;
      model = new QStandardItemModel(r,c);
      table = new QTableView();
      table->setModel(model); 
          for (int row = 0; row < r; ++row) {
              for (int column = 0; column < c; ++column) {
                  QStandardItem *item = new QStandardItem(QString("iii"));
                  model->setItem(row,column,item);
              }
          }
      

      So like the TableWidget you simply give it items.

      S 1 Reply Last reply 9 May 2021, 14:58
      1
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 9 May 2021, 14:46 last edited by
        #2

        Hi
        well, you use a model with your view.
        So you could stuff the JSON values into a
        https://doc.qt.io/qt-5/qstandarditemmodel.html
        which is basically what a QTableWidget does.

        I assume you just plan to loop the data from the db and add to the model.
        I mean, you dont read the db data into any kind of internal storage already ?

        S 1 Reply Last reply 9 May 2021, 14:48
        0
        • M mrjj
          9 May 2021, 14:46

          Hi
          well, you use a model with your view.
          So you could stuff the JSON values into a
          https://doc.qt.io/qt-5/qstandarditemmodel.html
          which is basically what a QTableWidget does.

          I assume you just plan to loop the data from the db and add to the model.
          I mean, you dont read the db data into any kind of internal storage already ?

          S Offline
          S Offline
          SPlatten
          wrote on 9 May 2021, 14:48 last edited by SPlatten 5 Sept 2021, 14:49
          #3

          @mrjj , each record has a JSON array which I've done to ensure the order is the same. Is there an example which I can reference?

          Kind Regards,
          Sy

          M 1 Reply Last reply 9 May 2021, 14:56
          0
          • S SPlatten
            9 May 2021, 14:48

            @mrjj , each record has a JSON array which I've done to ensure the order is the same. Is there an example which I can reference?

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 9 May 2021, 14:56 last edited by mrjj 5 Sept 2021, 14:56
            #4

            @SPlatten
            Hi
            well you must then parse the json and loop the array to get each values to use for the item.

            Using a view + QStandardItemModel.

            int r=10;
            int c=5;
            model = new QStandardItemModel(r,c);
            table = new QTableView();
            table->setModel(model); 
                for (int row = 0; row < r; ++row) {
                    for (int column = 0; column < c; ++column) {
                        QStandardItem *item = new QStandardItem(QString("iii"));
                        model->setItem(row,column,item);
                    }
                }
            

            So like the TableWidget you simply give it items.

            S 1 Reply Last reply 9 May 2021, 14:58
            1
            • M mrjj
              9 May 2021, 14:56

              @SPlatten
              Hi
              well you must then parse the json and loop the array to get each values to use for the item.

              Using a view + QStandardItemModel.

              int r=10;
              int c=5;
              model = new QStandardItemModel(r,c);
              table = new QTableView();
              table->setModel(model); 
                  for (int row = 0; row < r; ++row) {
                      for (int column = 0; column < c; ++column) {
                          QStandardItem *item = new QStandardItem(QString("iii"));
                          model->setItem(row,column,item);
                      }
                  }
              

              So like the TableWidget you simply give it items.

              S Offline
              S Offline
              SPlatten
              wrote on 9 May 2021, 14:58 last edited by
              #5

              @mrjj , thank you, do I have to specify the rows and columns up front or can I do add rows dynamically?

              Kind Regards,
              Sy

              M 1 Reply Last reply 9 May 2021, 15:05
              0
              • S SPlatten
                9 May 2021, 14:58

                @mrjj , thank you, do I have to specify the rows and columns up front or can I do add rows dynamically?

                M Offline
                M Offline
                mrjj
                Lifetime Qt Champion
                wrote on 9 May 2021, 15:05 last edited by mrjj 5 Sept 2021, 15:06
                #6

                @SPlatten

                Hi
                Well you do tell it a start col and row count but
                the most operation will raise the values if needed and
                you can also manually raise the limits when ever needed.

                void QStandardItemModel::setItem(int row, int column, QStandardItem *item)
                Sets the item for the given row and column to item. The model takes ownership of the item. If necessary, the row count and column count are increased to fit the item.

                Also note it has a InsertRows that takes a list and adds whole row at a time.

                S 1 Reply Last reply 9 May 2021, 15:09
                0
                • M mrjj
                  9 May 2021, 15:05

                  @SPlatten

                  Hi
                  Well you do tell it a start col and row count but
                  the most operation will raise the values if needed and
                  you can also manually raise the limits when ever needed.

                  void QStandardItemModel::setItem(int row, int column, QStandardItem *item)
                  Sets the item for the given row and column to item. The model takes ownership of the item. If necessary, the row count and column count are increased to fit the item.

                  Also note it has a InsertRows that takes a list and adds whole row at a time.

                  S Offline
                  S Offline
                  SPlatten
                  wrote on 9 May 2021, 15:09 last edited by SPlatten 5 Sept 2021, 15:13
                  #7

                  @mrjj , I'm using Qt 5.9.2 and so far I've added:

                  mptwRecs = new QTableWidget(this);
                  mpsiModel = new QStandardItemModel(this);
                  mptwRecs->setModel(mpsiModel);
                  

                  This will not compile and I get the error message:

                  C2248: `QTableWidget::setModel` cannot access private member declared in class `QTableWidget`
                  

                  Fixed, looks like I have to use QTableView.

                  Kind Regards,
                  Sy

                  M 1 Reply Last reply 9 May 2021, 16:11
                  1
                  • S SPlatten
                    9 May 2021, 15:09

                    @mrjj , I'm using Qt 5.9.2 and so far I've added:

                    mptwRecs = new QTableWidget(this);
                    mpsiModel = new QStandardItemModel(this);
                    mptwRecs->setModel(mpsiModel);
                    

                    This will not compile and I get the error message:

                    C2248: `QTableWidget::setModel` cannot access private member declared in class `QTableWidget`
                    

                    Fixed, looks like I have to use QTableView.

                    M Offline
                    M Offline
                    mrjj
                    Lifetime Qt Champion
                    wrote on 9 May 2021, 16:11 last edited by mrjj 5 Sept 2021, 17:18
                    #8

                    @SPlatten
                    Hi
                    yes the TableWIDGET comes with its onw internal model and it wont use external one.
                    So yes., Use TreeView.

                    S 1 Reply Last reply 9 May 2021, 17:02
                    0
                    • M mrjj
                      9 May 2021, 16:11

                      @SPlatten
                      Hi
                      yes the TableWIDGET comes with its onw internal model and it wont use external one.
                      So yes., Use TreeView.

                      S Offline
                      S Offline
                      SPlatten
                      wrote on 9 May 2021, 17:02 last edited by SPlatten 5 Sept 2021, 17:08
                      #9

                      @mrjj , did you mean TreeView or are you just trying to confuse me...more ? :)

                      Kind Regards,
                      Sy

                      M 1 Reply Last reply 9 May 2021, 17:18
                      0
                      • S SPlatten
                        9 May 2021, 17:02

                        @mrjj , did you mean TreeView or are you just trying to confuse me...more ? :)

                        M Offline
                        M Offline
                        mrjj
                        Lifetime Qt Champion
                        wrote on 9 May 2021, 17:18 last edited by
                        #10

                        @SPlatten

                        Sorry :)
                        I meant TableView. (no trees)

                        S 1 Reply Last reply 9 May 2021, 17:35
                        0
                        • M mrjj
                          9 May 2021, 17:18

                          @SPlatten

                          Sorry :)
                          I meant TableView. (no trees)

                          S Offline
                          S Offline
                          SPlatten
                          wrote on 9 May 2021, 17:35 last edited by
                          #11

                          @mrjj , next related question, when I add the rows I use:

                          lstRow.append(new QStandardItem(strValue));
                          

                          Later once the list is ready to add:

                          mpsiModel->appendRow(lstRow);
                          

                          The question is how do I get access to the row list when I want to delete each instance of QStandardItem?

                          Kind Regards,
                          Sy

                          M 1 Reply Last reply 9 May 2021, 17:42
                          0
                          • S SPlatten
                            9 May 2021, 17:35

                            @mrjj , next related question, when I add the rows I use:

                            lstRow.append(new QStandardItem(strValue));
                            

                            Later once the list is ready to add:

                            mpsiModel->appendRow(lstRow);
                            

                            The question is how do I get access to the row list when I want to delete each instance of QStandardItem?

                            M Offline
                            M Offline
                            mrjj
                            Lifetime Qt Champion
                            wrote on 9 May 2021, 17:42 last edited by
                            #12

                            @SPlatten
                            Hi
                            Well the models own the items so you dont need to clean up. It will do.
                            If you want to empty it, you can call clear() on it.

                            S 2 Replies Last reply 9 May 2021, 17:43
                            1
                            • M mrjj
                              9 May 2021, 17:42

                              @SPlatten
                              Hi
                              Well the models own the items so you dont need to clean up. It will do.
                              If you want to empty it, you can call clear() on it.

                              S Offline
                              S Offline
                              SPlatten
                              wrote on 9 May 2021, 17:43 last edited by
                              #13

                              @mrjj , thank you.

                              Kind Regards,
                              Sy

                              1 Reply Last reply
                              0
                              • M mrjj
                                9 May 2021, 17:42

                                @SPlatten
                                Hi
                                Well the models own the items so you dont need to clean up. It will do.
                                If you want to empty it, you can call clear() on it.

                                S Offline
                                S Offline
                                SPlatten
                                wrote on 9 May 2021, 17:54 last edited by
                                #14

                                @mrjj , A few more tweets, each row has a lot of space and padding top and bottom of each row, including headers, I would like to remove this and also I would like to disable editing of the cells.

                                I want to make it so when I double click a row it selects that row and then signals my application of this.

                                Kind Regards,
                                Sy

                                M 1 Reply Last reply 9 May 2021, 18:45
                                0
                                • S SPlatten
                                  9 May 2021, 17:54

                                  @mrjj , A few more tweets, each row has a lot of space and padding top and bottom of each row, including headers, I would like to remove this and also I would like to disable editing of the cells.

                                  I want to make it so when I double click a row it selects that row and then signals my application of this.

                                  M Offline
                                  M Offline
                                  mrjj
                                  Lifetime Qt Champion
                                  wrote on 9 May 2021, 18:45 last edited by mrjj 5 Sept 2021, 18:46
                                  #15

                                  @SPlatten

                                  hi
                                  selecting row:
                                  table->setSelectionBehavior(QAbstractItemView::SelectRows);

                                  for nonediting, you can set that on the item

                                  item->setFlags(item->flags() & ~Qt::ItemIsEditable) // we remove the edit flag. there are others flags so we only clear that one out as not to fux up stuff

                                  for the spaces:

                                  QHeaderView* header=tableView->verticalHeader();
                                  header->setDefaultSectionSize(20); // 20 px height for all

                                  S 1 Reply Last reply 10 May 2021, 06:00
                                  1
                                  • M mrjj
                                    9 May 2021, 18:45

                                    @SPlatten

                                    hi
                                    selecting row:
                                    table->setSelectionBehavior(QAbstractItemView::SelectRows);

                                    for nonediting, you can set that on the item

                                    item->setFlags(item->flags() & ~Qt::ItemIsEditable) // we remove the edit flag. there are others flags so we only clear that one out as not to fux up stuff

                                    for the spaces:

                                    QHeaderView* header=tableView->verticalHeader();
                                    header->setDefaultSectionSize(20); // 20 px height for all

                                    S Offline
                                    S Offline
                                    SPlatten
                                    wrote on 10 May 2021, 06:00 last edited by SPlatten 5 Oct 2021, 06:05
                                    #16

                                    @mrjj ,the headers I want to resize are the horizontal headers, I tried:

                                    QHeaderView* phvHorizontal(mptvRecs->horizontalHeader());
                                    phvHoriztonal->setDefaultSectionSize(14);
                                    

                                    However that effects the header width and not the height.

                                    Kind Regards,
                                    Sy

                                    M 1 Reply Last reply 10 May 2021, 06:06
                                    0
                                    • S SPlatten
                                      10 May 2021, 06:00

                                      @mrjj ,the headers I want to resize are the horizontal headers, I tried:

                                      QHeaderView* phvHorizontal(mptvRecs->horizontalHeader());
                                      phvHoriztonal->setDefaultSectionSize(14);
                                      

                                      However that effects the header width and not the height.

                                      M Offline
                                      M Offline
                                      mrjj
                                      Lifetime Qt Champion
                                      wrote on 10 May 2021, 06:06 last edited by
                                      #17

                                      @SPlatten
                                      Hi
                                      The verticalheader() gives access to the "height"

                                      S 2 Replies Last reply 10 May 2021, 06:13
                                      0
                                      • M mrjj
                                        10 May 2021, 06:06

                                        @SPlatten
                                        Hi
                                        The verticalheader() gives access to the "height"

                                        S Offline
                                        S Offline
                                        SPlatten
                                        wrote on 10 May 2021, 06:13 last edited by SPlatten 5 Oct 2021, 06:18
                                        #18

                                        @mrjj, checking this out now...also I'm using a lambda connection to connect to:

                                        QObject::connect(mptvRecs, &QTableView::doubleClicked, [this, objJSONdataTypes]() {
                                            ...
                                        });
                                        

                                        This works, but is there anyway to get access to the parameter associated with the doubleClicked signal from lambda ?

                                        [Edit]...trying

                                        QObject::connect(mptvRecs, &QTableView::doubleClicked, [this, objJSONdataTypes](const QModelIndex& crIndex) {
                                            ...
                                        });
                                        

                                        [Edit2], yes that works!

                                        Kind Regards,
                                        Sy

                                        1 Reply Last reply
                                        1
                                        • M mrjj
                                          10 May 2021, 06:06

                                          @SPlatten
                                          Hi
                                          The verticalheader() gives access to the "height"

                                          S Offline
                                          S Offline
                                          SPlatten
                                          wrote on 10 May 2021, 06:27 last edited by SPlatten 5 Oct 2021, 06:42
                                          #19

                                          @mrjj , Is there a way to remove the padding which seems to be present above and below the text regardless of the height setting ?

                                          If I use setFixedHeight there are still several pixels above the text.

                                          [Edit] Yes, by using setStyleSheet on header.

                                          Kind Regards,
                                          Sy

                                          1 Reply Last reply
                                          1

                                          1/19

                                          9 May 2021, 14:33

                                          • Login

                                          • Login or register to search.
                                          1 out of 19
                                          • First post
                                            1/19
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved