Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
How to display a model data in 2 views in different format, say in binary and hexadecimal?
Hi I'm new to both GUI programming and Qt, just having a question and it has confused me for days.
I need to display a model in 2 views, but in different format, in details, I have a 2 dimensional array of unsigned char, and want to display them in 2 table, one in binary format, and the other in hexadecimal.
The model is derived from the QAbstractTableModel, and I'm using 2 QTableView to display it.
I know I can let the model to output a QString in binary or hexadecimal format but that doesn't meet my requirement to share the same model within 2 views. So I just let the data() function to return a unsigned char directly.
I guess a delegate is the right way to get the data rendered properly for each view? but I couldn't find a document about this, the delegate documents are all about editing data with widgets or graphic displays.
I noticed there is a function called displayText() in QStyledItemDelegate, and I tried it and it works (derived 2 new delegates from it and overrided the displayText() functions) . But this function doesn't exist in QItemDelegate, according to the class reference, QStyledItemDelegate only comes to existance after Qt 4.4, so I guess there must be some other way to do it, in other words I might use the wrong approach.
Could anyone give me some advice on this? Thanks
qxoz last edited by
Well you can create two proxy models, one for hex and one for bin. "qabstractproxymodel":http://qt-project.org/doc/qt-4.8/qabstractproxymodel.html
"example":http://qt-project.org/doc/qt-5.0/qtwidgets/itemviews-customsortfiltermodel.html - this is a simple example for creating custom proxy model. In your case you need reimplement data().
You can also manage to do it with only one delegate that you would configure. What version of Qt are you using ?
Thank you all guys.
Yep I agree a proxy model is another way to do that.
I'm using Qt 4.6.2
andre last edited by
I actually think a delegate is a nicer option here.
I still got the question, if you want to use delegate, is the displayText() function in QStyledItemDelegate the only way to go? What if you used a QItemDelegate ?
andre last edited by
It really depends on your precise requirements. You can even do the complete rendering of the item yourself if you want. I'd base my delegate of QStyledItemDelegate if I had a choice, but if you base of QItemDelegate I'd simply reimplement drawDisplay().
Thank you Andre