Resizing vertical header in QTableView
Hello all,
How to resize the vertical header of a QTableView to make it wide enough so it does not clip the text?
I guess it must go via QHeaderView gained using the verticalHeader() method of QTableView. However what ever I try it does not have any effect.
If the vertical header could resize itself to fit the contents as the columns do that would be just perfect.
I see a lot about this issue on google around the time Qt4 came out, however I could not find a solution.
Please help. :crying:
Re: Resizing vertical header in QTableView
The vertical header resizes to accommodate the row headings for me. Can you post a minimal compilable example showing the behaviour you are trying to change?
Code:
#include <QtGui>
#include <QDebug>
Q_OBJECT
public:
connect(&t, SIGNAL(timeout()), this, SLOT(changeIt()));
t.start(5000);
}
public slots:
void changeIt() {
model()->setHeaderData(2, Qt::Vertical, "Really long test");
};
private:
};
int main(int argc, char *argv[])
{
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QString("row %0, column %1").
arg(row
).
arg(column
) );
model.setItem(row, column, item);
}
}
// change before display
model.setHeaderData(0, Qt::Vertical, "Test");
View v;
v.setModel(&model);
v.show();
// change after display
model.setHeaderData(1, Qt::Vertical, "Long Test");
return app.exec();
}
#include "main.moc"
Re: Resizing vertical header in QTableView
Hello and thanks for the response.
I have a heavily styled widget with rounded borders and padding. The vertical header text is added automatically by when I insert a new row. These are numbers starting from 1.
The single digit numbers cannot be seen. The header resizes if there is more then 9 rows - double digit header text -, which makes the single digit number visible and part of the second digit of the double digit ones. It works fine without styling.
I use this code to insert the rows:
Code:
AddTextItem ( displayqueue_item.item1, ITEM1, 0, brush );
AddPixmapItem ( "", displayqueue_item.item2, ITEM2, 0, brush );
As a workaround, I added an extra space to every header text after calling insertRow().
Code:
for( int i=model->rowCount(); i>0; --i)
model
->setHeaderData
(i
-1, Qt
::Vertical,
QString("%1 ").
arg(i
));
This is now looks right until the style is changed again. Is this a bug in the QT resizing algorithm?
Re: Resizing vertical header in QTableView
Possibly, but you haven't really given us anything to go on. How is your heavily styled widget achieved; style sheets, global custom style, derived widget paint method ? Do you override QHeaderView and return a revised size hint? Do you style QHeaderView or just the QTableView (or whatever)?