Frage

I am using Qt tableWidget in order to display a list of items with columns. However there is huge space between the items in this list.

How can I change the spacing so that items (only text) are more close to each other?

Example screenshot:

enter image description here

War es hilfreich?

Lösung 2

I would try to exploit

void QTableView::resizeRowToContents ( int row ) [slot]

Resizes the given row based on the size hints of the delegate used to render each item in the row.

It behaves by default as expected for my use case, growing the row depending on the image being visible, shrinking to fit text otherwise.

Sorry I didn't explored the finer control on actual size you can achieve with the render delegate

Andere Tipps

Probably the easiest approach is to hide grid and repaint it with size you need. For this you'll have to create own class which inherits QTableWidget and define custom paintEvent. Sample code:

void paintEvent(QPaintEvent *event)
{
    QTableWidget::paintEvent(event);
    QPainter painter(viewport());

    for (int i = 0; i < columnCount(); ++i)
    {
        int start = horizontalHeader()->sectionViewportPosition(i);
        QPoint from = QPoint(start, 0);
        QPoint to = QPoint(start, height());

        painter.drawLine(from, to);
        start += horizontalHeader()->sectionSize(i) - 10;
        from = QPoint(start, 0);
        to = QPoint(start, height());
        painter.drawLine(from, to);
    }

    for (int j = 0; j < rowCount(); ++j)
    {
        int start = verticalHeader()->sectionViewportPosition(j);
        QPoint from = QPoint(0, start);
        QPoint to = QPoint(width(), start);

        painter.drawLine(from, to);
        start += verticalHeader()->sectionSize(j) - 10;
        from = QPoint(0, start);
        to = QPoint(width(), start);
        painter.drawLine(from, to);
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top