PDA

View Full Version : QTableWidget combobox



aekilic
10th September 2007, 11:50
Dear All

We have a problem for the QTableWidget.

We have an widget that containes combo boxes in some of the column. What we would like to do is, when we start editing a column it should turn into a combo box, and after we finish, it should turn into a normal qtablewidget item.

Please help us

jpn
10th September 2007, 13:15
Take a look at the spinbox delegate example (http://doc.trolltech.com/4.3/itemviews-spinboxdelegate.html). Provide a combo box editor in the similar way than the example provides a spinbox.

rajesh
10th September 2007, 13:17
You dont keep combo boxes in your QTableWidget initially.
keep one combo boxe pointer seperatally as a member of class with hide.

whenever you click in any cell you do the following:
1. initialize your combo box with your data.
2. take data of that tableWidget's selected cell data and match with combo box data to make it selected in combobox
3. set combo box item in selected cell and show
4. after editing you read data of combo box and place in cell
5. hide your combo box.

aekilic
10th September 2007, 17:15
Dear jpn

For the spin box delegate doesnt help, because the data in the combo changes allways, but the other solution could help

Like the combos should be in the 2nd coloum. how we could send a signal for the second coloum?

jpn
10th September 2007, 17:37
Like the combos should be in the 2nd coloum.
You can create combo box editors to 2nd column like this:


QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() == 1)
{
// 2nd column is being edited, return a QComboBox as an editor
QComboBox *editor = new QComboBox(parent);
...
return editor;
}
return QItemDelegate::createEditor(parent, option, index);
}



For the spin box delegate doesnt help, because the data in the combo changes allways
Sure it does. setEditorData() gets always called for the item in question. Just fill the combo box accordingly:


void ComboBoxDelegate::setEditorData(QWidget *editor,
const QModelIndex &index) const
{
if (index.column() == 1)
{
// 2nd column is being edited, we know it's a QComboBox
// fill the combo box with anything you want
QComboBox *comboBox = static_cast<QComboBox*>(editor);
comboBox->addItem(...);
...
}
else
{
QItemDelegate::setEditorData(editor, index);
}
}

aekilic
14th September 2007, 22:45
Dear Jpn

Could you please help us how we are going to put these into our code? Do we have to write a new cpp?

jpn
14th September 2007, 22:58
Yes, I'd suggest implementing the delegate into separate files. Could you take a look at the spinbox delegate example (http://doc.trolltech.com/4.3/itemviews-spinboxdelegate.html) as already suggested? Just create similar files for the delegate.

aekilic
14th September 2007, 23:42
we have made a new .h and a new .cpp

like


#ifndef TABLEIHALESARTNAMEDELEGATE_H
#define TABLEIHALESARTNAMEDELEGATE_H

#include <QItemDelegate>
#include <QModelIndex>
#include <QObject>
#include <QSize>
#include <QDoubleSpinBox>
#include <QtGui>


class ComboBoxDelegate : public QItemDelegate
{
Q_OBJECT

public:
ComboBoxDelegate(QObject *parent = 0);

QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
const QModelIndex &index) const;
};

#endif



and made our .cpp



#include <QtGui>
#include "tableIhaleSartnameDelegate.h"

QWidget *ComboBoxDelegate::createEditor(QWidget *parent,
const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
if (index.column() == 0)
{
// 2nd column is being edited, return a QComboBox as an editor
QComboBox *editor = new QComboBox(parent);

return editor;
}
return QItemDelegate::createEditor(parent, option, index);
}


after that what we should do?

aekilic
14th September 2007, 23:51
and how are are going to call for our delegate?

marcel
14th September 2007, 23:54
Now you have to use it.


QComboBox *combo = new QComboBox();
combo->setItemDelegate(new ComboBoxDelegate());



You could have seen how to use it if you just looked at the delegate examples.

aekilic
15th September 2007, 00:14
I have tried that but we have an error like

QtCore -lz -lm -lrt -ldl -lpthread
obj/moc_anaPencere.o: In function `formIhale::ihaleSartnameGoster()':
moc_anaPencere.cpp:(.gnu.linkonce.t._ZN9formIhale1 9ihaleSartnameGosterEv+0x4d5): undefined reference to `ComboBoxDelegate::ComboBoxDelegate(QObject*)'
collect2: ld returned 1 exit status
make: *** [Deneme34d] Hata 1

jpn
15th September 2007, 09:30
I have tried that but we have an error like

QtCore -lz -lm -lrt -ldl -lpthread
obj/moc_anaPencere.o: In function `formIhale::ihaleSartnameGoster()':
moc_anaPencere.cpp:(.gnu.linkonce.t._ZN9formIhale1 9ihaleSartnameGosterEv+0x4d5): undefined reference to `ComboBoxDelegate::ComboBoxDelegate(QObject*)'
collect2: ld returned 1 exit status
make: *** [Deneme34d] Hata 1
Implementation body is missing. In another words, there's a declaration but no implementation for ComboBoxDelegate constructor.

aekilic
15th September 2007, 11:36
What should we do?

jpn
15th September 2007, 11:39
Plain declaration of a function is not enough. To be able to use a function you must also implement it. So add an implementation for ComboBoxDelegate constructor to your .cpp file:


ComboBoxDelegate::ComboBoxDelegate(QObject *parent)
QItemDelegate(parent)
{
}

aekilic
17th September 2007, 15:39
Dear All

Thank you very much we have solved our problem. We were able to delegate the combobox!