Filling a TableModel doesn't work !



  • 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);
    }
    
    

  • Lifetime Qt Champion

    Hi,

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



  • @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);
    }
    
    

  • Lifetime Qt Champion

    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.



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


  • Lifetime Qt Champion

    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 :)



  • Done ! Thanks again


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.