1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- #include "clickabletableview.h"
- ClickableTableView::ClickableTableView(QWidget *parent)
- : QTableView(parent) {
- // 构造函数的实现
- // // 设置背景颜色
- // QPalette palette = this->palette();
- // palette.setColor(QPalette::Base, Qt::white);
- // palette.setColor(QPalette::AlternateBase, Qt::lightGray);
- // palette.setColor(QPalette::Highlight, QColor(0, 120, 215));
- // palette.setColor(QPalette::HighlightedText, Qt::white);
- // this->setPalette(palette);
- // // 设置网格线颜色
- // this->setGridStyle(Qt::SolidLine);
- // QColor gridColor(Qt::darkGray);
- this->setStyleSheet(QString("QTableView { selection-background-color: blue;}"
- "QTableView QTableCornerButton::section { background: transparent; } "
- "QHeaderView::section { background-color: gray; color: white; padding-left: 4px; padding-right: 4px; } "));
- // // 设置边框
- // this->setFrameShape(QFrame::Box);
- // this->setLineWidth(1);
- // // 设置单元格内边距
- // this->verticalHeader()->setDefaultSectionSize(30);
- // this->horizontalHeader()->setDefaultSectionSize(100);
- // }
- }
- void ClickableTableView::mouseReleaseEvent(QMouseEvent *event) {
- QModelIndex index = indexAt(event->pos());
- if (index.isValid() && index.column() == 8) {
- QRect rect = visualRect(index);
- QRect firstButtonRect = rect.adjusted(0, 0, -rect.width() / 2, 0);
- QRect secondButtonRect = rect.adjusted(rect.width() / 2, 0, 0, 0);
- if (firstButtonRect.contains(event->pos())) {
- // 处理第一个按钮点击事件
- qDebug() << "Button 1 clicked at row:" << index.row();
- displayRowData(index.row());
- } else if (secondButtonRect.contains(event->pos())) {
- // 处理第二个按钮点击事件
- qDebug() << "Button 2 clicked at row:" << index.row();
- displayRowData(index.row());
- }
- }
- QTableView::mouseReleaseEvent(event);
- }
- void ClickableTableView::displayRowData(int row) {
- QAbstractItemModel* model = this->model();
- if (model) {
- QString rowData;
- for (int col = 0; col < model->columnCount(); ++col) {
- QModelIndex index = model->index(row, col);
- QVariant data = model->data(index);
- rowData += QString("Column %1: %2 | ").arg(col).arg(data.toString());
- }
- qDebug() << "Row" << row << "Data:" << rowData;
- }
- }
|