45

I am new to this Model/View Framework of Qt. In my application I want to have 1000 X 1000 cells. There should be minimum memory requirement & it should be fast. I don't know what this Model terminology is for. But I have my own class which knows how to deal with the double variables stored in the table. Currently I am using QLineEdit's with a Validator to create the array of cells. But it was way too slow for cells > 50 X 50. So I decided to go the good old MS Excel way.

So which Widget should I use: QTableWidget or QTableView?

And can anybody please explain in short what this Model/View framework is? I am not a Computer Science guy hence I am finding it tough to understand...

1
  • 1
    Speaking of the "MS Excel way", they would never create the equivalent of one QWidget per cell. In your example, that would be 1,000,000 cells! You really only need enough such widgets to cover the screen and then you move the content accordingly. Commented Jul 4, 2022 at 23:18

2 Answers 2

35

cmannett85's recommendation is a good one. Read the docs about a dozen times.

Start with QTableWidget if your needs are pretty basic. QTableWidget is a QTableView PLUS a basic data model, built in. The table manages its own data.

Then, if performance and memory issues are your primary concern and you think you can out-perform the QTableWidget implementation, then a QTableView interface on top of a QAbstractTableModel or QStandardItemModel is your best bet. QStandardItemModel combines the raw data and model into a single class, while QAbstractTableModel allows you to store your raw data separately from the model. Since you're new to Qt's model-view architecture, I'd recommend using the QStandardItemModel until you feel like you're getting the hang of it.


If your performance still isn't good enough, avoid a lot of the memory duplication and wasted objects by implementing your custom model. Plus, get yourself a good textbook and read its chapter on the model-view framework about 12 times. That section alone was worth its weight in gold, imho.

Here are the basics for Qt's custom model-view framework:

  • Your actual data is stored in a list/tree somewhere
  • The model provides a standard framework for queries to and edits for your data
  • Proxy models (optional) allow you to sort/filter your data without affecting the original model
  • The view provides a means to visually observe and interact with your data
  • Delegates (often optional) tweak the appearance of your data and provide custom editors to the data

If you're feeling both cheap and brave, check out this excerpt on implementing your own custom model. Work at it one function at a time and play with it as you go.

2
  • The "this excerpt" link is now dead
    – Claudiu
    Commented Aug 21, 2015 at 21:38
  • The broken link should work now.
    – Phlucious
    Commented Dec 21, 2023 at 0:31
21

To understand the framework, start off with the documentation about it. It starts slow, but becomes moderately extensive and covers most of the classes involved.

QTableWidget or QTableView?

Once you have read the documentation you will see why this question doesn't really make any sense: a QTableWidget uses a QTableView to display the data. QTableWidget (along with QTreeWidget, etc.) uses the MVC framework, but it encapsulates it all to a handy package useful for most purposes, but if you need to do something different, you will have to crack it into it's component parts and reimplement the bits you need.

2
  • Thank You read that the document about 2-3 times, starting to get a hang of it.
    – Cool_Coder
    Commented Mar 11, 2013 at 10:12
  • 4
    QTableWidget uses a QTableView, that's actually the information I was after, thanks. Edit: I see that QTableWidget even is a QTableView
    – Bernhard
    Commented Nov 27, 2018 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.