Qt: cellWidget 如何获取 对应的 QtableWidget所在的行列

  • 很多时候我们想通过cellwidget获取对应的QTableWidget的行列号,下面给一个简单的额demo:

首先是CellWidget的代码

cellwidget.h

#pragma once
#include 
#include 
#include 
class CellWidget : public QWidget
{
	//在这里定义信号,所谓信号,本身是没有实现的,只有签名,是emit出去的
	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);//layout填满整个widget
	this->setContentsMargins(0, 0, 0, 0);//widget尽量填满他的父控件

	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 /*= false*/)
{
	//注意信号的发送者就是CellWidget,而不是CellWidget的_first_button
	emit firstButtonClicked();
}

void CellWidget::buttonSecondClicked(bool clicked /*= false*/)
{
	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_H

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);
	}
}

你可能感兴趣的:(Qt)