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. How to get data from sqlquery that result 3 columns in a row and show only 1 Colum and use the other columns data
Forum Updated to NodeBB v4.3 + New Features

How to get data from sqlquery that result 3 columns in a row and show only 1 Colum and use the other columns data

Scheduled Pinned Locked Moved General and Desktop
28 Posts 3 Posters 15.1k Views 1 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.
  • A Offline
    A Offline
    andre
    wrote on last edited by
    #15

    [quote author="umen242" date="1333364703"]im using QSortFilterProxyModel to filter column that im getting from QSqlQueryModel model but becose the filterAcceptsColumn method is const "all the way" i have problem to set Qt::UserRole data in the right index . how can i overcome this?
    [/quote]
    I fail to understand why you'd need to modify the model from your filterAcceptsColumn() implementation.

    1 Reply Last reply
    0
    • U Offline
      U Offline
      umen242
      wrote on last edited by
      #16

      very simple , sorry im not native English speaker so the semantics are wrong (most of the time..).
      what im trying to do is very simple ( very simple in the logic of it )

      1. run sql query that returns 3 columns in each row ("select foo1,foo2,foo3 from tbl")
      2. use the data from column 1 and 2 ( concat the 2 strings )
      3. set it to column 3 Qt::UserRole placeholder ( i need it for later use ) that in the end becomes the only row that is displayed
        4 this is for each row / record that returns from the sql query
      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on last edited by
        #17

        OK. You don't need to modify the model at all for that. In either your base model or the proxy model, I would just reimplement the data method to return the right value for your Qt::UserRole for the column you're after. The filterAcceptsColumn method has nothing to do with that.

        1 Reply Last reply
        0
        • U Offline
          U Offline
          umen242
          wrote on last edited by
          #18

          well i have reimplement the data method in the QSqlQueryModel
          it looks like this :
          @QVariant MiniSqlModel::data(const QModelIndex &index, int role) const
          {
          QVariant value;
          int c = index.column(); //HERE IT IS ALWAYS == 2
          int r = index.row();
          value = QSqlQueryModel::data(index, role);
          QString s = value.toString();
          if(role == Qt::DisplayRole)
          {
          value = QSqlQueryModel::data(index, role);
          QString s = value.toString();
          }

          return value;
          }@

          the problem is that im getting only the last column (2) and i dont have the data of the other 2 .
          the only place i managed to catch the data of the first 2 columns (0,1) is in the filterAcceptsColumn method of the proxy .

          i missing here something small .. i feel i realy close to solve this . ( already 5 days .. )

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #19

            Of course! Your proxy model only has one column! That's what you wanted to achieve, after all...
            Your source model will still have all three columns, so you can still query these from the data method of proxy model.

            Edit:
            I would do something like this:
            @
            MyProxyModel::data(const QModelIndex& index, int role) const
            {
            if (!sourceModel())
            return QVariant(); //we don't do useful things without a source model

            if (index.column() == 0 && role == Qt::UserRole) {
            //get the merged data from the underlying model
            QModelIndex sourceIndex = mapToSource(index);
            if (!sourceIndex.isValid())
            return QVariant(); //you never know, right?

            QModelIndex col1Index = sourceModel()->index(sourceIndex.row(), 0, sourceIndex.parent());
            QString column1 = col1Index.data(Qt::DisplayRole).toString();
            QModelIndex col2Index = sourceModel()->index(sourceIndex.row(), 1, sourceIndex.parent());
            QString column2 = col1Index.data(Qt::DisplayRole).toString();
            
            QString result = column1 + " - " + column2;
            return QVariant(result);
            

            }
            return QSortFilterProxyModel(index, role);
            }
            @

            Note: the above is untested, just typed into the editor directly.

            1 Reply Last reply
            0
            • U Offline
              U Offline
              umen242
              wrote on last edited by
              #20

              no its the opposite, the data function is in the QSqlQueryModel.
              i dont understand this :
              @"...so you can still query these from the data method of proxy model...."@

              how ? where ?

              do you mean i need to implement the data method of the proxy ?

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on last edited by
                #21

                [quote author="umen242" date="1333367911"]no its the opposite, the data function is in the QSqlQueryModel.
                i dont understand this :
                @"...so you can still query these from the data method of proxy model...."@

                how ? where ?

                do you mean i need to implement the data method of the proxy ?[/quote]
                See the edit to my post above for an example.

                1 Reply Last reply
                0
                • U Offline
                  U Offline
                  umen242
                  wrote on last edited by
                  #22

                  Thanks very much i will try it now

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    andre
                    wrote on last edited by
                    #23

                    Note that this example does not try to modify any data. It just changes whatever it tells the user of the model is the data in it. Also note that I did not implement setData(). If you need to support setData too (for this role), things will quickly become more complicated...

                    1 Reply Last reply
                    0
                    • U Offline
                      U Offline
                      umen242
                      wrote on last edited by
                      #24

                      hi i tryed you example , and there is 2 problems and that i dont understand
                      first one is that when it is:
                      @if (index.column() == 0 && role == Qt::UserRole) {@
                      it never enter the if , but when i change it to
                      @if (c == 0 && role == Qt::DisplayRole) {@

                      it does Enter , but as expected the joined string is displayed in the row displayRole
                      how can i put it in the UserRole?

                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        andre
                        wrote on last edited by
                        #25

                        Of course it is not entered, it is only entered when the UserRole is actually requested. That is how it ends up "in" the display role. The role is only the argument you pass to the data() function. Nothing more, nothing less. The normal delegates don't query for the UserRole (they only need the standard roles).

                        1 Reply Last reply
                        0
                        • U Offline
                          U Offline
                          umen242
                          wrote on last edited by
                          #26

                          ok so how do i trigger it to be requested ? so i finally could set it

                          1 Reply Last reply
                          0
                          • A Offline
                            A Offline
                            andre
                            wrote on last edited by
                            #27

                            You simply request the data manually, where you need it. I assume you set this dat to the userRole for a reason, right? And that reason is presumably that you need this bit of information somewhere. So, how about actually requesting the data where you need it? It would be as simple as getting the right QModelIndex (that is: an index pointing to your proxy model; your view may supply it), and calling data(Qt::UserRole) on it.

                            1 Reply Last reply
                            0
                            • U Offline
                              U Offline
                              umen242
                              wrote on last edited by
                              #28

                              finally !
                              this wasn't trivial at all man . its working thanks to you .
                              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