Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt Creator and other tools
  4. Qt ListView not displaying data

Qt ListView not displaying data

Scheduled Pinned Locked Moved Solved Qt Creator and other tools
5 Posts 3 Posters 308 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Faris Elbaz
    wrote on 23 Apr 2024, 21:10 last edited by
    #1
    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

    R 1 Reply Last reply 23 Apr 2024, 21:17
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 Apr 2024, 21:14 last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      F 1 Reply Last reply 23 Apr 2024, 21:21
      0
      • F Faris Elbaz
        23 Apr 2024, 21:10
        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

        R Offline
        R Offline
        Ronel_qtmaster
        wrote on 23 Apr 2024, 21:17 last edited by Ronel_qtmaster
        #3

        @Faris-Elbaz IF YOU want to display data from a database use QSqlTableModel
        a use case

        QSqlTableModel *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();
        
        1 Reply Last reply
        0
        • S SGaist
          23 Apr 2024, 21:14

          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 ?

          F Offline
          F Offline
          Faris Elbaz
          wrote on 23 Apr 2024, 21:21 last edited by
          #4

          @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 Screenshot 2024-04-23 at 11.20.09 PM.png
          See no Data is showing at all even though through my debugging im 100% sure its been inserted into the listView

          S 1 Reply Last reply 23 Apr 2024, 21:23
          0
          • F Faris Elbaz
            23 Apr 2024, 21:21

            @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 Screenshot 2024-04-23 at 11.20.09 PM.png
            See no Data is showing at all even though through my debugging im 100% sure its been inserted into the listView

            S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 23 Apr 2024, 21:23 last edited by
            #5

            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.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • F Faris Elbaz has marked this topic as solved on 24 Apr 2024, 09:28

            1/5

            23 Apr 2024, 21:10

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved