{
Q_OBJECT
public:
enum Id
{
Row = Qt::UserRole + 1,
Col
};
explicit TableWidget
(QWidget * parent
= 0) {
setRowCount(5);
setColumnCount(2);
for(int row = 0; row < rowCount(); ++row)
{
for(int col = 0; col < columnCount(); ++col)
{
if(col == 0)
{
comboBox
->insertItems
(0,
QString("1,2,3,4,5,6").
split(","));
comboBox->setItemData(0, row, Row);
comboBox->setItemData(0, col, Col);
connect(comboBox,
SIGNAL(currentIndexChanged
(QString)),
SLOT(onComboChanged
(QString)));
setCellWidget(row, 0, comboBox);
}
}
}
show();
}
private slots:
void onComboChanged
(const QString & text
) {
QComboBox * comboBox
= dynamic_cast<QComboBox
*>
(sender
());
if(comboBox)
{
int row = comboBox->itemData(0, Row).toInt();
int col = comboBox->itemData(0, Col).toInt();
item(row, col + 1)->setText(text);
}
}
};
class TableWidget : public QTableWidget
{
Q_OBJECT
public:
enum Id
{
Row = Qt::UserRole + 1,
Col
};
explicit TableWidget(QWidget * parent = 0)
: QTableWidget(parent)
{
setRowCount(5);
setColumnCount(2);
for(int row = 0; row < rowCount(); ++row)
{
for(int col = 0; col < columnCount(); ++col)
{
setItem(row, col, new QTableWidgetItem());
if(col == 0)
{
QComboBox * comboBox = new QComboBox();
comboBox->insertItems(0, QString("1,2,3,4,5,6").split(","));
comboBox->setItemData(0, row, Row);
comboBox->setItemData(0, col, Col);
connect(comboBox, SIGNAL(currentIndexChanged(QString)), SLOT(onComboChanged(QString)));
setCellWidget(row, 0, comboBox);
}
}
}
show();
}
private slots:
void onComboChanged(const QString & text)
{
QComboBox * comboBox = dynamic_cast<QComboBox *>(sender());
if(comboBox)
{
int row = comboBox->itemData(0, Row).toInt();
int col = comboBox->itemData(0, Col).toInt();
item(row, col + 1)->setText(text);
}
}
};
To copy to clipboard, switch view to plain text mode
Bookmarks