mercredi 25 mars 2020

How to add a QAction to a QListWidget

I have the following code:

roslaserscandoialog.h

public:
    explicit ROSLaserScanDialog(QWidget *parent = nullptr);
    ~ROSLaserScanDialog();
    QListWidgetItem *createItemFromAction(const QAction* action);

private slots:
    void on_listWidget_itemClicked(QListWidgetItem *item);

private:
    Ui::ROSLaserScanDialog *ui;
    QAction *mAddMsgs;
    QAction *mDeleteMsgs;

roslaserscandoialog.cpp

ROSLaserScanDialog::ROSLaserScanDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ROSLaserScanDialog)
{
    ui->setupUi(this);
    connect(ui->listWidget,SIGNAL(on_listWidget_itemClicked(QListWidgetItem*)),this,SLOT(createItemFromAction(QListWidgetItem*)));

}


QListWidgetItem *ROSLaserScanDialog::createItemFromAction(const QAction *action)
{
    Q_ASSERT( action );
    QListWidgetItem *mAddMsgs = new QListWidgetItem();
    mAddMsgs->setText( action->text() );
    mAddMsgs->setToolTip( action->toolTip() );
    mAddMsgs->setIcon( action->icon() );
    // ...
    return mAddMsgs;
}

void ROSLaserScanDialog::on_listWidget_itemClicked(QListWidgetItem *item)
{
    mAddMsgs = new QAction(QIcon(":ros.png"), tr("Add New Message"), this);
    mDeleteMsgs = new QAction(QIcon(":remove_item.png"), tr("Remove Message"), this);
}

What I have done so far:

I came across this post and also this one. Very useful as I set up my project in a very similar way, but nothing happens when I try to click on the QListWidget.

I know that in order to trigger the action I have to go to the slot called itemClicked as I did on the above code provided. On the official documentation I was trying to apply what is advised but I don't know why nothing happens.

Please point to the right direction for solving this problem.

Aucun commentaire:

Enregistrer un commentaire