【问题标题】:Qt - how to access QComboBox data inserted in QTreeWidgetQt - 如何访问 QTreeWidget 中插入的 QComboBox 数据
【发布时间】:2013-01-07 13:12:02
【问题描述】:

在运行时,我在我的 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。 解决这个问题?

【问题讨论】:

    标签: qt qt4


    【解决方案1】:

    QComboBox *box = (QComboBox*)item->data(COLUMN_2, 0); 当我阅读这段代码时,我进入了恐慌模式。看签名:

    QVariant QTreeWidgetItem::data ( int column, int role ) const
    

    你使用setItemWidget,你应该使用

    QWidget * QTreeWidget::itemWidget ( QTreeWidgetItem * item, int column ) const
    

    ps:如果要转换,请使用 C++ 转换。更好的是,将qobject_cast<SubtypeofQObjectPtr> 用于QObject。强制转换无效时返回 null。

    确实,我的意思是使用类似于以下的调用检索组合框:

    QComboBox* box = qobject_cast<QComboBox*>(treeWidget->itemWidget(item, column));
    

    【讨论】:

    • 谢谢,但您的意思不是说您可以从 qwidget 中获取组合框 currentText 吗?
    • @jaguzu 简单地投射它...QComboBox* box = qobject_cast&lt;QComboBox&gt;(treeWidget-&gt;itemWidget(item, column));
    【解决方案2】:

    在上面的@Umnyobe 和@Zaiborg 的帮助下解决了这个问题。这是一个完整的工作示例:

    使用 column1 中的文本和 column2 中的 QComboBox 初始化 QTreeWidget:

    //global defines
    #define COLUMN_1 (0)
    #define COLUMN_2 (1)
    
    QTreeWidgetItem *item = new QTreeWidgetItem(_myTreeWidgetPtr);//item to put in tree
    item->setText(COLUMN_1,"animal");                             //item for column 1 in the tree.
    
    QComboBox *box = new QComboBox();
    box->addItem("mouse");                                        //adds selections for comboboxes
    box->addItem("cat");
    box->addItem("dog");
    
    _myTreeWidgetPtr->setItemWidget(item, COLUMN_2, box);           //insert items in tree.
    

    从树中读取值:

    QTreeWidgetItemIterator it(_myTreeWidgetPtr);
    while(*it)
    {
        QTreeWidgetItem *item = *it;
    
        //Init pointer to current combobox
        QComboBox* box = qobject_cast<QComboBox*>(_myTreeWidgetPtr->itemWidget(item, COLUMN_2)); 
    
        //Get data from QTreeWidget
        QString col1Str = item->text(COLUMN_LOGGER);   
        QString col2Str = box->currentText();
    
        it++;
    }
    

    希望它可以帮助某人:)

    【讨论】:

      【解决方案3】:

      使用QSignalMapper 类收集treewidget 中的不同框。

      然后将QSignalMapper::mapped() 信号连接到某个插槽并使用组合框

      编辑:

      QSignalMapper* mapper = new QSignalMapper(this);
      QComboBox *box = new QComboBox();   
      connect( box, SLOT(/*whatever*/), mapper, SLOT( map() ) );
      mapper->setMapping( box );
      myTreeWidget->setItemWidget(item, 1, comboBox);
      

      【讨论】:

      • 谢谢!是的,我正在考虑保存指向组合框的指针,但我希望可以通过 treewidget 访问它们。
      • 保存指针可能是一个“简单”的解决方案,但请考虑重新填充树。您存储了无效指针的可能性太高了。
      【解决方案4】:

      对于正在寻找 Python 解决方案的任何人, (QTreeWidget 中的 PySide / PyQt QComboBox),这里是:

          item = QTreeWidgetItem(self.treeWidgetAnimals)
          item.setText(0, "animal")
          combo_box = QComboBox()
          combo_box.addItem('mouse')
          combo_box.addItem('cat')
          combo_box.addItem('dog')
          self.treeWidgetAnimals.setItemWidget(item, 1, combo_box)
      

      我一直在寻找几个小时,但没有其他论坛像传递参考“父母”这样的“代表团”:

              item = QTreeWidgetItem (self.myTreeWidgetItemObject)

      如果不传递父级,则不会返回错误但 ComboBox 不会出现在显示 TreeWidget 中。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-15
        • 2013-09-17
        • 1970-01-01
        • 1970-01-01
        • 2018-04-18
        • 1970-01-01
        • 2014-12-15
        • 2019-09-29
        相关资源
        最近更新 更多