You are not logged in.

1

Wednesday, December 28th 2011, 1:35pm

Row size question with QTableView and QItemDelegate

I have a QTableView and model that works fine. The is used for scheduling flights. It has 4 rows, Flight #, Aircraft, Time, Payload.

The table is editable, the Flight # is a QLineEdit, the Aircraft is a QComboBox, the Time is a QTimeEdit, and payload is a QListWidget. The first three work perfectly, the list widget is so-so. The list widget will display and set my selection accordingly, but visually it is not correct.

What I want to happen is that I have a bunch of flights (rows) scheduled. Say 5. Each row is the normal, default height that isn't overridden from my sizeHint() function in my delegate class. When I select a cell, the row size shouldn't change UNLESS I select the 4th column to view the payload's list widget. At this time the row should expand to say, 200px. Then, when I'm done editing, or when I select a different row/cell, I want the size to return to normal.

I have attempted many, many things. When I define my own sizeHint() function in the delegate class, it resizes all of the rows permanently to 200. I've also tried messing with the sizes of 4 different widgets inside of the createEditor() function with limited success. A more elegant, less "hackish" way would be nice. Thank you! :D

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
void cScheduleDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    switch (index.column())
    {
        case FlightNum:
        {
            //Make a new QLineEdit object and return it so it can be displayed in the table at the current index
            QLineEdit* lineEdit = new QLineEdit(parent);

            lineEdit->setMaximumHeight(lineEdit->sizeHint().height());

            return lineEdit;
        }
        case Aircraft:
        {
            //Make a new QComboBox object and return it so it can be displayed in the table at the current index
            QComboBox* comboBox = new QComboBox(parent);

            comboBox->setMaximumHeight(comboBox->sizeHint().height());

            return comboBox;
        }
        case FlightTime:
        {
            //Make a new QTimeEdit object and return it so it can be displayed in the table at the current index
            QTimeEdit* timeEdit = new QTimeEdit(parent);

            timeEdit->setMaximumHeight(timeEdit->sizeHint().height());

            return timeEdit;
        }
        case Payload:
        {
            //Make a new QListWidget object and return it so it can be displayed in the table at the current index
            QListWidget* listWidget = new QListWidget(parent);

            listWidget->setMaximumHeight(listWidget->sizeHint().height());

            return listWidget;
        }
        default:
        {
            return QItemDelegate::createEditor(parent, option, index);
        }
    }
}


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
QSize cScheduleDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    switch (index.column())
    {
        case Payload:
        {
            //width is 0 because I have the horizontal headers stretch
            return QSize(0, 200);
        }
        default:
        {
            //width is 0 because I have the horizontal headers stretch
            return QSize(0, 50);
        }
    }
}

2

Wednesday, December 28th 2011, 3:21pm

did you try using selectionChanged signal and then using setrowheight?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.