Qt ListView not displaying data
-
maincode::maincode(SQLHDBC hdbc, QWidget *parent) : QMainWindow(parent), ui(new Ui::maincode) // Change Ui::maincode to Ui::maincode { ui->setupUi(this); qDebug() << "UI setup complete"; // Create a QListView and connect its clicked signal to a slot listView = new QListView(this); model = new QStandardItemModel(this); listView->setModel(model); connect(listView, &QListView::clicked, this, &maincode::onItemClicked); qDebug() << "ListView created and connected"; // Attempt to open the Function connection if (!db.connOpen()) { QMessageBox::critical(this, "Error", "Failed to connect to the Function"); return; } qDebug() << "Database connection opened successfully"; // Retrieve table names from the database QMap<QString, QString> tableNamesMap = db.getTableNames(); qDebug() << "Table Names Map: " << tableNamesMap; // Extract table names from the map QStringList tableNames = tableNamesMap.keys(); qDebug() << "Table Names List: " << tableNames; // Add table names to the model foreach (const QString &tableName, tableNames) { model->appendRow(new QStandardItem(tableName)); } qDebug() << "Items added to model"; for (int row = 0; row < model->rowCount(); ++row) { QModelIndex index = model->index(row, 0); qDebug() << "Item:" << model->data(index).toString(); } // Set up the default table model tableview = new QTableView(this); loadTableData(); qDebug() << "Table view set up"; }
QMap<QString, QString> Function::getTableNames() { QMap<QString, QString> tableNames; SQLHSTMT hstmt; SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); // SQLTablesA query to retrieve only user-created tables SQLCHAR query[] = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'"; if (SQLExecDirectA(hstmt, query, SQL_NTS) != SQL_SUCCESS) { SQLFreeHandle(SQL_HANDLE_STMT, hstmt); return tableNames; } SQLCHAR tableName[256]; SQLLEN len; while (SQLFetch(hstmt) == SQL_SUCCESS) { SQLGetData(hstmt, 1, SQL_C_CHAR, tableName, sizeof(tableName), &len); QString name = QString::fromUtf8(reinterpret_cast<const char*>(tableName)); qDebug() << "Table Name: " << name; tableNames[name] = ""; } SQLFreeHandle(SQL_HANDLE_STMT, hstmt); return tableNames; }
The debugging lines of code i put show that everything is working correctly but my ListView is not displaying anything, and for some reason a combobox that had nothing to do with any of the code no longer displays a drop down menu
-
Hi,
You don't seem to show the QListView. Are you sure that you are not seeing one that you put there using Designer and you are not using it ?
-
@Faris-Elbaz IF YOU want to display data from a database use QSqlTableModel
a use caseQSqlTableModel *model = new QSqlTableModel(parentObject, database);
model->setTable("employee");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select();
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Salary"));QTableView *view = new QTableView; view->setModel(model); view->hideColumn(0); view->show();
-
@SGaist Hi, im using Qt creator so by default it should be there in the UI file, when i see i cant see it im referring to the data within the ListView not the ListView widget itself
See no Data is showing at all even though through my debugging im 100% sure its been inserted into the listView -
Well, that's exactly what I wrote about, you create your own QListView that you are not showing rather than using the one from your Ui object hence it stays empty.
-