gpt4 book ai didi

c++ - QTabWidget里面的QTableView不显示数据

转载 作者:行者123 更新时间:2023-11-30 05:43:38 25 4
gpt4 key购买 nike

我有一个 QTabWidget,每个选项卡都包含一个 QTableView。tableviews 使用 QTableModels 从 sqlite 数据库中读取数据。

模型被保存为成员变量,所以我不认为这是一个范围问题(这是我通过搜索问题发现的最常见的问题)。

其他数据库操作有效:我尝试在代码中填充/创建表,并且这些操作正确执行。

类定义如下:

#include <QMainWindow>
#include <QtSql>
#include <QString>

namespace Ui {
class MainWindow;
}

class AlisnagMainWindow : public QMainWindow
{
Q_OBJECT

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

protected:
bool createDB(const QString& filename);
bool loadDB(const QString& filename);

protected slots:
void newFile();
void openFile();
void addRow();

private:
void initModels();
Ui::MainWindow *ui;
QSqlDatabase db;
QSqlRelationalTableModel *loans_model;
QSqlTableModel *people_model;
QSqlRelationalTableModel *items_model;
};

这是构造函数:

AlisnagMainWindow::AlisnagMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
loans_model( new QSqlRelationalTableModel(this) ),
people_model( new QSqlTableModel(this) ),
items_model( new QSqlRelationalTableModel(this) )
{
ui->setupUi(this);

db = QSqlDatabase::addDatabase("QSQLITE");

connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile()));
connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openFile()));
}

这是我初始化模型和 View 的方式:

void AlisnagMainWindow::initModels(){
people_model->setTable("people");
people_model->setEditStrategy(QSqlTableModel::OnRowChange);
people_model->select();
people_model->setHeaderData(0, Qt::Horizontal, tr("Name"));
people_model->setHeaderData(1, Qt::Horizontal, tr("Surname"));
people_model->setHeaderData(2, Qt::Horizontal, tr("Phone"));
people_model->setHeaderData(3, Qt::Horizontal, tr("Address"));

items_model->setTable("items");
items_model->setEditStrategy(QSqlTableModel::OnRowChange);
items_model->setRelation(3, QSqlRelation("categories","id","name"));
items_model->select();

loans_model->setTable("loans");
loans_model->setEditStrategy(QSqlTableModel::OnRowChange);
loans_model->setRelation(0, QSqlRelation("items","id","name"));
loans_model->setRelation(1, QSqlRelation("people","id","name"));
loans_model->select();

ui->peopleView->setModel(people_model);
ui->peopleView->hideColumn(0);

ui->itemsView->setModel(items_model);
ui->itemsView->hideColumn(0);

ui->loansView->setModel(loans_model);
ui->loansView->hideColumn(0);

}

数据库加载如下:

bool AlisnagMainWindow::loadDB(const QString &filename)
{
if(db.isOpen())
db.close();
db.setDatabaseName(filename);

if( !db.open() ){
QMessageBox::critical(this, tr("Error"),
tr("Could not open db %1").arg(filename),
QMessageBox::Ok);
return false;
}

ui->statusBar->showMessage("DB successfully opened!");
return true;

}

然后这就是所有组合的方式:

void AlisnagMainWindow::openFile()
{
QString filename = QFileDialog::getOpenFileName(this, tr("Choose DB file"),
".", "*.db");
loadDB(filename);

initModels();
}

我正在打开的文件已从外部填充。当我运行代码时,数据和标题都没有显示。有什么见解吗?

提前致谢。

编辑:根据要求添加了一些上下文

最佳答案

作为Greenflow指出,问题出在构造函数中,特别是在初始化顺序中。
构造函数修改如下

AlisnagMainWindow::AlisnagMainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
db ( QSqlDatabase::addDatabase("QSQLITE")),
loans_model( new QSqlRelationalTableModel(this, db) ),
people_model( new QSqlTableModel(this, db) ),
items_model( new QSqlRelationalTableModel(this, db) )
{
ui->setupUi(this);
connect(ui->actionNew, SIGNAL(triggered()), this, SLOT(newFile()));
connect(ui->actionOpen, SIGNAL(triggered()), this, SLOT(openFile()));
}

关于c++ - QTabWidget里面的QTableView不显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30126464/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com