PDA

View Full Version : QTableView with buttons



mak_user
31st October 2011, 15:36
Is it possible to put pushbuttons in one column of QTableview which represen QSqlRelationalTableModel?
For now I only succeed to put pushbutton which is only available in edit mode after dblclk the cell. I am doing that using my delegate(by sub-classing QStyledItemDelegate). My goal is to have pushbuton available always not only in edit mode and I need help for this.

Santosh Reddy
1st November 2011, 04:12
will this work

//main.cpp
#include <QtGui>
//QPushButton in QTreeView
class MyWidget : public QTreeView
{
public:
MyWidget(QWidget* parent = 0) : QTreeView(parent)
{
setModel(&model);
model.setRowCount(10);
model.setColumnCount(2);
for(int i = 0; i < model.rowCount(); i++)
{
model.setData(model.index(i, 0), QString("PushButton %1").arg(i));
setIndexWidget(model.index(i, 1), new QPushButton(QString("PushButton %1").arg(i)));
}
}
private:
QStandardItemModel model;
};


int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
w.show();
return a.exec();
}
#include "main.moc"

mak_user
1st November 2011, 09:22
My QTableView is not static. It is dynamically populated from database table. That is why I need to use sub-classed delegate. Any other idea?

Santosh Reddy
2nd November 2011, 00:16
QPushBotton is a widget which you want to be displayed and clickable in QTreeView cell. Delegate can be used to create a dynamic editor, and some static painting on the cell area, a picture or icon or some advanced text, but it cannot be used for a widget. The problem having a widget there is that that widget should be able to receive mouse events (in case of QPushButton widget).

The only way the widget in the cell can receive events is when it is part(child widget) of the view widget, a widget cannot be part of delegate. Now to have a widgets dynamically added cells int the view, you need to subclass QTreeView and connect to the row insert, delete & move signals from model and create, delete or move the widget accordingly in the view. (this is some task)