Best method for attaching QWidget into a Qtable()
-
Hi everyone,
This is my first time posting here. I have been coding in Qt for a few years now, but have only recently gone a bit deeper into the Model/View/Customising Widgets.
Im currently stuck on a problem, and was wondering if anyone could help. I have looked around but have been unable to find something that helps with regards to this problem.
I have a QAbstractTable that gets populates about 100+ entries, To edit this table I have a QStylisedItemDelegate, that on specific cells creates a drop down with a list of users from the DB. All of this works great, I have color cells depending on which department, or heading is in a specific cell.
Now here comes the curve ball.
In 5 of my columns I want to populate the cell with a widget that I have writen, only when in view mode, not in edit mode, as I still want my drop-downs when editing. The widget is nothing to advanced, It draws a block for every stage under the column and gives the status of a stage a color. If the user moves over the color the tooltip gives more information about the stage.
!https://dl.dropboxusercontent.com/u/10740198/QtQuestion.jpg(The Table and the Widget)!
Is there a way to populate the cells with my widget, should I instead of creating instances of the my custom widget, use the Delegate.paint() function and recreate the widget inside my delegate(to me this seems a bit messy, but if it has to be done it shall) or should I look into creating a custom view. Im still new to views as so all suggestions are welcome :)
Thank for the help everyone
-
You need to implement the paint() method of delegate when you are in read only mode to display your customwidget.
-
How does one display the widget in paint(), as paint I have found only allows you to paint something new, not create a widget and input it into a cell.
Am I missing something, or a special way of creating a widget, pulling out its paint and putting it into my delegates paint?
-
Yes, It is painting something new. Not for the putting your existing widget. You can copy-paste the widget code as you said.
-
Thanks Dheerendra, I was wondering if by any chance you have a simplecode snippet of something like this as im still struggling to find the correct method of doing this.
-
Here is snippet.
have following enums defined for 5 columns.
@
enum Column {Name, Father,Family,Adhar_No,State};
@So paint is called for Adhar_no column
Tableview settings :
@
ui->tableView->setItemDelegateForColumn(Adhar_No,myDeligate);
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
@
Delegate Paint implementation.
@
void QMyDeligate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const{
qDebug() << "****I am here *****"<<endl;
painter->save();
if (index.column() == Adhar_No){
qDebug() << "Create Hell" << endl;
if (option.state & QStyle::State_Selected)
painter->fillRect(option.rect, option.palette.highlight());
else if (option.state & QStyle::State_MouseOver)
painter->fillRect(option.rect, option.palette.midlight());
QFontMetrics fm(option.font);QRect outline = option.rect; int outmid = outline.center().y(); QRect fnBound = fm.boundingRect("filename"); int fnBoundMid = fnBound.center().y(); fnBound.moveLeft(outline.left()); fnBound.translate(0, outmid - fnBoundMid); painter->drawEllipse(fnBound); //painter->drawText(fnBound, Qt::AlignVCenter | Qt::AlignLeft, "filename"); } painter->restore();
}
@ -
do you know of a way to only paint every cell on startup and then only selected cells after startup. as currently all 500cells are getting repainted ever time I select anything, and it has created a massive slow down.
-
Thanks, I finally figured it all out and its working :D hooraa!