QTableView设置表格左上的文字

本文介绍如何在QTableView中设置表格左上角的文字,通过创建一个继承自QTableView的类并实现自定义绘制来实现该功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

设置文字和设置按钮或者其他控件都是可以的
QTableView设置表格左上的文字主要方法是继承后通过绘制在左上角
在这里插入图片描述
首先写一个类继承QTableView
class GCornerTableView : public QTableView
GCornerTableView.h

#ifndef GCORNERTABLEVIEW_H
#define GCORNERTABLEVIEW_H

#include <QPainter>
#include <QTableView>
#include <QAbstractButton>

class GCornerTableView : public QTableView
{
    Q_OBJECT

public:
    explicit GCornerTableView(QWidget *parent = 0);
    void setCornerButtonText(const QString &text);
    void setCornerTextAlignment(Qt::Alignment align);

private:
    QString m_cornerButtonText;

    Q_DECLARE_PRIVATE(QTableView)
};


class GTableCornerButton : public QAbstractButton
{
    Q_OBJECT

public:
    GTableCornerButton(QWidget *parent = 0)
        : QAbstractButton(parent)
    {

    }

    void paintEvent(QPaintEvent*)
    {
        QStyleOptionHeader opt;
        opt.initFrom(this);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

vqt5_qt6

你的鼓励是我们创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值