Inserire QPushButton in QTableWidget

Mattepuffo's logo
Inserire QPushButton in QTableWidget

Inserire QPushButton in QTableWidget

Sostanzialmente riempio una QTableWidget da database, e voglio mettere un QPushButton nelll'ultima colonna (il classico tasto per cancellazione).

Non andremo a vedere come reperisco i dati da db, ma diamo per scontato di avere una QList perfettamente riempita.

Nel nostro file di intestazione andiamo ad aggiungere questo:

private slots:
    void delSync(QTableWidgetItem *item);

private:
    void createMainLayout();

Uno è lo SLOT intercettato dal tasto, al quale passiamo l'item selezionato; l'altro è un metodo richiamato dal costruttore della finestra, e che si preoccupa di costruire il layout.

Eccoli qua sotto:

void MainWindow::createMainLayout() {
    QStringList header;
    header << "ID"
           << "NOME"
           << "COMANDO"
           << "SOURCE"
           << "DESTINAZIONE"
           << "";

    tbl = new QTableWidget();
    tbl->setColumnCount(header.length());
    tbl->horizontalHeader()->show();
    tbl->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    tbl->verticalHeader()->setVisible(false);
    tbl->setHorizontalHeaderLabels(header);
    tbl->setContextMenuPolicy(Qt::CustomContextMenu);
    tbl->setEditTriggers(QAbstractItemView::NoEditTriggers);

    int rows = 0;
    QList<Sync> list = db.getAll();
    QList<Sync>::const_iterator iter;
    for (iter = list.constBegin(); iter != list.constEnd(); ++iter) {
        const Sync &sync = *iter;
        tbl->insertRow(tbl->rowCount());
        tbl->setItem(rows, 0, new QTableWidgetItem(QString::number(sync.getId())));
        tbl->setItem(rows, 1, new QTableWidgetItem(sync.getNome()));
        tbl->setItem(rows, 2, new QTableWidgetItem(sync.getComando()));
        tbl->setItem(rows, 3, new QTableWidgetItem(sync.getSource()));
        tbl->setItem(rows, 4, new QTableWidgetItem(sync.getDestination()));

        QPushButton *btnCancella = new QPushButton("Cancella");
        btnCancella->setStyleSheet("QPushButton {"
                                   "background-color: red"
                                   "}");
								   
        tbl->setIndexWidget(tbl->model()->index(rows, 5), btnCancella);

        connect(btnCancella, &QPushButton::clicked, this, std::bind(&MainWindow::delSync, 
					this, tbl->item(rows, 0)));

        rows++;
    }
    this->setCentralWidget(tbl);
}

void MainWindow::delSync(QTableWidgetItem *item) {
    qDebug() << item->text();
}

Per completezza vi metto anche il mio oggetto Sync (che rappresenta la tabella sul db):

#ifndef SYNC_H
#define SYNC_H

#include <QObject>

class Sync {

public:
    Sync();
    Sync(const Sync &other) = default;
    Sync &operator = (const Sync &other) = default;
    virtual ~Sync();

    int getId() const;
    void setId(const int &value);
    QString getNome() const;
    void setNome(const QString &value);
    QString getComando() const;
    void setComando(const QString &value);
    QString getSource() const;
    void setSource(const QString &value);
    QString getDestination() const;
    void setDestination(const QString &value);

private:
    int id;
    QString nome;
    QString comando;
    QString source;
    QString destination;
};

#endif // SYNC_H

Enjoy!


Condividi

Commentami!