- 很多时候我们想通过cellwidget获取对应的QTableWidget的行列号,下面给一个简单的额demo:
首先是CellWidget的代码
cellwidget.h
#pragma once
#include
#include
#include
class CellWidget : public QWidget
{
Q_OBJECT
public:
signals:
void firstButtonClicked();
void secondButtonClicked();
public:
CellWidget(QWidget *parent = 0);
~CellWidget();
private:
void buttonFirstClicked(bool clicked = false);
void buttonSecondClicked(bool clicked = false);
private:
QHBoxLayout *_layout;
QPushButton *_first_button;
QPushButton *_second_button;
};
CellWidget.cpp
#include "CellWidget.h"
CellWidget::CellWidget(QWidget *parent):
QWidget(parent),
_layout(new QHBoxLayout(this)),
_first_button(new QPushButton("first",this)),
_second_button(new QPushButton("second",this))
{
this->setLayout(_layout);
_layout->addWidget(_first_button);
_layout->addWidget(_second_button);
_layout->setContentsMargins(0, 0, 0, 0);
this->setContentsMargins(0, 0, 0, 0);
QObject::connect(_first_button, &QPushButton::clicked, this, &CellWidget::buttonFirstClicked);
QObject::connect(_second_button, &QPushButton::clicked, this, &CellWidget::buttonSecondClicked);
}
CellWidget::~CellWidget()
{
}
void CellWidget::buttonFirstClicked(bool clicked )
{
emit firstButtonClicked();
}
void CellWidget::buttonSecondClicked(bool clicked )
{
emit secondButtonClicked();
}
这里是主窗口Widget的代码
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include
#include
#include
#include
#include
#include "CellWidget.h"
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = 0);
~Widget();
void setTableRowCountAndColumnCount(int row_count, int column_count);
void appendCellWidget(CellWidget *widget);
private:
void AButtonClicked();
void BButtonClicked();
private:
QVBoxLayout *_layout;
QTableWidget *_table_widget;
};
#endif
widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent),
_layout(new QVBoxLayout(this)),
_table_widget(new QTableWidget(this))
{
this->setLayout(_layout);
_layout->addWidget(_table_widget);
}
Widget::~Widget()
{
}
void Widget::setTableRowCountAndColumnCount(int row_count, int column_count)
{
_table_widget->setRowCount(row_count);
_table_widget->setColumnCount(column_count);
}
void Widget::appendCellWidget(CellWidget *widget)
{
_table_widget->insertRow(_table_widget->rowCount());
int row_count = _table_widget->rowCount();
_table_widget->setCellWidget(row_count - 1, 0, widget);
QObject::connect(widget, &CellWidget::firstButtonClicked, this, &Widget::AButtonClicked);
QObject::connect(widget, &CellWidget::secondButtonClicked, this, &Widget::BButtonClicked);
}
void Widget::AButtonClicked()
{
auto sender = qobject_cast<CellWidget*>(QObject::sender());
if (sender)
{
auto index = _table_widget->indexAt(sender->pos());
QString message = QString("row : %1, column : %2").arg(index.row()).arg(index.column());
QMessageBox::warning(NULL, "", message, NULL, NULL);
}
}
void Widget::BButtonClicked()
{
auto sender = qobject_cast<CellWidget*>(QObject::sender());
if (sender)
{
auto index = _table_widget->indexAt(sender->pos());
QString message = QString("row : %1, column : %2").arg(index.row()).arg(index.column());
QMessageBox::warning(NULL, "", message, NULL, NULL);
}
}