PDA

View Full Version : Resizing vertical header in QTableView



p.csakany
31st October 2010, 21:10
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:

ChrisW67
31st October 2010, 22:54
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?


#include <QtGui>
#include <QDebug>

class View: public QTableView {
Q_OBJECT
public:
View(QWidget *p = 0): QTableView(p) {
connect(&t, SIGNAL(timeout()), this, SLOT(changeIt()));
t.start(5000);
}
public slots:
void changeIt() {
model()->setHeaderData(2, Qt::Vertical, "Really long test");
};
private:
QTimer t;
};

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

QStandardItemModel model(4, 4);
for (int row = 0; row < 4; ++row) {
for (int column = 0; column < 4; ++column) {
QStandardItem *item = new QStandardItem(
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"

p.csakany
1st November 2010, 08:49
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:


QStandardItemModel * model =( ( QStandardItemModel * ) ( this->model() ) ); model->insertRows ( 0, 1 );

QBrush brush=QBrush(color);
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().


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?

ChrisW67
1st November 2010, 23:06
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)?