1
votes

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.

2

2 Answers

1
votes

ok let's try with this. It works to me:

//pro file:
QT       += core gui sql

TARGET = test1
TEMPLATE = app


SOURCES += main.cpp\
           widget.cpp \
           database.cpp

HEADERS  += widget.h \
            database.h

FORMS    += widget.ui

//database.h:
#ifndef DATABASE_H
#define DATABASE_H

#include <QSqlDatabase>

class Database
{
public:
    Database();
    ~Database();
    QSqlDatabase db;
    bool connection();
    void createTables();
};

#endif // DATABASE_H

//database.cpp:
#include "database.h"
#include <QtGui>
#include <QSqlQuery>

Database::Database()
{
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName(":memory:");
}

Database::~Database()
{
    db.close();
}

bool Database::connection()
{
    if (!db.open()) {
        QMessageBox::critical(0, qApp->tr("Cannot open database"),
        qApp->tr("Unable to establish a database connection.\n"
                 "This example needs SQLite support. Please read "
                 "the Qt SQL driver documentation for information how "
                 "to build it.\n\n"
                 "Click Cancel to exit."), QMessageBox::Ok);
        return false;
    }
    return true;
}

void Database::createTables()
{
    QSqlQuery q;
    q.exec("CREATE TABLE test(id integer primary key,name varchar(20))");

    q.exec("INSERT INTO test(name) VALUES('foo')");
    q.exec("INSERT INTO test(name) VALUES('fie')");
    q.exec("INSERT INTO test(name) VALUES('bar')");
}

//widget.h:
#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
class QSqlQueryModel;
class Database;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    QSqlQueryModel *model;
    Database *m_db;
    Ui::Widget *ui;
};

#endif // WIDGET_H

//widget.cpp:
#include "widget.h"
#include "ui_widget.h"
#include "database.h"
#include <QSqlQueryModel>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    m_db = new Database();
    m_db->connection();
    m_db->createTables();
    model = new QSqlQueryModel();
    model->setQuery("SELECT * from test");
    ui->tableView->setModel(model);
}

Widget::~Widget()
{
    delete ui;
}

//widget.ui:
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>274</width>
    <height>210</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout">
   <item>
    <widget class="QTableView" name="tableView"/>
   </item>
  </layout>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>
0
votes

i guess your mistake is close db connection. Db connection must be closed on application closure