在运行时,我在我的 QTreeWidget 中插入了 QCombobox,如下所示:
//global defines
#define COLUMN_1 (0)
#define COLUMN_2 (1)
//Init QComboBox to QTreeWidget - works fine.
QTreeWidgetItem *item = new QTreeWidgetItem(_myTreeWidget);
item->setText(COLUMN_1,"testing");
QComboBox *box = new QComboBox();
box->addItem("select1");
box->addItem("select2");
box->addItem("select3");
_myTreeWidget->setItemWidget(item, 1, box);
上面的代码有效,但我也想读取这些列中的数据文本。例如。从上面的代码中获取字符串“testing”和“select2”。问题是我不知道如何阅读组合框中的“QComboBox::currentText()”。我试过了:
QTreeWidgetItemIterator it(_myTreeWidget);
while(*it)
{
QTreeWidgetItem *item = *it;
QVariant first = item->text(COLUMN_1);
QString firstStr = loggerName.toString(); //this works
QComboBox *box = (QComboBox*)item->data(COLUMN_2, 0);
QString boxValStr = box->text().toString(); //this doesn't works, always empty string
//... more code to handle strings...
it++;
}
感觉“item->data(COLUMN_2, 0)”是错误的方法,因为它返回一个 QVariant。解决这个问题?