我有一个小的 GUI,其中有一个
QTableView
和一个 QPuchButton
。
QPushButton
的功能主要是动态的,这意味着点击按钮时需要发生的操作如下:
QAbstractTableModel
QTableView
上ui
问题是数据库打不开,因此,没有文件临时存储在
NewFile.db
中,我无法读取/显示QTableView
中的数据
void writeTemporaryFilesOnDatabase::on_addMsgBtn_clicked()
{
// 1 - Create a database file dynamically and store the file.db in the following document folder as an example
QString path("/home/to/Documents/");
QDir dir; // Initialize to the desired dir if 'path' is relative
// We create the directory if needed
if (!dir.exists(path))
dir.mkpath(path); // You can check the success if needed
QFile file(path + "NewFile.db");
file.open(QIODevice::WriteOnly); // Or QIODevice::ReadWrite
// 2 - Create a table in runtime mode and fill out with temporary data
QList<QString> contactNames;
QList<QString> contactPhoneNums;
contactNames.append("Thomas");
contactNames.append("Richard");
contactNames.append("Harrison");
contactPhoneNums.append("123");
contactPhoneNums.append("222");
contactPhoneNums.append("333");
// 3 - Create a database in runtime mode and fill out with temporary data just created
QSqlDatabase *database = new QSqlDatabase();
database->QSqlDatabase::addDatabase("QSQLITE","/home/to/Documents/NewFile.db");
database->open();
if (database->open()) {
qDebug() << __LINE__ << QString("Open Db!");
} else {
qDebug() << __LINE__ << QString("Db not opening!");
}
// Create Phone Book with subclassed QAbstractTableModel:
TestModel *PhoneBookModel = new TestModel(this);
// Populate model with data:
PhoneBookModel->populateData(contactNames,contactPhoneNums);
// Connect model to table view:
QSqlTableModel *tableModel = nullptr;
QTableView *tView = new QTableView;
tView->setModel(PhoneBookModel);
// Make table header visible and display table:
tView->horizontalHeader()->setVisible(true); // <-- This run correctly and appears as soon as I launch the application (I did it for debugging reasons and the table is correctly created)
tView->show();
if (database->open()) {
qDebug() << __LINE__ << QString("Open Db or not?????");
QSqlQuery query;
query.exec("DROP TABLE items");
query.exec(
"CREATE TABLE items ("
"Name TEXT NOT NULL,"
"Phone INTEGER NOT NULL ''"
")"
);
query.prepare("INSERT INTO items (Name, Phone) VALUES (?,?)");
query.addBindValue(newItem->name());
query.addBindValue(newItem->phone().toInt());
tableModel = new QSqlTableModel(this, *database);
tableModel->setTable("items");
tableModel->setEditStrategy(QSqlTableModel::OnFieldChange);
tableModel->select();
tView->setModel(PhoneBookModel);
// Read the data created in the `TestModel *PhoneBookModel = new TestModel(this);`
if( file.open( QIODevice::WriteOnly ) )
{
QTextStream ts( &file );
QStringList strList;
for (int i=0; i<PhoneBookModel->rowCount(); i++)
{
strList.clear();
for (int j=0; j<PhoneBookModel->columnCount(); j++)
strList << PhoneBookModel->data(PhoneBookModel->index(i,j)).toString();
ts << strList.join(" ") + "\n";
}
ts << &strList;
file.close();
}
} else {
qDebug() << __FUNCTION__ << QString("Error!");
return;
}
// 4 - LOOP THROUGH THE HEADERS OF THAT FILE AND SHOW THE TABLE
for(int i=0; i< ui->tableWidget->rowCount(); i++)
{
QString viewFieldName = ui->tableView->item(i, 0)->text();
mModel->setHeaderData(i, Qt::Horizontal, viewFieldName);
mModel->submitAll();
}
}
具体来说,
Qt Objects QAbstractTableModel
(在此示例中分类)和QTableView
主要用于练习如何使用QAbstractTableModel
,但我不确定我在这里做错了什么。任何帮助将不胜感激。
如果您想知道我是如何将
QAbstractTableModel
分类的,请参见下文:
class TestModel : public QAbstractTableModel
{
Q_OBJECT
public:
TestModel(QObject *parent = 0);
void populateData(const QList<QString> &contactName,const QList<QString> &contactPhone);
int rowCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
int columnCount(const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const Q_DECL_OVERRIDE;
bool addItem(itemExample *newItem);
private:
QList<QString> tm_contact_name;
QList<QString> tm_contact_phone;
QString mError;
};