5

I'm totally new to Qt, so I'd be glad to have a wide answer.

Here I drew up some model:

enter image description here

We have a kind of table that contains:

  • an integer value with a spinbox.
  • a cell with three(not specifically) grouped radio buttons
  • Editbox
  • A button that interacts with this particular editbox.

Also we have 2 buttons to add and remove items from the table.

I did some google search and found out that it can be done via QTableView. Is there any way to put such complex structures into a cell? Must it be a separate class inherited from QTableView?

Ron
  • 14,674
  • 4
  • 34
  • 47
Alignant
  • 61
  • 4
  • 1
    You can make your own classes inherited from 'QAbstractItemModel' and 'QStyledItemDelegate'. Example: http://doc.qt.io/qt-5/qtwidgets-itemviews-spinboxdelegate-example.html – olya Sep 01 '17 at 11:57
  • Usually, the table will not contain the edit boxes but will display the value, and when you edit the cell, an editor widget will be overlaid on top. – Kuba hasn't forgotten Monica Sep 01 '17 at 18:56

2 Answers2

2

I see at least three options to implement that in Qt:

  • Use a QtableView or QTableWidget and insert some custom controls in it. See comments made be other persons to your post
  • Use a QGridLayout and fill it with your controls by line and column
  • Make your own QWidget to store and manage the line elements (the spinbox, edit field, radio button) using a QHBoxLayout. You can design this in QtCreator, it can have it's own .ui. This could make it easy to handle the interaction between each QWidget of a line (directly handled by your QWidget class). Later, you can put an instance of it for every line you need in a QVBoxLayout.

Personnaly, I would go with the last option, but it may not work smartly if the controls of each line have different content/size (see comments), then first options should be prefered.

jpo38
  • 20,821
  • 10
  • 70
  • 151
  • In last option case, how you will align cells of each line with cells of other lines? – olya Sep 01 '17 at 12:07
  • @olya: that could be an issue, but not consider what the OP shows. `QSpinBox`, `QRadioButton` `QPushButton` size should be the same on every line, so then the `QLineEdit` will extend to occupy all space left. It may become an issue if `QSpinBox`, `QRadioButton` `QPushButton` content is different on every line. Then you would need to manually handle the size which may be a pain, I admit. – jpo38 Sep 01 '17 at 12:11
  • So you are inventing QTableView :) – olya Sep 01 '17 at 12:14
  • I would use the last option. Using a QTableView for non Qlabels is hell, considering OP is very new, this is the best way. – Jeroen3 Sep 01 '17 at 12:19
  • Implementing a delegate that exposes a typical control widget is maybe what 15 lines? – Kuba hasn't forgotten Monica Sep 01 '17 at 19:02
2

If you're going to have up to a hundred or maybe a few hundreds of elements in the table, then use QTableWidget.

If you're going to have too many elements (about thousands), then go for QTableView, and learn model-view programming.

The reason why I recommend QTableWidget is because you're a beginner. All you have to do there is create a widget, and use setCellWidget() and you're done.

If you have thousands of rows, then you're gonna have to draw the widgets yourself using QStyledItemDelegate, which will paint the widgets inside your QTableView. This is a very painful thing to do, but there's no way around it. The reasons you can find here.

The Quantum Physicist
  • 24,987
  • 19
  • 103
  • 189
  • I don't think it might be more than ten. Will it work for three radio buttons in a single cell? It is a custom widget. – Alignant Sep 01 '17 at 12:25
  • @Alignant Then definitely `QTableWidget`! Just keep in mind that the data will be duplicated in the table and wherever you want to use it. This is MUCH easier than Model-View (especially for a beginner). And yes, you can create ANY widget you want and put it in the table. Good luck! – The Quantum Physicist Sep 01 '17 at 12:27