Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Filling a TableModel doesn't work !
Qt 6.11 is out! See what's new in the release blog

Filling a TableModel doesn't work !

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 3.4k Views 2 Watching
  • 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.
  • T Offline
    T Offline
    theo_ld
    wrote on last edited by theo_ld
    #1

    Hello everyone,

    I've created the addEntry function to fill in a TableModel to display in my ipWidget. The problem is that nothing is displayed in my main Window. Can you guys help me out ? I've run my code in debug mode and both of the input QStrings are ok.

    IpWidget.h

    #ifndef IPWIDGET_H
    #define IPWIDGET_H
    
    #include "TableModel.h"
    
    #include <QItemSelection>
    #include <QTabWidget>
    #include <vector>
    
    class QSortFilterProxyModel;
    class QItemSelectionModel;
    
    class IpWidget : public QTabWidget
    {
        Q_OBJECT
    
    public:
        IpWidget(QWidget *parent = 0);
        void addEntry(QString name, QString address);
        void updateUserList(std::vector<std::string> const &requestVector);
        void call();
        void hangup();
    
    signals:
        void selectionChanged (const QItemSelection &selected);
    
    private:
        void setupTabs();
    
        TableModel *table;
        QSortFilterProxyModel *proxyModel;
    };
    
    #endif // IPWIDGET_H
    
    

    Part of IpWidget.cpp

    void IpWidget::addEntry(QString name, QString ip)
    {
        QList<QPair<QString, QString>> list = table->getList();
        QPair<QString, QString> pair(name, ip);
    
        table->insertRows(0, 1, QModelIndex());
    
        QModelIndex index = table->index(0, 0, QModelIndex());
        table->setData(index, name, Qt::EditRole);
    
        index = table->index(0, 1, QModelIndex());
        table->setData(index, ip, Qt::EditRole);
    }
    

    MainWindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include "IpWidget.h"
    #include "CmdHandler.h"
    
    #include <QMainWindow>
    #include <memory>
    
    #define UPDT_USR_LIST "101 adresse Ip"
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow();
        void updateUsers(std::string const &);
    
    private slots:
        void updateActions(const QItemSelection &selection);
    
    private:
        void createMenus();
    
        IpWidget *ipWidget;
        QMenu *fileMenu;
        QMenu *toolMenu;
        QAction *refreshAct;
        QAction *exitAct;
        QAction *callAct;
        QAction *hangupAct;
        std::shared_ptr<CmdHandler> ch;
    };
    
    #endif // MAINWINDOW_H
    
    

    MainWindow.cpp

    #include "MainWindow.h"
    
    #include <QAction>
    #include <QFileDialog>
    #include <QMenuBar>
    
    MainWindow::MainWindow()
    {
        ipWidget = new IpWidget;
        ch = std::make_shared<CmdHandler>();
        setCentralWidget(ipWidget);
        createMenus();
        setWindowTitle(tr("Babel"));
        updateUsers(std::string("101 adresse Ip\n127.x.x.x antonio\n127.x.x.x john\n"));
    }
    
    void MainWindow::createMenus()
    {
        fileMenu = menuBar()->addMenu(tr("&File"));
    
        refreshAct = new QAction(tr("&Refresh"), this);
        fileMenu->addAction(refreshAct);
        //connect(refreshAct, &QAction::triggered, this, &MainWindow::updateUsers);
    
        fileMenu->addSeparator();
    
        exitAct = new QAction(tr("E&xit"), this);
        fileMenu->addAction(exitAct);
        connect(exitAct, &QAction::triggered, this, &QWidget::close);
    
        toolMenu = menuBar()->addMenu(tr("&Tools"));
    
        callAct = new QAction(tr("&Call contact"), this);
        toolMenu->addAction(callAct);
        connect(callAct, &QAction::triggered, ipWidget, &IpWidget::call);
    
        hangupAct = new QAction(tr("&Hangup contact"), this);
        hangupAct->setEnabled(false);
        toolMenu->addAction(hangupAct);
        connect(hangupAct, &QAction::triggered, ipWidget, &IpWidget::hangup);
    
        connect(ipWidget, &IpWidget::selectionChanged, this, &MainWindow::updateActions);
    }
    
    void MainWindow::updateActions(const QItemSelection &selection)
    {
        QModelIndexList indexes = selection.indexes();
    
        if (!indexes.isEmpty()) {
            callAct->setEnabled(true);
            hangupAct->setEnabled(true);
        } else {
            callAct->setEnabled(false);
            hangupAct->setEnabled(false);
        }
    }
    void MainWindow::updateUsers(std::string const &request)
    {
        std::vector<std::string> users = ch->splitRequest(request);
    
        if (users.front() == UPDT_USR_LIST)
          ipWidget->updateUserList(users);
    }
    
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      From the code you posted it's impossible to know whether you set your model to a QTableView, is it the case ?

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

      T 1 Reply Last reply
      1
      • SGaistS SGaist

        Hi,

        From the code you posted it's impossible to know whether you set your model to a QTableView, is it the case ?

        T Offline
        T Offline
        theo_ld
        wrote on last edited by
        #3

        @SGaist No I don't do it, how can i integrate it into my current code ?

        Here is full IpWidget

        #include "IpWidget.h"
        
        #include <QtWidgets>
        
        IpWidget::IpWidget(QWidget *parent)
            : QTabWidget(parent)
        {
            table = new TableModel(this);
        
            setupTabs();
        }
        
        void IpWidget::setupTabs()
        {
            QStringList groups;
            groups << "ABC" << "DEF" << "GHI" << "JKL" << "MNO" << "PQR" << "STU" << "VW" << "XYZ";
        
            for (int i = 0; i < groups.size(); ++i) {
                QString str = groups.at(i);
                QString regExp = QString("^[%1].*").arg(str);
        
                proxyModel = new QSortFilterProxyModel(this);
                proxyModel->setSourceModel(table);
                proxyModel->setFilterRegExp(QRegExp(regExp, Qt::CaseInsensitive));
                proxyModel->setFilterKeyColumn(0);
        
                QTableView *tableView = new QTableView;
                tableView->setModel(proxyModel);
        
                tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
                tableView->horizontalHeader()->setStretchLastSection(true);
                tableView->verticalHeader()->hide();
                tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
                tableView->setSelectionMode(QAbstractItemView::SingleSelection);
        
                tableView->setSortingEnabled(true);
        
                connect(tableView->selectionModel(),
                    &QItemSelectionModel::selectionChanged,
                    this, &IpWidget::selectionChanged);
        
                addTab(tableView, str);
            }
        }
        
        void IpWidget::addEntry(QString name, QString ip)
        {
            QList<QPair<QString, QString>> list = table->getList();
            QPair<QString, QString> pair(name, ip);
        
            table->insertRows(0, 1, QModelIndex());
        
            QModelIndex index = table->index(0, 0, QModelIndex());
            table->setData(index, name, Qt::EditRole);
        
            index = table->index(0, 1, QModelIndex());
            table->setData(index, ip, Qt::EditRole);
        }
        
        void IpWidget::updateUserList(std::vector<std::string> const &requestVector)
        {
            size_t pos;
            std::string ip, name;
        
            if (table->rowCount(QModelIndex()))
                table->removeRows(0, table->rowCount(QModelIndex()));
        
            for (size_t i = 1; i < requestVector.size(); ++i)
            {
                pos = requestVector[i].find_first_of(' ');
        
                name = requestVector[i].substr(0, pos);
                ip = requestVector[i].substr(pos + 1);
        
                addEntry(QString::fromStdString(name), QString::fromStdString(ip));
            }
        }
        
        void IpWidget::call()
        {
            QTableView *temp = static_cast<QTableView*>(currentWidget());
            QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());
            QItemSelectionModel *selectionModel = temp->selectionModel();
        
            QModelIndexList indexes = selectionModel->selectedRows();
            QString ip;
            int row = -1;
        
            foreach (QModelIndex index, indexes) {
                row = proxy->mapToSource(index).row();
        
                QModelIndex ipIndex = table->index(row, 1, QModelIndex());
                QVariant varAddr = table->data(ipIndex, Qt::DisplayRole);
                ip = varAddr.toString();
            }
            // TODO NetworkClass::call(ip);
        }
        
        void IpWidget::hangup()
        {
            QTableView *temp = static_cast<QTableView*>(currentWidget());
            QSortFilterProxyModel *proxy = static_cast<QSortFilterProxyModel*>(temp->model());
            QItemSelectionModel *selectionModel = temp->selectionModel();
        
            QModelIndexList indexes = selectionModel->selectedRows();
            QString ip;
            int row = -1;
        
            foreach (QModelIndex index, indexes) {
                row = proxy->mapToSource(index).row();
        
                QModelIndex ipIndex = table->index(row, 1, QModelIndex());
                QVariant varAddr = table->data(ipIndex, Qt::DisplayRole);
                ip = varAddr.toString();
            }
            // TODO NetworkClass::hangup(ip);
        }
        
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You are setting a filter with regular expression, did you check that it's not that that is just filtering away everything ?

          On a side note, you should use QRegularExpression. QRegExp is deprecated.

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

          T 1 Reply Last reply
          1
          • SGaistS SGaist

            You are setting a filter with regular expression, did you check that it's not that that is just filtering away everything ?

            On a side note, you should use QRegularExpression. QRegExp is deprecated.

            T Offline
            T Offline
            theo_ld
            wrote on last edited by
            #5

            @SGaist I took this example from Qt Example Book, I'm an idiot, it works fine, thank you so much !

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              You're welcome !

              Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

              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
              • T Offline
                T Offline
                theo_ld
                wrote on last edited by
                #7

                Done ! Thanks again

                1 Reply Last reply
                0

                • Login

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