[Qt-interest] QGridview of custom QWidgets

SeekAWayOut SeekAWayOut at gmail.com
Thu Sep 23 02:14:11 CEST 2010


On 2010年09月22日 03:33, Leonardo M. Ramé wrote:
> Hi, I would like to know if it is possible to create a QGridView
> showing QWidget descendants, a link to an example would be great.
>
> Thanks in advance,

What version of Your qt? Qt3 or Qt4?
What do you mean QGridView?
QGridLayout?
Or Q3GridView (Qt4 is QTableView)

the Qt4 help say--
"The QGridView class has been renamed Q3GridView and moved to the 
Qt3Support library. In Qt 4, we recommend that you use QTableView or 
QAbstractItemView for presenting tabular data."

If QGridView means QGridLayout. Do not read the contents of the 
following, I misunderstood.

If you mean is to show an edit control in a QGridView cell,Use 
QItemDelegate class(In Qt4. In Qt3 I don't know).
First create A derived class based on QItemDelegate.Overload member 
function "createEditor", "setEditorData", "setModelData"
Then, assign it to QtableView object.

example

class customEditor : public QItemDelegate{
...
public:
  /* createEditor return your custom Editor Widget*/
  QWidget* createEditor (QWidget* parent, const QStyleOptionViewItem& 
option, const QModelIndex& index) const {
   /* "index" argument is model's current row and column.
If only the specified cell to display, you must determine the 
parameters. or use QTableView's setItemDelegateForColumn, 
setItemDelegateForRow to specified cells of column or row */
   /* if (index.column () == 2) */
   return new QLineEdit (parent);
  }
  /* setEditorData and setModelData was called by QTableView Object. */
  /* overload setEditorData function For how to display data in your 
custom widget */
  void setEditorData (QWidget* editor, const QModelIndex& index ) const {
   QLineEdit* theEditor = qobject_cast < QLineEdit* > (editor);
   theEditor->setText (index.data ().toString ());
  }
  /* overload setModelData function for how to set the Editor data to 
Model */
  void setModelData (QWidget* editor, QAbstractItemModel* model, const 
QModelIndex & index ) const {
   QLineEdit* theEditor = qobject_cast < QLineEdit* > (editor);
   model->setData (index, theEditor->text ());
  }
...
};

QTableView* view = new QTableView (0);
...
view->setItemDelegate (new customEditor); // or 
setItemDelegateForColumn, or setItemDelegateForRow


I am a Chinese-speaking people.i don't know do you understand my 
chinese-style english. Hope you can understand.



More information about the Qt-interest-old mailing list