JustPaste.it


```
#ifndef MYWINDOW_H
#define MYWINDOW_H

#include <QDialog>
#include <QTableView>
#include <QStandardItemModel>
#include <QItemSelectionModel>
#include <QItemSelection>
#include <QPixmap>
#include <QList>

class MyWindow : public QDialog {
    Q_OBJECT

public:
    explicit MyWindow(QWidget *parent = nullptr); // Constructor
    void updateImage(int row, int column, const QString& imagePath); // Function to update image
    QList<int> getSelectedColumns() const; // Function to get selected column indices

private slots:
    void onSelectionChanged(); // Slot to handle selection changes

private:
    QTableView* tableView; // Table view to display text and images
    QStandardItemModel* model; // Model to hold data for the table
    QItemSelectionModel* selectionModel; // Selection model to track selections
};

#endif // MYWINDOW_H
```

```
MyWindow::MyWindow(QWidget *parent) : QDialog(parent) {
    // Set up the table view and model
    tableView = new QTableView(this);
    model = new QStandardItemModel(10, 4, this); // 10 rows and 4 columns
    tableView->setModel(model);

    // Set column headers
    model->setHorizontalHeaderLabels({"Text 1", "Image 1", "Text 2", "Image 2"});

    // Initialize table with placeholder text and images
    for (int row = 0; row < 10; ++row) {
        for (int column = 0; column < 4; ++column) {
            if (column % 2 == 0) { // Even columns: Text
                QStandardItem* item = new QStandardItem(QString("Text %1").arg(row * 2 + column / 2 + 1));
                model->setItem(row, column, item);
            } else { // Odd columns: Image
                QStandardItem* item = new QStandardItem();
                item->setData(QVariant(QPixmap()), Qt::DecorationRole); // Set placeholder image
                model->setItem(row, column, item);
            }
        }
    }

    // Set layout
    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addWidget(tableView);
    setLayout(layout);

    // Set up selection model and connect the selectionChanged signal
    selectionModel = tableView->selectionModel();
    connect(selectionModel, &QItemSelectionModel::selectionChanged, this, &MyWindow::onSelectionChanged);
}

void MyWindow::updateImage(int row, int column, const QString& imagePath) {
    if (row >= 0 && row < model->rowCount() && column >= 0 && column < model->columnCount() && column % 2 == 1) {
        QPixmap pixmap(imagePath);
        if (!pixmap.isNull()) {
            QStandardItem* item = model->item(row, column);
            item->setData(QVariant(pixmap), Qt::DecorationRole); // Update image
        } else {
            qDebug() << "Image not found";
        }
    } else {
        qDebug() << "Row or column out of bounds, or not an image column";
    }
}

QList<int> MyWindow::getSelectedColumns() const {
    QList<int> selectedColumns;
    QModelIndexList indexes = selectionModel->selectedColumns();
    for (const QModelIndex &index : indexes) {
        selectedColumns.append(index.column());
    }
    return selectedColumns;
}

void MyWindow::onSelectionChanged() {
    // Get selected columns and print them
    QList<int> selectedColumns = getSelectedColumns();
    qDebug() << "Selected columns:" << selectedColumns;
}
```