I'm implementing a QtableView with the data from a QSqlQueryModeL.
What happens is, if I select a item in the view, the item after a second disappears and the items in the other rows disappear as well.
my code: ui->medicstableView->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded); ui->medicstableView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); ui->medicstableView->setStyleSheet(QString::fromUtf8("background-color: rgb(227, 226, 226);")); //ui->medicstableView->setVisible(true); //ui->medicstableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
doctors_table = new QSqlQueryModel(this);
QSqlDatabase mydb;
QString Squery;
QString conn = mydb.connectionName();
if (conn.isEmpty()) {
mydb = QSqlDatabase::addDatabase("QMYSQL");
}
else
{
mydb.close();
QSqlDatabase::removeDatabase(conn);
mydb = QSqlDatabase::addDatabase("QMYSQL");
}
mydb.setHostName(myhost);
mydb.setUserName(myuser);
mydb.setPassword(mypass);
mydb.setDatabaseName(mybase);
try
{
if(mydb.open()==true)
{
Squery=QString("SELECT id_medico,medico FROM Registos_Medicos");
doctors_table->setQuery(Squery);
doctors_table->setHeaderData(0, Qt::Horizontal, QObject::tr("ID"));
doctors_table->setHeaderData(1, Qt::Horizontal, QObject::tr("Nome do médico"));
ui->medicstableView->setModel(doctors_table);
mydb.close();
//ui->statusbar->addAction();
ui->medicstableView->resizeColumnsToContents ();
ui->medicstableView->resizeRowsToContents ();
ui->medicstableView->setColumnWidth(1,255);
ui->medicstableView->show();
The QTableView is created in the Qt Designer with the name "medicstableView"
Thanks in advance.