5

I have a QTableView and some content in it. I want a behavior like horizontalHeader() -> setResizeMode( ResizeToContent ) but it must not create horizontal scrollbars - I mean the viewport must not be wider than the table.

Even with creating a new QHeaderView it's not so easy, since there are only a few virtual methods.

drawing to visualize the problem

Thanks in advance,

Charly

PS: I have a custom item delegate, which shortens the long texts with "...". It returns as sizeHint() the full size, but when the paint() method receives a smaller size (e.g. simulated with resizeMode() == Qt::Interactive) it crops the content.

Charly
  • 1,270
  • 19
  • 42

1 Answers1

6

New Answer

You need to set the stretch on individual sections, I've created a simple test app:

Screen shot of test

test.cpp

#include <QtGui>

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

    QStandardItemModel mdl(3, 3); // rows, cols
    mdl.setHorizontalHeaderLabels(QStringList() << "Name" << "Size" << "Date");
    mdl.setItem(0, 0, new QStandardItem("Short name"));
    mdl.setItem(0, 1, new QStandardItem("25kb"));
    mdl.setItem(0, 2, new QStandardItem("2011/07/05"));
    mdl.setItem(1, 0, new QStandardItem("This is a long name"));
    mdl.setItem(1, 1, new QStandardItem("25kb"));
    mdl.setItem(1, 2, new QStandardItem("2011/07/05"));
    mdl.setItem(2, 0, new QStandardItem("This is a long long long long name"));
    mdl.setItem(2, 1, new QStandardItem("25kb"));
    mdl.setItem(2, 2, new QStandardItem("2011/07/05"));


    QTableView view;
    view.setModel(&mdl);
    QHeaderView* hdr = view.horizontalHeader();
    hdr->setResizeMode(0, QHeaderView::Stretch);
    hdr->setResizeMode(1, QHeaderView::ResizeToContents);
    hdr->setResizeMode(2, QHeaderView::ResizeToContents);

    view.show();
    return app.exec();
}

test.pro

QT += core gui
SOURCES=test.cpp

Notice: It's important that void QHeaderView::setResizeMode(int, ResizeMode) is called when when this logical index exists, that is, when a model which defines these columns is attached to the view.


Old Answer

QAbstractScrollArea has the horizontalScrollBarPolicy property which can have the option ScrollBarAlwaysOff.

Try something like:

QAbstractScrollArea* scrollArea = // ???
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

Silas Parker
  • 8,017
  • 1
  • 28
  • 43
  • Using `ScrollBarAlwaysOff` has a different effect. It just hides the scrollbar, but has no influence on the size of the column - or at least does not solve the problem. – Charly Apr 25 '12 at 06:13
  • What about also setting the properties `resizeMode` to `ResizeToContent` and `stretchLastSection` to `true`? You could add a sketch/drawing/mockup to your post to illustrate what you are trying to achieve. – Silas Parker Apr 25 '12 at 07:39
  • Ok, I will prepare a drawing. `ResizeToContent` and `strechLastSection(true)` doesn't help... – Charly Apr 25 '12 at 09:24
  • The test-app works fine, my 'real app' doesn't work... but this is what I can figure out for myself. Thanks a lot though! – Charly Apr 25 '12 at 12:33
  • 1
    it wasn't working because I set the resize mode at a time, where no model was attached - thus the table has zero columns and this data got lost. I added this point to your answer... – Charly Apr 25 '12 at 12:56