I have created one table by using QTableview and QAbstractTableModel .
In one of the cells, I want to add one help button in the right corner of that cell.
Is there any way to achieve this?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
You will have to implement your own delegate for that.
In Qt, aside from the Data, the Model and the View, you have your Delegates. They provide input capabilities, and they are also responsible for rendering “special” items in the View, which is what you need.
Qt doc has a good coverage on those (keywords:
Model/View programming), and you can also find some examples here and here.Also (a little off-topic, but I think I should point this out), if you use an ordinary
QTableWidget, you can insert anything into any cell with it’ssetCellWidget()function.UPD
here is a slightly modified example from Qt docs (I suck with model/view stuff in Qt so don’t beat me hard for this code). It will draw a button in each cell on the right, and catch the click events in cells to check if the click was on the “button”, and react accordingly.
Probably this is not the best way to do it, but as I mentioned, I’m not too good with Qt’s models and views.
To do things right and allow proper editing, you will need to also implement
createEditor(),setEditorData()andsetModelData()functions.To draw your stuff in a specific cell instead of all cells, just add a condition into the
paint()function (note that it gets the model index as an argument, so you can always know in what cell you are painting, and paint accordingly).delegate.h:
delegate.cpp:
main.cpp
The result will look like this: