Views displays blank (invisible) rows
Hi, question from newbie.
Does anybody meets next problem:
I created QSqlQueryModel (named model).
this->model = new QSqlQueryModel(); model->setQuery("SELECT firnaz FROM accfir");
On form also created QListView and QTableView (model based).
With ui->tableView->setModel(model); and ui->listView->setModel(model); linked same model with two views.
All works well, no errors. Statement SELECT reads 4 rows (string data). But on both views I see four blank (invisible) rows ! With mouse hovering over rows in both views I can see that rows are there but can't see its text (values). When I click on some row I can select it, again can't see row text.
qDebug() <<(model->rowCount()); shows me that model have 4 rows.
qDebug() <<model->data(model->index(1, 0)); shows me value for second row in debug window, but can't see that value in both views !!
Please answer anyone who experienced this problem. I can't upload screenshot or I do not know in this forum.
Thanks in advance,
Hi and welcome to devnet,
What do you have in that table ?
In MySql table named accfir I have several fields, but I select only one field, firnaz char(40). First I started only with QlistView widget and that's reason why I read only one field. When I got rows with invisible values, added on form new QTableView, linking it with same model. Only difference is that in tableView I see header row ("FIRNAZ") and on left side I see row numbers 1,2,3,4. Rows exists but views displays no text, as I said both in listView and tableView !? Using QDebug() for that rows I can see correct values. It looks like display problem.
What happens if you are using a QSqlTableModel ?
Using a QSqlTableModel makes no progress. Different is that in tableView now I can see headers of all fileds in my table. Rows are there but displays no data. Using:
I can see that again model have 4 records, which is correct. Also in debug window I can display values for model rows.
Meanwhile I found this link http://www.qtcentre.org/threads/61840-QTableView-displays-column-headers-but-no-data . It describes same situation I have. I applied morphing as suggested but no lack. Still cant see data.
Did not mentioned that I use Qt 5.5.0, Qt Creator 3.4.2 (open source), 32 bit. Windows 8 Pro.
New: I installed Qt 5.5.0 and MySql 5.6.26 on another 32 bit machine. Copied project, build ... nothing changed, still can't see data in view.
Hi, problem solved !
As a newbie I try to learn from examples on Internet.
I found video example how to use QSqlQueryModel and QlistView. In that example seqence of code is as following:
connOpen(); // to open connection to MySql database
.... statements to make model and link it to ui->listView (or tableView)
connClose(); // last statement, to close connection
After building and starting project all works without issuing any error, but with empty rows as I described at the start of these posts. In video example I can see that all works OK and row values are displayed in view.
Today I excluded (commented) last statement connClose();. After that all works OK !! Can see data in window. In all model and view variations I tried in code. As soon as I again use last statement to close connection, problem appears again.
So, I thought that making model and linking it to view garanties that program reads data in model that I can use after connection is closed. Why connection must stay to be able to see data in view ? Are they not in model ? Or model only links view and database ?
It appears that I missunderstud example on Internet. I must carefully read how to use Sql databases in Qt. Please comment if you think that it can help me.
Thanks SGaist for your help.
The database is only queried when needed, so if you close the connection before anything is requested then you might get only partial data. e.g. the model knows that your table contains 10'000 lines but it won't fetch them all at start, it would be bad performance and memory wise.
What why you generally have is the connection setup at your program start then close when it ends. It doesn't mean that you have to always do it like that. Depending on the environment and constraints you might have to do the connection/query database/deconnection each time