1

我正在使用来自 QSqlQueryModeL 的数据实现 QtableView。

发生的情况是,如果我在视图中选择一个项目,一秒钟后该项目消失,其他行中的项目也消失。

我的代码: ui->medicstableView->setHorizo​​ntalScrollBarPolicy(Qt::ScrollBarAsNeeded); ui->medicstableView->setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); ui->medicstableView->setStyleSheet(QString::fromUtf8("背景色: 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();

QTableView 是在 Qt 设计器中创建的,名称为“medicstableView”

提前致谢。

4

2 回答 2

1

好的,让我们试试这个。它对我有用:

//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>
于 2012-10-10T17:16:42.837 回答
0

我猜你的错误是关闭数据库连接。数据库连接必须在应用程序关闭时关闭

于 2012-10-10T11:55:38.447 回答