我正在使用来自QSqlQueryModeL的数据实现QtableView。
如果我在视图中选择一个项目,那么该项目会在一秒钟后消失,而其他行中的项目也会消失。
我的代码: UI-> medicstableView-> setHorizontalScrollBarPolicy(QT :: ScrollBarAsNeeded); UI-> medicstableView-> setVerticalScrollBarPolicy(QT :: ScrollBarAsNeeded); ui-> medicstableView-> setStyleSheet(QString :: fromUtf8(“background-color:rgb(227,226,226);”)); // UI-> medicstableView->调用setVisible(真); // 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 Designer中创建,名称为“medicstableView”
提前致谢。
答案 0 :(得分: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>
答案 1 :(得分:0)
我猜你的错误是关闭数据库连接。必须在应用程序关闭时关闭Db连接