Search code examples
c++qtactionqlistwidget

adding an action to a QListWidget


I try to add actions to an QListWidget like this:

toolItems = new QListWidget(this);
toolItems->addAction(ui->itemLight);
toolItems->addAction(ui->itemDarkLight);
toolItems->addAction(ui->itemCameraPos);
toolItems->addAction(ui->itemCamera);
toolItems->addAction(ui->itemRounded);
toolItems->addAction(ui->itemLightbulb);
toolItems->addAction(ui->itemCommentOnScreen);

But the problem is that this displays nothing, but all actions have text?! How I can fix this?


Solution

  • It is OK, because QListWidget does not support adding actions. You need to read documentation and to use QListWidget::addItem method.

    But you may create items from actions:

    QListWidgetItem *createItemFromAction( const QAction* action )
    {
      Q_ASSERT( action );
      QListWidgetItem *item = new QListWidgetItem();
      item->setText( action->text() );
      item->setToolTip( action->toolTip() );
      item->setIcon( action->icon() );
      // ...
      return item;
    }
    //...
    toolItems->addAction( createItemFromAction( ui->itemCommentOnScreen ) );