Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly change the row height of a TableView?

Window {
    id: uninstallWindow
    width: 640
    height: 480

    property variant pluginData;

    TableView {
        id:_pluginTable
        anchors.right: parent.right
        anchors.rightMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 43
        anchors.top: parent.top
        anchors.topMargin: 0
        model: pluginData

        itemDelegate: Text {
            text: modelData
            font.pixelSize: 24
        }

        TableViewColumn {

        }
    }
}

It's taken me hours just to get this far, and I feel like this should be a relatively simple operation, so why is it so hard? As you can see I change the font size of the items in the table because they were too small by default. This simply caused them to get clipped by the non-changing row size. I've tried

  1. Setting a rowDelegate object (but this causes loss of all other styling info that is there by default like background, selection color, etc and I don't know how to specify it otherwise)

  2. Setting a custom model object based on QAbstractListModel / QAbstractTableModel (for some reason only known to Qt, the "data" function was never ever called...)

  3. Setting a custom item delegate (it seems that the height is no longer controlled from this object though)

What hoops do I need to jump through to get the rows to change their size?

like image 384
borrrden Avatar asked Jan 10 '23 23:01

borrrden


2 Answers

As the Asker already wrote, custom row height can be achieved using the rowDelegate, but this discards the default style. The default style can be restored using the SystemPalette.

rowDelegate: Rectangle {
   height: 30
   SystemPalette {
      id: myPalette;
      colorGroup: SystemPalette.Active
   }
   color: {
      var baseColor = styleData.alternate?myPalette.alternateBase:myPalette.base
      return styleData.selected?myPalette.highlight:baseColor
   }
}

This restores the default background color of the rows (including alternating the row colors when desired) and the color of the selected rows, which seems to be all that is needed.

like image 182
pepan Avatar answered Jan 12 '23 12:01

pepan


The following just worked like a charm for me in Qt 5.10:

rowDelegate: Item { height: 30 }

I do the actual styling (fonts/colors) in itemDelegate (and in headerDelegate) and provide content by TableViewColumns (with and without delegates of their own).

like image 30
HamRusTal Avatar answered Jan 12 '23 12:01

HamRusTal