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

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

首先是CellWidget的代码

cellwidget.h

#pragma once
#include <QWidget>
#include <QPushButton>
#include <QHBoxLayout>
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 <QTableWidget>
#include <QWidget>
#include <QApplication>
#include <QVBoxLayout>
#include <QMessageBox>
#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);
	}
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值