March 28, 2012

umen242 umen242
Ant Farmer
326 posts

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

Page  
1

hello all
im using QSqlQueryModel subclass to excute sql querys and popolate them to tableview
now i have simple sql query that results 3 coulms / fields in arow that invoked in the QSqlQueryModel subclas like this :

  1. QString query_local = "SELECT foo1,foo2,foo3 FROM tbl" ;
  2. QSqlQueryModel::setQuery(query_local, queryDB);
  3. if (lastError().type() != QSqlError::NoError)
  4. {
  5.          QString err = lastError().text();
  6. }

now later i set this to show me only foo3 in the table view that contains 1 column

  1. QSqlQueryModel::removeColumns(0,2);

what i need is that the data from foo1 and foo2 and set in the the UserRole of each row in the tableview ( i have itemDelegate of the tableView ) and show in the display only foo3
how can it be done ?

27 replies

March 29, 2012

Lykurg Lykurg
Lab Rat
13 posts

Create your own model. Subclass QSqlQueryModel and reimp QSqlQueryModel::data(). If role is Qt::MyMagicFoo1 then use e.g.

  1. foo1Idx = index.sibling(index.row(), 0);
  2. // data of foo1
  3. index.data(Qt::DisplayRole);

EDIT: where 0 is the column of foo1 sql position.

April 1, 2012

umen242 umen242
Ant Farmer
326 posts

hi
Thanks for the replay but i didnt understand it .
in the data subclass method i did :

  1.          QVariant value;
  2.   int c = index.column();
  3.   int r = index.row();
  4.  
  5.  
  6.  
  7.   value = QSqlQueryModel::data(index, role);
  8.   QString s = value.toString();
  9.   if(role == Qt::DisplayRole)
  10.   {
  11.   value = QSqlQueryModel::data(index, role);
  12.   QString s = value.toString();
  13.   }
  14.   if(c ==2)
  15.   {
  16.    return value;
  17.   }
  18.   value ="";
  19.   return value;

but it still show me in the result 3 columns , and only in the third the display
i dont what to display 3 columns only one

April 2, 2012

Andre Andre
Area 51 Engineer
6031 posts

The way I see it, you have two options:

  1. Just hide the columns you don’t want to see in the view. QTableView supports the hideColumn(int column) method for that.
  2. Use a QSortFilterProxyModel to hide the columns you don’t want, by subclassing that class and reimplementing the filterAcceptsColumn method to return false for the columns you want to hide.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 2, 2012

umen242 umen242
Ant Farmer
326 posts

hi and thanks for the replay i tryed to use hideColumn but it didnt work

  1. m_pPlayListMiniItemDelegate =new PlayListMiniItemDelegate(this);
  2.  ui->PlayListMini_tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
  3.  ui->PlayListMini_tableView->setSelectionMode(QAbstractItemView::SingleSelection);
  4.  ui->PlayListMini_tableView->setItemDelegate(m_pPlayListMiniItemDelegate);
  5.  ui->PlayListMini_tableView->setAlternatingRowColors(true);
  6.  ui->PlayListMini_tableView->setModel(PlayListMiniSqlModel::instance());
  7.  ui->PlayListMini_tableView->hideColumn(0);
  8.  ui->PlayListMini_tableView->hideColumn(1);

i want to avoid to add yet another model to the stack

April 2, 2012

Andre Andre
Area 51 Engineer
6031 posts

You mean that even after calling hideColumn, it is still visible in your view?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 2, 2012

umen242 umen242
Ant Farmer
326 posts

yes , but i implementing the QSortFilterProxyModel now i hope it will work finally …

April 2, 2012

Andre Andre
Area 51 Engineer
6031 posts

Well, that would be a bug in Qt. If you could create a minimal simple, self-contained example that has this behaviour, then please share that.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 2, 2012

umen242 umen242
Ant Farmer
326 posts

will do , it will take time to build complex GUI as sample . but i will

April 2, 2012

Andre Andre
Area 51 Engineer
6031 posts

Please keep it as simple as possible when creating a sample. Less is more in these cases!

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 2, 2012

umen242 umen242
Ant Farmer
326 posts

sure , by the way the filter proxy worked . thanks!

April 2, 2012

umen242 umen242
Ant Farmer
326 posts

Hi but now i have another problem , in the proxy model
i set to filter the 2 first columns like this :

  1. bool PlayListMiniSortFilterProxyModel::filterAcceptsColumn(int source_column, const QModelIndex& index) const
  2. {
  3.  //qDebug() << "filterAcceptsColumn(): column = " << source_column ;
  4.  if(source_column ==2)
  5.   return QSortFilterProxyModel::filterAcceptsColumn(source_column,index);
  6.  return false;
  7. }

and in the PlayListMiniSqlModel ( its the QSqlQueryModel im using in the table)
when i try to get the display string of the hidden 2 columns i only get the 3 column .

  1. QVariant PlayListMiniSqlModel::data(const QModelIndex &index, int role) const
  2. {
  3.      
  4.  
  5.   QVariant value;
  6.  
  7.   int c = index.column();  //HERE IT ONLY SHOW ME 2 , NEVER 0 and 1
  8.   int r = index.row();
  9.  
  10.    value = QSqlQueryModel::data(index, role);
  11.   QString s = value.toString();
  12.   if(role == Qt::DisplayRole)
  13.   {
  14.   value = QSqlQueryModel::data(index, role);
  15.   QString s = value.toString();
  16.   }
  17.  
  18.   return value;
  19. }

where in the model i can get the hidden column data ?

April 2, 2012

umen242 umen242
Ant Farmer
326 posts

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?

  1. bool MiniSortFilterProxyModel::filterAcceptsColumn(int source_column, const QModelIndex& index) const
  2.     {
  3.         QVariant tmp ;
  4.         if(source_column ==0)
  5.         {
  6.  
  7.             setRowid(index.data(Qt::DisplayRole));
  8. // here im getting compilation error
  9. //: error C2678: binary '=' : no operator found which takes a left-hand operand of type 'const QString' (or there is no acceptable conversion)
  10.             m_rowId = index.model()->data(index,Qt::DisplayRole).toString();
  11.             return false;
  12.         }
  13.         else if(source_column ==1)
  14.         {
  15.             setYTid(index.data(Qt::DisplayRole));
  16.             return false;
  17.         }
  18.         else if(source_column ==2)
  19.         {
  20.  
  21.             setNewData(index);
  22.             return QSortFilterProxyModel::filterAcceptsColumn(source_column,index);
  23.         }
  24.         return false;
  25.     }
  26.  
  27.  
  28.     void MiniSortFilterProxyModel::setRowid(QVariant rowId) const
  29.     {
  30. // here also compilation error:
  31.         m_rowId = rowId.toString();
  32.     }
  33.  
  34.     void MiniSortFilterProxyModel::setYTid(QVariant ytId) const
  35.     {
  36. / here also compilation error:      
  37. m_ytId = ytId.toString();
  38.     }
  39.  
  40.     void MiniSortFilterProxyModel::setNewData(QModelIndex& index) const
  41.     {
  42. // here also compilation error:
  43. //error C2511: 'void MiniSortFilterProxyModel::setNewData(QModelIndex &) const' : //overloaded member function not found in 'MiniSortFilterProxyModel'            
  44.  QVariant data = m_rowId+"_"+m_ytId;
  45.             index.model()->setData(index,data,Qt::UserRole);
  46.     }

April 2, 2012

Andre Andre
Area 51 Engineer
6031 posts

umen242 wrote:
Hi but now i have another problem , in the proxy model
i set to filter the 2 first columns like this :
  1. bool PlayListMiniSortFilterProxyModel::filterAcceptsColumn(int source_column, const QModelIndex& index) const
  2. {
  3.  //qDebug() << "filterAcceptsColumn(): column = " << source_column ;
  4.  if(source_column ==2)
  5.   return QSortFilterProxyModel::filterAcceptsColumn(source_column,index);
  6.  return false;
  7. }

and in the PlayListMiniSqlModel ( its the QSqlQueryModel im using in the table)
when i try to get the display string of the hidden 2 columns i only get the 3 column .

  1. QVariant PlayListMiniSqlModel::data(const QModelIndex &index, int role) const
  2. {
  3.      
  4.  
  5.   QVariant value;
  6.  
  7.   int c = index.column();  //HERE IT ONLY SHOW ME 2 , NEVER 0 and 1
  8.   int r = index.row();
  9.  
  10.    value = QSqlQueryModel::data(index, role);
  11.   QString s = value.toString();
  12.   if(role == Qt::DisplayRole)
  13.   {
  14.   value = QSqlQueryModel::data(index, role);
  15.   QString s = value.toString();
  16.   }
  17.  
  18.   return value;
  19. }

where in the model i can get the hidden column data ?

That sounds logical to me. The data() method will be called by the view or proxy model on top of the model. That object will (as much as possible) only query for the items it needs. In this case, that would be column 2, not column 0 or 1, as your proxy hides these. Why do you expect this method to be called for all cells, even the ones you’re not showing?

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 2, 2012

Andre Andre
Area 51 Engineer
6031 posts

umen242 wrote:
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?

I fail to understand why you’d need to modify the model from your filterAcceptsColumn() implementation.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

April 2, 2012

umen242 umen242
Ant Farmer
326 posts

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

Page  
1

  ‹‹ How to force update QFileSystemModel ?      QScrollArea issue ››

You must log in to post a reply. Not a member yet? Register here!