Important: Please read the Qt Code of Conduct -

[Solved] How to get a QTableView with a text and a delegate in the same cell?

  • Hello guys:

    I've a QTableVew working fine with a QAbstractTableModel subclass and a QItemDelegate subclass. I would like to know if there is a way to get a delegate sharing the same cell with a text. For example:

    Lets say I've a column named Proxy and I want the cells have the following format:

    ...where the ip address is a text (maybe a QLineEdit) and the port is a QSpinBox, and they are in the same cell.

    I've made some test but I didn't get it working.

    ...some suggestions?

    Thanks in advance.

  • A delegate does not occupy a cell, it is responsible for both painting a cell and handing interaction with one. That also means that you cannot "share" the space in a cell between a delegate and a text, as a delegate is responsible for rendering that text in the first place. If you did not set a delegate, then Qt is using the default one.

    So, if you want a delegate that renders both a text and something else, you will have to create that.

    From your text, it is not clear if you want to have your line edit and your spinbox always visible, or just when editing. I would suggest the later. In that case, the easiest solution is to create a widget that combines a QLabel, a QLineEdit and a QSpinBox in a horizontal layout, and use that as the editor widget for your delegate.

  • Yes, I prefer to show the widget just when editing the cell.

    What I don't understand is, why I need a QLabel and a QLineEdit in the widget I have to create? I believe the QLineEdit together with the QSpinBox could be enough, isn't?

    Anyway, I will do it as you suggested, it seems to be logical, and maybe the only way or at less the simplest. In fact during my tests I had some similar idea, now I understand why it doesn't worked.

    Thanks for your quick answer and the technical correction.

  • I guess it depends how you want your editor to look exactly. I was under the impression you had some text there that was not directly editable (you talked about sharing the space between the rendered text and an (editable?) delegate), but perhaps I just misunderstood. If you don't need the static text while editing, then you just leave out the label.

    Glad to help, I hope it works out well for you!

  • Sorry for the misunderstanding. What I meant was a text that could be edited when editing the cell, that was the reason what I mentioned to use QLineEdit instead of QLabel.

    But what surely was very clear, was your advice. I got it working exactly as you suggested.

    Thanks again for your help.

Log in to reply