PDA

View Full Version : QWidget in tableview



sepehr
21st February 2009, 12:32
I want to know how to put a widget into a tableView along with the row from my database table?(each row has it's own widget) like a QPushbutton which suppose is the delete button for the row and when is clicked deletes that row from corresponding table.
should I paint the shape of the button and set editor to a QPushbutton using an QItemDelegate? or what are other approaches ?
---
thanks in advance

wysota
21st February 2009, 13:25
Hmm... it depends on the effect you want to achieve. If you only have a few such rows and the buttons are to be related strictly to the view (not tied to the model in any way), you can use QAbstractItemView::setIndexWidget(). Then you have to connect signals from the buttons to a custom slot in the view (probably through a QSignalMapper) and that's it. The downside is that this approach only makes sense if you have just a few rows, otherwise the whole view starts to slow down. In that case you have to use the delegate approach you mentioned.

But I would like to suggest an alternative approach. Buttons in table rows came from the web world where it was the easiest way to perform actions on some parts of tables. With desktop applications we have other means of doing that - context menus, toolbars, checkboxes and external buttons. Finally you can also reuse the header or place your own widget inside the view (just like the header) and use it the way you want.

sepehr
22nd February 2009, 04:08
void buttonDelegate::paint( QPainter *painter,const QStyleOptionViewItem &option, const QModelIndex &index ) const
{
if (index.column() == 1)
{
QStyleOptionButton button;
button.rect = option.rect;
button.text = QString::fromUtf8("RemoveRow");
button.state=QStyle::State_Raised |QStyle::State_Enabled;
QApplication::style()->drawControl(QStyle::CE_PushButton,&button, painter);
}

}

I managed to make the delegate like that,but there is another question arising here which is how can I catch the paint event when user moves the mouse over the row containing the drawn widget? so I can paint mouse over state of the widget

wysota
22nd February 2009, 07:31
Set the WA_Hover attribute on the viewport of the view and then handle the events through the delegate's editorEvent() method.