Insert fill circle into cell of QTableWidget
-
Hi,
Is posible insert circle into cell of QTableWidget? I need the size of the circle can be changed dynamically.
-
Hi,
Use a custom QStyledItemDelegate and do the drawing yourself in there.
-
@SGaist Thank you for your answer.
Sorry but I'm starting to use QT, could you give me an example? How would you insert it in the table?
-
See the Star Item Delegate Example.
-
There are too many classes to just paint a circle in a cell. There is no other way?
-
There are too many classes to just paint a circle in a cell. There is no other way?
@juaniyoalm
Well you can also add the circle as an icon to the cell.
If you add blank text, the effect is similar.However, how big do u need the circle to be ?
It cannot be bigger than the cell height in any case.Update: this was just to answer if there was alternatives.
However, a delegate is far better for it. -
You need one class: the delegate.
In this example they have two if you take the custom editor that you likely don't need.
-
Hi
Adding to SGaist . Its not as bad as it looks.
For a paint circle only delegate, you need very little code.#include <QStyledItemDelegate> #include <QPainter> class CircleDelegate: public QStyledItemDelegate { protected: void paint(QPainter* painter, const QStyleOptionViewItem& opt, const QModelIndex& index) const { // set brush to green if selected else blue ( not needed just for fun) if (opt.state & QStyle::State_Selected) { painter->setBrush(Qt::green); } else { painter->setBrush(Qt::blue); } // paint a circle int CircleSize=10; painter->drawEllipse(opt.rect.center(), CircleSize, CircleSize); } };
and you set it to the table by
ui->tableWidget->setItemDelegateForColumn(1, new CircleDelegate());
the 1 is the col where u want itYou will need to read over
http://doc.qt.io/qt-5/model-view-programming.html
mostly section Item roles
as to make circle dynamic in size, you can use
a Qt::UserRole and setData.
so the size comes from the model. -
Just a couple of small addition to @mrjj 's code:
- you'll probably also need to reimplement
sizeHint()
to return a size appropriate to contain the circle. - blindly modifying the painter argument is risky. You should call
painter->save();
before setting the brush andpainter->restore();
after you finished the painting
- you'll probably also need to reimplement
-
Thank you, the solution is good for me!!! but I would like that the background of cells is fill too. This colour have to change too.
I thought about inserting a rectangle but I would need the circle to be located on top... -
Thank you, the solution is good for me!!! but I would like that the background of cells is fill too. This colour have to change too.
I thought about inserting a rectangle but I would need the circle to be located on top...@juaniyoalm
Hi
Just use other paint methods like
painter->drawRect(opt.rect); -
@juaniyoalm
Hi
Just use other paint methods like
painter->drawRect(opt.rect);@mrjj Thank you so much!!
Okay, I have that solved.
Another question is that the size of each circle depends on a value that my mushroom class has. In my class I created a paint method but I do not know how to link that method with the delegate or what is the way to write the method.
-
@mrjj Thank you so much!!
Okay, I have that solved.
Another question is that the size of each circle depends on a value that my mushroom class has. In my class I created a paint method but I do not know how to link that method with the delegate or what is the way to write the method.
@juaniyoalm
Hi, is the value in the model ?
The delegate can use values from model.
so the color and size of circle could come from model. -
@juaniyoalm
Hi, is the value in the model ?
The delegate can use values from model.
so the color and size of circle could come from model.No, that value is in a c++class. I have to create a custom model... It's ok?? Or I can use a default model??
-
No, that value is in a c++class. I have to create a custom model... It's ok?? Or I can use a default model??
@juaniyoalm
You can use http://doc.qt.io/qt-5/qstandarditemmodel.html
if it is. dont have to be custom.
I though you already had a model ?
Also is this with a QTableWidget ?
(it uses a model already ) -
@juaniyoalm
You can use http://doc.qt.io/qt-5/qstandarditemmodel.html
if it is. dont have to be custom.
I though you already had a model ?
Also is this with a QTableWidget ?
(it uses a model already ) -
@juaniyoalm
Super. With view its much easier.
so you have to construct a model with the data
that comes from the class.
Did you make a qstandarditemmodel before ? -
@juaniyoalm
Super. With view its much easier.
so you have to construct a model with the data
that comes from the class.
Did you make a qstandarditemmodel before ?@mrjj
Yes, I did but without data class -
@mrjj
Do you show me how to make?? -
@mrjj
Do you show me how to make??@juaniyoalm
Hi
Please read about userRoles and setDatahttp://doc.qt.io/qt-5/model-view-programming.html
section Item rolesyou can simply put the color / size in user role, or you can have
it directly as an item in your model.
Both ways are fine.
Then delegate read the user role and use the data.