QTableView为每一行添加委托

在这里插入图片描述

#include <QtGui>

//编号列,只读委托
//这个方法我还真想不到,呵呵
class ReadOnlyDelegate : public QItemDelegate
{
	Q_OBJECT
public:
	ReadOnlyDelegate(QObject *parent = 0): QItemDelegate(parent) { }
	QWidget *createEditor(QWidget*parent, const QStyleOptionViewItem &option,
		const QModelIndex &index) const
	{
		return NULL;
	}
};

//ID列,只能输入1-12个数字
//利用QLineEdit委托和正则表达式对输入进行限制
class UserIDDelegate : public QItemDelegate
{
	Q_OBJECT
public:
	UserIDDelegate(QObject *parent = 0): QItemDelegate(parent) { }
	QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
		const QModelIndex &index) const
	{
		QLineEdit *editor = new QLineEdit(parent);
		QRegExp regExp("[0-9]{0,10}");
		editor->setValidator(new QRegExpValidator(regExp, parent));
		return editor;
	}
	void setEditorData(QWidget *editor, const QModelIndex &index) const
	{
		QString text = index.model()->data(index, Qt::EditRole).toString();
		QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
		lineEdit->setText(text);
	}
	void setModelData(QWidget *editor, QAbstractItemModel *model,
		const QModelIndex &index) const
	{
		QLineEdit *lineEdit = static_cast<QLineEdit*>(editor);
		QString text = lineEdit->text();
		model->setData(index, text, Qt::EditRole);
	}
	void updateEditorGeometry(QWidget *editor,
		const QStyleOptionViewItem &option, const QModelIndex &index) const
	{
		editor->setGeometry(option.rect);
	}
};

//年龄列,利用QSpinBox委托进行输入限制,只能输入1-100之间的数字
class AgeDelegate : public QItemDelegate
{
	Q_OBJECT
public:
	AgeDelegate(QObject *parent = 0): QItemDelegate(parent) { }
	QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
		const QModelIndex &index) const
	{
		QSpinBox *editor = new QSpinBox(parent);
		editor->setMinimum(1);
		editor->setMaximum(100);
		return editor;
	}
	void setEditorData(QWidget *editor, const QModelIndex &index) const
	{
		int value = index.model()->data(index, Qt::EditRole).toInt();
		QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
		spinBox->setValue(value);
	}
	void setModelData(QWidget *editor, QAbstractItemModel *model,
		const QModelIndex &index) const
	{
		QSpinBox *spinBox = static_cast<QSpinBox*>(editor);
		spinBox->interpretText();
		int value = spinBox->value();
		model->setData(index, value, Qt::EditRole);
	}
	void updateEditorGeometry(QWidget *editor,
		const QStyleOptionViewItem &option, const QModelIndex &index) const
	{
		editor->setGeometry(option.rect);
	}
};

//性别列,利用QComboBox委托对输入进行限制
//这一列的单元格只能输入Male或Female
class SexDelegate : public QItemDelegate
{
	Q_OBJECT
public:
	SexDelegate(QObject *parent = 0): QItemDelegate(parent) { }
	QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option,
		const QModelIndex &index) const
	{
		QComboBox *editor = new QComboBox(parent);
		editor->addItem("Female");
		editor->addItem("Male");
		return editor;
	}
	void setEditorData(QWidget *editor, const QModelIndex &index) const
	{
		QString text = index.model()->data(index, Qt::EditRole).toString();
		QComboBox *comboBox = static_cast<QComboBox*>(editor);
		int tindex = comboBox->findText(text);
		comboBox->setCurrentIndex(tindex);
	}
	void setModelData(QWidget *editor, QAbstractItemModel *model,
		const QModelIndex &index) const
	{
		QComboBox *comboBox = static_cast<QComboBox*>(editor);
		QString text = comboBox->currentText();
		model->setData(index, text, Qt::EditRole);
	}
	void updateEditorGeometry(QWidget *editor,
		const QStyleOptionViewItem &option, const QModelIndex &index) const
	{
		editor->setGeometry(option.rect);
	}
};

//头像列,只是在单元格中央放一张小图而已
class IconDelegate : public QItemDelegate
{
	Q_OBJECT
public:
	IconDelegate(QObject *parent = 0): QItemDelegate(parent) { }
	void paint(QPainter *painter, const QStyleOptionViewItem &option,
		const QModelIndex & index ) const
	{
		//show.bmp是在工程目录中的一张图片(其实就是QQ的图标啦,呵呵)
		QPixmap pixmap = QPixmap("show.bmp").scaled(24, 24);
		qApp->style()->drawItemPixmap(painter, option.rect,  Qt::AlignCenter, QPixmap(pixmap));
	}
};

//代理类,把所有单元格中的字符居中显示
class VIPModel : public QStandardItemModel
{
	Q_OBJECT
public:
	VIPModel(QObject *parent=NULL) : QStandardItemModel(parent) { }
	VIPModel(int row, int column, QObject *parent=NULL)
		: QStandardItemModel(row, column, parent) { }
	QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const
	{
		if( Qt::TextAlignmentRole == role )
			return Qt::AlignCenter; 
		return QStandardItemModel::data(index, role);
	}

};

#include "main.moc"

int main(int argc, char *argv[])
{
	QApplication app(argc, argv);

	VIPModel *model = new VIPModel(5, 5);
	QTableView *tableView = new QTableView;

	//把表格的背景调成黄蓝相间
	//这种方法是在网上看到的,用起来还真方便啊
	tableView->setAlternatingRowColors(true);
	tableView->setStyleSheet("QTableView{background-color: rgb(250, 250, 115);"
		"alternate-background-color: rgb(141, 163, 215);}");

	tableView->setWindowTitle("VIP List");
	tableView->resize(700, 400);
	tableView->setModel(model);
	QStringList headerList;
	headerList << "No." << "ID" << "Name" << "Age" << "Sex" << "Show";
	model->setHorizontalHeaderLabels(headerList);
	tableView->verticalHeader()->setVisible(false);
	tableView->horizontalHeader()->setStretchLastSection(true);

	//为每一列加载委托
	ReadOnlyDelegate readOnlyDelegate;
	tableView->setItemDelegateForColumn(0, &readOnlyDelegate);
	UserIDDelegate userIDDelegate;
	tableView->setItemDelegateForColumn(1, &userIDDelegate);
	AgeDelegate spinBoxDelegate;
	tableView->setItemDelegateForColumn(3, &spinBoxDelegate);
	SexDelegate comboBoxDelegate;
	tableView->setItemDelegateForColumn(4, &comboBoxDelegate);
	IconDelegate iconDelegate;
	tableView->setItemDelegateForColumn(5, &iconDelegate);

	for(int i=0; i<10; i++)
	{
		QModelIndex index = model->index(i, 0, QModelIndex());
		model->setData(index, i);
	}

	tableView->show();
	return app.exec();
}

### 回答1: 您可以通过 QStyledItemDelegate 类来实现 QTableView一行的颜色改变。以下是一个简单的示例代码: ```cpp class MyDelegate : public QStyledItemDelegate { public: MyDelegate(QObject *parent = nullptr) : QStyledItemDelegate(parent) {} void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override { // 获取表格中该行的索引 int row = index.row(); // 获取表格中该行某一列的值 QVariant value = index.model()->data(index.model()->index(row, 0)); // 判断该行某一列的值是否满足特定条件,如果满足则改变该行的背景色 if (value.toString() == "需要改变颜色的值") { painter->fillRect(option.rect, QColor(Qt::yellow).lighter(160)); } else { QStyledItemDelegate::paint(painter, option, index); } } }; ``` 然后,将该委托类对象设置为 QTableView委托即可: ```cpp MyDelegate *delegate = new MyDelegate(this); ui->tableView->setItemDelegate(delegate); ``` 当表格中某一行的某一列的值为特定值时,该行的背景色将会被更改为黄色。您可以根据实际需求更改颜色和判断条件。 ### 回答2: 要改变QTableView中某一行的颜色,可以通过自定义QItemDelegate来实现。 首先,我们需要创建一个继承自QItemDelegate的子类,重写paint()函数。在paint()函数中,我们可以通过QStyleOptionViewItem参数来获取当前单元格的信息,包括所在的行和列。然后,我们可以根据行号来判断是否要改变该行的颜色,如果需要改变颜色,就使用QStylePainter类来绘制背景色。最后,调用父类的paint()函数来绘制其他内容。 接下来,在主程序中创建一个QTableView对象,并设置自定义的QItemDelegate作为它的itemDelegate。然后,通过调用setModel()函数设置数据模型,并通过调用setItemDelegateForRow()函数为需要改变颜色的行设置QItemDelegate。 最后,在自定义的QItemDelegate类中添加setRowColor()函数,用于设置需要改变颜色的行的行号,并在paint()函数中根据该行号来判断是否绘制背景色。 以下是实现该功能的代码示例: ```cpp #include <QApplication> #include <QTableView> #include <QStandardItemModel> #include <QItemDelegate> #include <QPainter> class RowColorDelegate : public QItemDelegate { public: explicit RowColorDelegate(QObject *parent = nullptr) : QItemDelegate(parent), m_rowColor(-1) {} void setRowColor(int row) { m_rowColor = row; } void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override { if (index.row() == m_rowColor) { QStyleOptionViewItem opt(option); opt.backgroundBrush = QBrush(Qt::red); // 设置背景色为红色 QStylePainter stylePainter(painter); stylePainter.drawControl(QStyle::CE_ItemViewItem, opt); // 绘制背景色 } QItemDelegate::paint(painter, option, index); // 绘制其他内容 } private: int m_rowColor; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); QTableView tableView; QStandardItemModel model(5, 3); // 创建一个5行3列的数据模型 tableView.setModel(&model); RowColorDelegate delegate; tableView.setItemDelegate(&delegate); delegate.setRowColor(2); // 设置第2行为红色 tableView.show(); return a.exec(); } ``` 在上述示例中,我们创建一个5行3列的QStandardItemModel作为数据模型,并将其设置给QTableView。然后,我们使用自定义的RowColorDelegate类作为tableView的itemDelegate,并通过调用setRowColor()函数将第2行设置为红色。 运行程序后,我们可以看到QTableView中第2行的背景色变为了红色。这样,我们就成功地实现了使用QTableView改变某一行的颜色的功能。 ### 回答3: 要改变QTableView中某一行的颜色,需要以下几个步骤实现: 1. 首先,我们需要自定义一个QStyledItemDelegate类,并重写它的paint方法。在paint方法中判断当前正在绘制的单元格所在的行是否是目标行,如果是则改变绘制颜色,否则继续使用默认颜色绘制。 2. 然后,我们需要为QTableView设置一个自定义的数据模型,并在该模型中添加一个方法用于设置目标行的行号。在该方法中,将传入的行号保存起来。 3. 在QTableView中的行绘制事件中,通过模型调用设置目标行行号的方法,并通过QStyledItemDelegate类为QTableView的每个单元格插入自定义的绘制逻辑。 以下是示例代码: ```python import sys from PyQt5.QtWidgets import QApplication, QTableView from PyQt5.QtGui import QStandardItemModel, QStandardItem, QStyledItemDelegate from PyQt5.QtCore import Qt, QStyleOptionViewItem class MyDelegate(QStyledItemDelegate): def __init__(self, parent=None): super().__init__(parent) def paint(self, painter, option, index): # 判断当前绘制的单元格所在行是否是目标行 if index.row() == self.parent().target_row: option.palette.setColor(QPalette.Highlight, QColor(Qt.red)) # 改变目标行的颜色为红色 super().paint(painter, option, index) # 继续使用默认的绘制逻辑 class MyModel(QStandardItemModel): def __init__(self, parent=None): super().__init__(parent) self.target_row = -1 # 目标行的行号,默认为-1表示没有目标行 def set_target_row(self, row): self.target_row = row class MyTableView(QTableView): def __init__(self, parent=None): super().__init__(parent) def drawRow(self, painter, options, index): self.openPersistentEditor(index) # 打开永久编辑器,以便能够绘制自定义的单元格 super().drawRow(painter, options, index) if __name__ == "__main__": app = QApplication(sys.argv) table = MyTableView() # 创建数据模型 model = MyModel() table.setModel(model) # 添加数据 model.appendRow([QStandardItem("列1"), QStandardItem("列2"), QStandardItem("列3")]) # 设置目标行 model.set_target_row(0) # 第一行改变颜色 # 设置绘制代理 delegate = MyDelegate(table) table.setItemDelegate(delegate) table.show() sys.exit(app.exec_()) ``` 上述代码中,我们自定义了一个QStyledItemDelegate类,并重写了它的paint方法。在这个方法中,我们通过判断正在绘制的单元格所在的行是否是目标行来决定是否改变绘制颜色。 然后,我们自定义了一个数据模型类MyModel,在该类中添加了set_target_row方法,用于设置目标行。在这个方法中,我们将传入的行号保存起来。 最后,在QTableView的行绘制事件中,我们调用模型的set_target_row方法设置目标行,并通过QStyledItemDelegate类为每个单元格插入了自定义的绘制逻辑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值