<?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[Resizing verticalHeader in QTableView doesn&#x27;t refresh?]]></title><description><![CDATA[<p dir="auto">Hi everyone,</p>
<p dir="auto">I have a QTableView with vertical header, and I need to programatically change the width of this header.</p>
<p dir="auto">So I call:</p>
<pre><code>myQTableView-&gt;verticalHeader()-&gt;setFixedWidth(newWidth);
</code></pre>
<p dir="auto">Looks simple enough. But nothing happens. The header doesn't change. It seems the new display just needs a kind of refresh.<br />
After this call, if I just manually minimize my window, and then show it again, for example, then the new width is taken into account.</p>
<p dir="auto">I tried calling myQTableView-&gt;repaint(), but it has no effect.</p>
<p dir="auto">I managed a hack to this issue by calling:</p>
<pre><code>myQTableView-&gt;verticalHeader()-&gt;setFixedWidth(newWidth);
myQTableView-&gt;hide();
myQTableView-&gt;show();
</code></pre>
<p dir="auto">This works, but it feels like a dirty hack. Do you have any cleaner solution, or explanations for why the header is not correctly resized in the first place?</p>
]]></description><link>https://forum.qt.io/topic/89562/resizing-verticalheader-in-qtableview-doesn-t-refresh</link><generator>RSS for Node</generator><lastBuildDate>Sat, 07 Nov 2026 05:29:03 GMT</lastBuildDate><atom:link href="https://forum.qt.io/topic/89562.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 05 Apr 2018 11:58:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Resizing verticalHeader in QTableView doesn&#x27;t refresh? on Fri, 04 May 2018 18:17:49 GMT]]></title><description><![CDATA[<p dir="auto">I can reproduce the issue (and have an idea how to fix it): <a href="https://bugreports.qt.io/browse/QTBUG-67532" target="_blank" rel="noopener noreferrer nofollow ugc">https://bugreports.qt.io/browse/QTBUG-67532</a></p>
<p dir="auto">/edit: fix will be in 5.12 :)</p>
]]></description><link>https://forum.qt.io/post/451709</link><guid isPermaLink="true">https://forum.qt.io/post/451709</guid><dc:creator><![CDATA[Christian Ehrlicher]]></dc:creator><pubDate>Fri, 04 May 2018 18:17:49 GMT</pubDate></item><item><title><![CDATA[Reply to Resizing verticalHeader in QTableView doesn&#x27;t refresh? on Fri, 06 Apr 2018 08:30:29 GMT]]></title><description><![CDATA[<p dir="auto">I'm using Qt5.6.3 on windows 10.</p>
<p dir="auto">Originally, it's from a pretty big project, but I managed to reproduce the issue on something smaller. However, I must have changed something in the process, because it's now slightly different. Now the header is indeed resized, but it hides the first column instead of moving it. I guess in the big project version, the header is resized, but hidden by the first column instead.</p>
<p dir="auto">Here is a compilable code that shows the issue (I'd upload the files, but apparently I don't have the rights for that, it requires more reputation in the forum, I guess):</p>
<p dir="auto">The .pro file (nothing special):</p>
<pre><code>#-------------------------------------------------
#
# Project created by QtCreator 2018-04-06T08:49:03
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = bug_minimal
TEMPLATE = app

SOURCES += main.cpp\
        mainwindow.cpp \
    tablemodel.cpp

HEADERS  += mainwindow.h \
    tablemodel.h

FORMS    += mainwindow.ui

</code></pre>
<p dir="auto">main.cpp (nothing special either):</p>
<pre><code>#include "mainwindow.h"
#include &lt;QApplication&gt;

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
</code></pre>
<p dir="auto">mainWindow.h: I tried to reduce to the smallest case, and thus put everything in mainWindow.</p>
<pre><code>#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include &lt;QMainWindow&gt;
#include &lt;QTableView&gt;
#include &lt;QPushButton&gt;
#include &lt;QSplitter&gt;

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

public slots:
    void changeHeaderSize();

private:
    Ui::MainWindow *ui;
    QTableView *myTableView ;
    QSplitter   * _splitter;
    QAbstractTableModel* _model;
    QPushButton *_myButton;
};

#endif // MAINWINDOW_H
</code></pre>
<p dir="auto">tableModel.h, the model we use for out table (Strongly reduced as well, here a simple subclassing of QAbstractTableModel):</p>
<pre><code>#ifndef DATAEDITOR_DATAEDITORTABLEMODEL_H_
#define DATAEDITOR_DATAEDITORTABLEMODEL_H_

#include &lt;QAbstractTableModel&gt;
#include &lt;QTableWidgetItem&gt;

/**
 * @brief TableModel : models that holds QTableWidgetItem, each representing a column in a dataframe.
 * It has thus only one row
 */
class TableModel : public QAbstractTableModel{
    Q_OBJECT
public:
    TableModel(QObject *parent = 0) : QAbstractTableModel(parent) {}
    ~TableModel(){ for (auto i: _itemList) delete i;}

    // Functions reimplemented as required in http://doc.qt.io/qt-5/qabstracttablemodel.html#subclassing
    virtual int rowCount(const QModelIndex &amp;parent = QModelIndex()) const {return parent.isValid()?0:1;}   // only one line, we make extensive use of roles
    virtual int columnCount(const QModelIndex &amp;parent = QModelIndex()) const {return parent.isValid()?0:_itemList.size();}
    virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;      // defines the header content
    virtual QVariant data(const QModelIndex &amp;index, int role = Qt::DisplayRole) const;
    virtual bool setData(const QModelIndex &amp;index, const QVariant &amp;value, int role = Qt::EditRole) override;
    virtual bool insertColumns(int column, int count, const QModelIndex &amp;parent = QModelIndex());


private:
    QList&lt;QTableWidgetItem*&gt; _itemList;


};

#endif /* DATAEDITOR_DATAEDITORTABLEMODEL_H_ */
</code></pre>
<p dir="auto">tableModel.cpp:</p>
<pre><code>
#include "tablemodel.h"
#include &lt;QDebug&gt;

QVariant TableModel::headerData(int section, Qt::Orientation orientation, int role) const{

    if(orientation == Qt::Horizontal) {
        return "horizontal header Name" ;
    }

    return QAbstractTableModel::headerData(section,orientation,role);
}

QVariant TableModel::data(const QModelIndex &amp;index, int role) const{
    if (!index.isValid()) return QVariant();        // if index is invalid, return empty QVariant
    return _itemList.at(index.column())-&gt;data(role);
}


/**
 * @brief TableModel::setData modifies the data in the model
 * @param index index if the data to modify
 * @param value value to set
 * @param role role to set
 */
bool TableModel::setData(const QModelIndex &amp;index, const QVariant &amp;value, int role){
    // if the index corresponds to a valid culumn (less than the number of elements in _itemList, and on the  0th line)
    if (index.isValid() &amp;&amp; index.column() &lt; _itemList.size() &amp;&amp; index.row() == 0){
        // set the data in the model
         _itemList[index.column()]-&gt;setData(role,value);

        // emit the approriate signal (dataChanged)
        emit dataChanged(index,index,QVector&lt;int&gt;(role));
    }
    // return success because we consider it can't fail (todo : take into accout errors)
    return true;
}


// insertColumns :
// The items in each new column will be children of the item represented by the parent model index.
// If column is 0, the columns are prepended to any existing columns.
// If column is columnCount(), the columns are appended to any existing columns.
// Parents are ignored in our implementation
// Returns true if the columns were successfully inserted; TO DO: otherwise returns false.
bool TableModel::insertColumns(int column, int count, const QModelIndex &amp;parent){
    beginInsertColumns(parent,column, column+count-1);  // Mandatory call per Qt framework.
    while (count--){
        // creates new item
        QTableWidgetItem* newCol = new QTableWidgetItem();
        // adds to it a default TrueFalseMap
        _itemList.insert(column,newCol);
    }
    endInsertColumns();  // Mandatory call per Qt framework.
    return true;
}
</code></pre>
<p dir="auto">And finally mainwindow.cpp, where we can comment or un-comment 2 lines to see the effect of my dirty fix:</p>
<pre><code>#include "tablemodel.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui-&gt;setupUi(this);

    _splitter =  new QSplitter(Qt::Vertical,this);
    _splitter-&gt;resize(350, 350);

    // create a model, and put some random data into it.
    _model = new TableModel();
    _model-&gt;insertColumns(0, 2, QModelIndex());
    _model-&gt;setData(_model-&gt;index(0,0,QModelIndex()), QVariant(42), Qt::EditRole) ;
    _model-&gt;setData(_model-&gt;index(0,1,QModelIndex()), QVariant(57), Qt::EditRole) ;

    // Create the QTableView
    myTableView = new QTableView(this);
    myTableView-&gt;setModel(_model);
    myTableView-&gt;setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    // Adds a button to trigger the change in header size.
    _myButton = new QPushButton(this);
    _myButton-&gt;setText("Change header size");
    connect(_myButton, &amp;QPushButton::clicked, this, &amp;MainWindow::changeHeaderSize);

    _splitter-&gt;insertWidget(0,_myButton);
    _splitter-&gt;insertWidget(1,myTableView);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::changeHeaderSize() {
    myTableView-&gt;verticalHeader()-&gt;setFixedWidth(100);
    // De-commenting the follwoing lines forces the update and "fixes" the issue.
    //myTableView-&gt;hide();
    //myTableView-&gt;show();
}
</code></pre>
<p dir="auto">Thanks for your attention.</p>
]]></description><link>https://forum.qt.io/post/451626</link><guid isPermaLink="true">https://forum.qt.io/post/451626</guid><dc:creator><![CDATA[Andeol]]></dc:creator><pubDate>Fri, 06 Apr 2018 08:30:29 GMT</pubDate></item><item><title><![CDATA[Reply to Resizing verticalHeader in QTableView doesn&#x27;t refresh? on Thu, 05 Apr 2018 20:57:00 GMT]]></title><description><![CDATA[<p dir="auto">Hi,</p>
<p dir="auto">What version of Qt are you using ?<br />
On what OS ?<br />
Can you provide a minimal compilable example that shows this behaviour ?</p>
]]></description><link>https://forum.qt.io/post/451555</link><guid isPermaLink="true">https://forum.qt.io/post/451555</guid><dc:creator><![CDATA[SGaist]]></dc:creator><pubDate>Thu, 05 Apr 2018 20:57:00 GMT</pubDate></item></channel></rss>