<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Filling a TableModel doesn&#x27;t work !]]></title><description><![CDATA[<p dir="auto">Hello everyone,</p>
<p dir="auto">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.</p>
<p dir="auto"><strong>IpWidget.h</strong></p>
<pre><code>#ifndef IPWIDGET_H
#define IPWIDGET_H

#include "TableModel.h"

#include &lt;QItemSelection&gt;
#include &lt;QTabWidget&gt;
#include &lt;vector&gt;

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&lt;std::string&gt; const &amp;requestVector);
    void call();
    void hangup();

signals:
    void selectionChanged (const QItemSelection &amp;selected);

private:
    void setupTabs();

    TableModel *table;
    QSortFilterProxyModel *proxyModel;
};

#endif // IPWIDGET_H

</code></pre>
<p dir="auto"><strong>Part of IpWidget.cpp</strong></p>
<pre><code>void IpWidget::addEntry(QString name, QString ip)
{
    QList&lt;QPair&lt;QString, QString&gt;&gt; list = table-&gt;getList();
    QPair&lt;QString, QString&gt; pair(name, ip);

    table-&gt;insertRows(0, 1, QModelIndex());

    QModelIndex index = table-&gt;index(0, 0, QModelIndex());
    table-&gt;setData(index, name, Qt::EditRole);

    index = table-&gt;index(0, 1, QModelIndex());
    table-&gt;setData(index, ip, Qt::EditRole);
}
</code></pre>
<p dir="auto"><strong>MainWindow.h</strong></p>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include "IpWidget.h"
#include "CmdHandler.h"

#include &lt;QMainWindow&gt;
#include &lt;memory&gt;

#define UPDT_USR_LIST "101 adresse Ip"

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow();
    void updateUsers(std::string const &amp;);

private slots:
    void updateActions(const QItemSelection &amp;selection);

private:
    void createMenus();

    IpWidget *ipWidget;
    QMenu *fileMenu;
    QMenu *toolMenu;
    QAction *refreshAct;
    QAction *exitAct;
    QAction *callAct;
    QAction *hangupAct;
    std::shared_ptr&lt;CmdHandler&gt; ch;
};

#endif // MAINWINDOW_H

</code></pre>
<p dir="auto"><strong>MainWindow.cpp</strong></p>
<pre><code>#include "MainWindow.h"

#include &lt;QAction&gt;
#include &lt;QFileDialog&gt;
#include &lt;QMenuBar&gt;

MainWindow::MainWindow()
{
    ipWidget = new IpWidget;
    ch = std::make_shared&lt;CmdHandler&gt;();
    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()-&gt;addMenu(tr("&amp;File"));

    refreshAct = new QAction(tr("&amp;Refresh"), this);
    fileMenu-&gt;addAction(refreshAct);
    //connect(refreshAct, &amp;QAction::triggered, this, &amp;MainWindow::updateUsers);

    fileMenu-&gt;addSeparator();

    exitAct = new QAction(tr("E&amp;xit"), this);
    fileMenu-&gt;addAction(exitAct);
    connect(exitAct, &amp;QAction::triggered, this, &amp;QWidget::close);

    toolMenu = menuBar()-&gt;addMenu(tr("&amp;Tools"));

    callAct = new QAction(tr("&amp;Call contact"), this);
    toolMenu-&gt;addAction(callAct);
    connect(callAct, &amp;QAction::triggered, ipWidget, &amp;IpWidget::call);

    hangupAct = new QAction(tr("&amp;Hangup contact"), this);
    hangupAct-&gt;setEnabled(false);
    toolMenu-&gt;addAction(hangupAct);
    connect(hangupAct, &amp;QAction::triggered, ipWidget, &amp;IpWidget::hangup);

    connect(ipWidget, &amp;IpWidget::selectionChanged, this, &amp;MainWindow::updateActions);
}

void MainWindow::updateActions(const QItemSelection &amp;selection)
{
    QModelIndexList indexes = selection.indexes();

    if (!indexes.isEmpty()) {
        callAct-&gt;setEnabled(true);
        hangupAct-&gt;setEnabled(true);
    } else {
        callAct-&gt;setEnabled(false);
        hangupAct-&gt;setEnabled(false);
    }
}
void MainWindow::updateUsers(std::string const &amp;request)
{
    std::vector&lt;std::string&gt; users = ch-&gt;splitRequest(request);

    if (users.front() == UPDT_USR_LIST)
      ipWidget-&gt;updateUserList(users);
}

</code></pre>
]]></description><link>https://forum.qt.io/topic/83852/filling-a-tablemodel-doesn-t-work</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 21:45:46 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/83852.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 03 Oct 2017 16:54:14 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Filling a TableModel doesn&#x27;t work ! on Tue, 03 Oct 2017 21:19:30 GMT]]></title><description><![CDATA[<p dir="auto">Done ! Thanks again</p>
]]></description><link>https://forum.qt.io/post/418648</link><guid isPermaLink="true">https://forum.qt.io/post/418648</guid><dc:creator><![CDATA[theo_ld]]></dc:creator><pubDate>Tue, 03 Oct 2017 21:19:30 GMT</pubDate></item><item><title><![CDATA[Reply to Filling a TableModel doesn&#x27;t work ! on Tue, 03 Oct 2017 21:18:13 GMT]]></title><description><![CDATA[<p dir="auto">You're welcome !</p>
<p dir="auto">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 :)</p>
]]></description><link>https://forum.qt.io/post/418645</link><guid isPermaLink="true">https://forum.qt.io/post/418645</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 03 Oct 2017 21:18:13 GMT</pubDate></item><item><title><![CDATA[Reply to Filling a TableModel doesn&#x27;t work ! on Tue, 03 Oct 2017 21:14:03 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> I took this example from Qt Example Book, I'm an idiot, it works fine, thank you so much !</p>
]]></description><link>https://forum.qt.io/post/418644</link><guid isPermaLink="true">https://forum.qt.io/post/418644</guid><dc:creator><![CDATA[theo_ld]]></dc:creator><pubDate>Tue, 03 Oct 2017 21:14:03 GMT</pubDate></item><item><title><![CDATA[Reply to Filling a TableModel doesn&#x27;t work ! on Tue, 03 Oct 2017 20:59:48 GMT]]></title><description><![CDATA[<p dir="auto">You are setting a filter with regular expression, did you check that it's not that that is just filtering away everything ?</p>
<p dir="auto">On a side note, you should use QRegularExpression. QRegExp is deprecated.</p>
]]></description><link>https://forum.qt.io/post/418640</link><guid isPermaLink="true">https://forum.qt.io/post/418640</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 03 Oct 2017 20:59:48 GMT</pubDate></item><item><title><![CDATA[Reply to Filling a TableModel doesn&#x27;t work ! on Tue, 03 Oct 2017 20:48:53 GMT]]></title><description><![CDATA[<p dir="auto"><a class="plugin-mentions-user plugin-mentions-a" href="/user/sgaist">@<bdi>SGaist</bdi></a> No I don't do it, how can i integrate it into my current code ?</p>
<p dir="auto">Here is full <strong>IpWidget</strong></p>
<pre><code>#include "IpWidget.h"

#include &lt;QtWidgets&gt;

IpWidget::IpWidget(QWidget *parent)
    : QTabWidget(parent)
{
    table = new TableModel(this);

    setupTabs();
}

void IpWidget::setupTabs()
{
    QStringList groups;
    groups &lt;&lt; "ABC" &lt;&lt; "DEF" &lt;&lt; "GHI" &lt;&lt; "JKL" &lt;&lt; "MNO" &lt;&lt; "PQR" &lt;&lt; "STU" &lt;&lt; "VW" &lt;&lt; "XYZ";

    for (int i = 0; i &lt; groups.size(); ++i) {
        QString str = groups.at(i);
        QString regExp = QString("^[%1].*").arg(str);

        proxyModel = new QSortFilterProxyModel(this);
        proxyModel-&gt;setSourceModel(table);
        proxyModel-&gt;setFilterRegExp(QRegExp(regExp, Qt::CaseInsensitive));
        proxyModel-&gt;setFilterKeyColumn(0);

        QTableView *tableView = new QTableView;
        tableView-&gt;setModel(proxyModel);

        tableView-&gt;setSelectionBehavior(QAbstractItemView::SelectRows);
        tableView-&gt;horizontalHeader()-&gt;setStretchLastSection(true);
        tableView-&gt;verticalHeader()-&gt;hide();
        tableView-&gt;setEditTriggers(QAbstractItemView::NoEditTriggers);
        tableView-&gt;setSelectionMode(QAbstractItemView::SingleSelection);

        tableView-&gt;setSortingEnabled(true);

        connect(tableView-&gt;selectionModel(),
            &amp;QItemSelectionModel::selectionChanged,
            this, &amp;IpWidget::selectionChanged);

        addTab(tableView, str);
    }
}

void IpWidget::addEntry(QString name, QString ip)
{
    QList&lt;QPair&lt;QString, QString&gt;&gt; list = table-&gt;getList();
    QPair&lt;QString, QString&gt; pair(name, ip);

    table-&gt;insertRows(0, 1, QModelIndex());

    QModelIndex index = table-&gt;index(0, 0, QModelIndex());
    table-&gt;setData(index, name, Qt::EditRole);

    index = table-&gt;index(0, 1, QModelIndex());
    table-&gt;setData(index, ip, Qt::EditRole);
}

void IpWidget::updateUserList(std::vector&lt;std::string&gt; const &amp;requestVector)
{
    size_t pos;
    std::string ip, name;

    if (table-&gt;rowCount(QModelIndex()))
        table-&gt;removeRows(0, table-&gt;rowCount(QModelIndex()));

    for (size_t i = 1; i &lt; 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&lt;QTableView*&gt;(currentWidget());
    QSortFilterProxyModel *proxy = static_cast&lt;QSortFilterProxyModel*&gt;(temp-&gt;model());
    QItemSelectionModel *selectionModel = temp-&gt;selectionModel();

    QModelIndexList indexes = selectionModel-&gt;selectedRows();
    QString ip;
    int row = -1;

    foreach (QModelIndex index, indexes) {
        row = proxy-&gt;mapToSource(index).row();

        QModelIndex ipIndex = table-&gt;index(row, 1, QModelIndex());
        QVariant varAddr = table-&gt;data(ipIndex, Qt::DisplayRole);
        ip = varAddr.toString();
    }
    // TODO NetworkClass::call(ip);
}

void IpWidget::hangup()
{
    QTableView *temp = static_cast&lt;QTableView*&gt;(currentWidget());
    QSortFilterProxyModel *proxy = static_cast&lt;QSortFilterProxyModel*&gt;(temp-&gt;model());
    QItemSelectionModel *selectionModel = temp-&gt;selectionModel();

    QModelIndexList indexes = selectionModel-&gt;selectedRows();
    QString ip;
    int row = -1;

    foreach (QModelIndex index, indexes) {
        row = proxy-&gt;mapToSource(index).row();

        QModelIndex ipIndex = table-&gt;index(row, 1, QModelIndex());
        QVariant varAddr = table-&gt;data(ipIndex, Qt::DisplayRole);
        ip = varAddr.toString();
    }
    // TODO NetworkClass::hangup(ip);
}

</code></pre>
]]></description><link>https://forum.qt.io/post/418638</link><guid isPermaLink="true">https://forum.qt.io/post/418638</guid><dc:creator><![CDATA[theo_ld]]></dc:creator><pubDate>Tue, 03 Oct 2017 20:48:53 GMT</pubDate></item><item><title><![CDATA[Reply to Filling a TableModel doesn&#x27;t work ! on Tue, 03 Oct 2017 19:58:53 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">From the code you posted it's impossible to know whether you set your model to a <a href="http://doc.qt.io/qt-5/qtableview.html" target="_blank" rel="noopener noreferrer nofollow ugc">QTableView</a>, is it the case ?</p>
]]></description><link>https://forum.qt.io/post/418624</link><guid isPermaLink="true">https://forum.qt.io/post/418624</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Tue, 03 Oct 2017 19:58:53 GMT</pubDate></item></channel></rss>