Important: Please read the Qt Code of Conduct -

QTableView displays column heading incorrectly when displaying results of a SQL query?

  • Here is some reconstructed code (ie not independantly tested):

    @QSqlQueryModel *model = new QSqlQueryModel;
    model->setQuery("SELECT type, COUNT(titles.title) FROM titles GROUP BY type");
    QTableView view = new QTableView;

    The code queries a table "titles" which contains a list of book titles and their types (business, cooking, sport, etc) plus other stuff.

    The column headings display correctly apart from that for
    which displays as

    However if instead I have @COUNT(title)@ the column heading displays correctly, ie as

    Possibly related to this problem: "titles.type" displays as "type".

    This all becomes a big issue when attempting to correctly display the headings for the results of a join.

    Thinking about it, this may be an HTML related problem, ie the view thinks it should be parsing HTML in the heading.

  • Have you tried modifying your query to us an AS clause? ie something like this:

    @model->setQuery("SELECT type, COUNT(titles.title) AS 'Title Count' FROM titles GROUP BY type");@

  • I agree with ZapB (Sean). Did you check what the column title for that query is if you run it through your DB directly? What DB are you using?

    AFAIK, most Qt SQL drivers won't just make up names for columns themselves. Instead, they get them from the database engine they interact with. There may be a bug in that interaction, of course, but you should try to determine the source of that problem.

  • Using

    @model->setQuery("SELECT type, COUNT(titles.title) AS 'Title Count' FROM titles GROUP BY type");@

    displays the correct heading. However surely both options should work?

    I'm using SQLite from Qt4.7.2

  • It would take more debugging & analysis to see where the problem lies. I would not automatically conclude the problem is with Qt, though it is possible that the SQL driver for SQLite is at fault, of course.

  • I've raised fault report "QTBUG-18095":

    Edit: fixed link; Andre

Log in to reply