QTableWidget and SQL (dynamic) not working
-
I can recommend reading these two articles to not only understand the model/view concept but also how Qt implements it:
As @SGaist pointed out there's an existing model that allows you to display SQL query results in a
QTableView
. The model is calledQSqlQueryModel
. You'd basically use aQTableView
instead of aQTableWidget
and useQTableView::setModel()
to set theQSqlQueryModel
as the model for the table view. -
@Joel-Bodenmann I tried to use QTableView but actually it would break a lot of my code so unless it's completely necessary I won't use it. I'm focusing on trying to solve my current issue.
-
What would break ?
In any case, I don't see you creating new rows. The incrementation of
i
doesn't mean your QTableWidget will have an additional row unless you did it before your while loop. -
@cxam
QTableWidget
actually inherits fromQTableView
so it shouldn't break anything in terms of the GUI. In terms of the data handling: You just outsource that to the model. As there's an existing model that is designed specifically for handling SQL queries I can imagine that the only thing it would do is drastically simplify your life.
TheQTableWidget
really is just aQTableView
with an internal model. That model is a generic model. Using theQTableView
would allow you to use the SQL query specificQSqlQueryModel
.@SGaist
QTableWidget::setItem()
actually creates the row internally. -
@Joel-Bodenmann Hmm I see. I'll check it out then, thanks for your help.
-
@Joel-Bodenmann No it doesn't. First thing done, it checks whether the row, column combination returns a valid index if not then it returns immediately.
-
-
@SGaist Indeed, you're right When I set the properties for my table I set that the number of rows was "1" so I have to create an "int" variable and then say that the number of rows is that variable starting from 1 and then increment that number on the loop.