Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

QTableWidget vs QTableView

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...

like image 815
Cool_Coder Avatar asked Mar 08 '13 09:03

Cool_Coder


People also ask

What is a qtablewidget?

Table widgets provide standard table display facilities for applications. The items in a QTableWidget are provided by QTableWidgetItem. If you want a table that uses your own data model you should use QTableView rather than this class. Table widgets can be constructed with the required numbers of rows and columns:

What is qtableview in Qt?

The QTableView class is one of the Model/View Classes and is part of Qt's model/view framework. QTableView implements the interfaces defined by the QAbstractItemView class to allow it to display data provided by models derived from the QAbstractItemModel class.

Should I use qhash or qtableview or qabstracttablemodel?

I usually prefer QTableView. You can subclass [ [Doc:QAbstractTableModel]] as a base. However, it also depends on the expected size of your data set, and the way you manipulate it. One problem is (for either case) that QHash does not suplly notification signals you can use to update your model.

How do I move from one cell to another in qtableview?

Because QTableView enables tabKeyNavigation by default, you can also hit Tab and Backtab to move from cell to cell. The table has a vertical header that can be obtained using the verticalHeader () function, and a horizontal header that is available through the horizontalHeader () function.


1 Answers

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

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 what you're looking for.

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 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.

like image 132
Phlucious Avatar answered Sep 19 '22 08:09

Phlucious