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. Setting Header Data with QSqlQueryModel does not work
Forum Updated to NodeBB v4.3 + New Features

Setting Header Data with QSqlQueryModel does not work

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 2.8k 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.
  • L Offline
    L Offline
    lycis
    wrote on last edited by
    #1

    Hi,

    I'm working with a "QSqlQueryModel ":http://qt-project.org/doc/qt-4.8/qsqlquerymodel.htmlto display data from a SQL query. Ususally the column header is taken from the column names returned by the query. As I'd like them to be always the same regardless of the underlying query I tried setting the column captions by using setHeaderData(...) but that does not work out and the captions are taken from the query.

    Here's what I'm currently doing:
    @
    void RecipeListWidget::setModel(QAbstractItemModel *model)
    {
    qDebug() << "RecipeLisWidget::setModel(" << model << ")" << endl;
    _recipeList.setModel(model);
    model->setHeaderData(0, Qt::Horizontal, tr("Name"));
    model->setHeaderData(1, Qt::Horizontal, tr("Score"));
    return;
    }
    @

    The could is designed to run with any kind of model so I'm using "QAbstractItemModel ":http://qt-project.org/doc/QAbstractItemModel.htmlin the function signature, but a QSqlQueryModel is passed as argument.

    Thanks for any advice on that one :)

    1 Reply Last reply
    0
    • D Offline
      D Offline
      DerManu
      wrote on last edited by
      #2

      Make a QAbstractProxyModel or QSortFilterProxyModel which only reimplements headerData() to return the header strings you want. All other stuff shall be forwarded (QSortFilterProxyModel already does the forwarding for you. Just disable the filtering by reimplementing acceptsRow/Column to always return true).

      //EDIT: Oh yes, and then use the proxy model between the model and the view with setSourceModel ;). If you want to tie the proxy model to the view, you can of course integrate the proxy model into the view and reimplement the setModel method of your view to call setSourceModel on the internal proxy, and then call the base class setModel function with the proxy as parameter.

      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