Add widget right aligned to a QTableWidget cell
-
Ok
@VRonin said in Add widget right aligned to a QTableWidget cell:
you should cast it to the correct type: QHoverEvent. Also me->pos() works in Qt5 but you need me->position() for Qt6
Will do it.
I have a question: in
createEditor
, I just deleted the twoconnect
lines and signals are still being emitted. So what is the purpose of creating theMyButton
object there? -
@VRonin said in Add widget right aligned to a QTableWidget cell:
Also me->pos() works in Qt5 but you need me->position() for Qt6
here
QRect::contains
takes aQPoint
butme->position
is aQPointF
, I will have to do some casting.Edit: I will just use
QPointF::toPoint
-
@hbatalha said in Add widget right aligned to a QTableWidget cell:
here QRect::contains takes a QPoint but me->position is a QPointF, I will have to do some casting.
Just call
.toPoint()
seeTableWidgetDelegate::viewportEvent
I have a question: in createEditor, I just deleted the two connect lines and signals are still being emitted. So what is the purpose of creating the MyButton object there?
Those 2 lines deal with when the user is actually editing. In my original example the delegate was the default editor (usually a lineedit) + a button on the side. The default editor is used to change the contents of the cell. If the user starts editing the cell (doubleclick by default) the editor will pop up and you'll see that clicking on the buttons will not do anything unless you have those 2
connect
s.
In a nutshell we have to handle 2 cases: when the delegate is just painting and when the editor is presented to the user -
@VRonin said in Add widget right aligned to a QTableWidget cell:
Those 2 lines deal with when the user is actually editing. In my original example the delegate was the default editor (usually a lineedit) + a button on the side. The default editor is used to change the contents of the cell. If the user starts editing the cell (doubleclick by default) the editor will pop up and you'll see that clicking on the buttons will not do anything unless you have those 2 connects.
In a nutshell we have to handle 2 cases: when the delegate is just painting and when the editor is presented to the userGot it, thanks.
Could you please answer me here?
-
@VRonin
I hope I can hijack this thread now that the OP has had his questions answered?I know the crusade you are on. And I have to respect that the OP may have a lot of items, I don't know. But would you admit that --- without starting a flame-war --- this is one hell of a lot of code to add for a delegate approach if
setCellWidget()
would have done the job? -
@JonB said in Add widget right aligned to a QTableWidget cell:
would you admit that this is one hell of a lot of code to add for a delegate approach
I'll even go one step further, and say that for anything more complicated than adding a button, going down reimplementing
paint
is completely unreasonable. It still doesn't excusesetCellWidget()
. You can always use something like this delegate (beware: old code) to have 1 widget taking care of painting every cell instead of having as many widgets as there are cells -
@JonB said in Add widget right aligned to a QTableWidget cell:
Beware that this topic is not about replacing
setCellWidget()
, the one aboutsetCellWidget()
is still open.this is one hell of a lot of code to add for a delegate approach if setCellWidget() would have done the job?
setCellWidget()
is just awful when you have a chance of using a delegate instead. Since I discovered that I can use a delegate instead, having asetCellWidget
has become very unsettling for me. -
@hbatalha
Thanks, I did wonder whether it was a different topic, but didn't try to go find it.Your sentiments must warm the cockles of @VRonin's heart ;-) But for those of us who value writing as little code as possble when there is built-in stuff it's not such good news. I do respect why delegates are a better choice, shame there is so much code to write.
@VRonin should "publish" a library/class,
QStyledItemWidgetDelegate
, perhaps based on the code of his referred to in his last post, which does everything one might want fromsetCellWidget
but in a delegate-y way, for everyone to use :) [Also, he should try to keep it down to, say, 10 lines...] -
@JonB said in Add widget right aligned to a QTableWidget cell:
Also, he should try to keep it down to, say, 10 lines...
ahahah!
should "publish" a library/class, QStyledItemWidgetDelegate
Already on my TODO list
-
@JonB said in Add widget right aligned to a QTableWidget cell:
@VRonin should "publish" a library/class, QStyledItemWidgetDelegate, perhaps based on the code of his referred to in his last post, which does everything one might want from setCellWidget but in a delegate-y way, for everyone to use :) [Also, he should try to keep it down to, say, 10 lines...]
I was about to say that. Maybe propose the library to the Qt team to add to the official release(if that's how it is done)
-
@hbatalha said in Add widget right aligned to a QTableWidget cell:
@VRonin I have a simple question: is it possible to use
setItemDelegateForRow
andsetItemDelegateForRow
at the same view(same row)?I assume you mean
setItemDelegateForRow
andsetItemDelegateForColumn
.
from https://doc.qt.io/qt-5/qabstractitemview.html#setItemDelegateForRowNote: If a delegate has been assigned to both a row and a column, the row delegate will take precedence and manage the intersecting cell index.