Qt 子类 QStyledItemDelegate 绘制方法永远不会被调用

发布于 2024-11-04 00:46:18 字数 2060 浏览 4 评论 0原文

更新:我的绘制方法不是 const

我有一个连接到 QAbstractTableModel 的自定义 QTableView

我最近创建了一个 QStyledItemDelegate 子类,其 Paint 方法打印一条调试消息,然后调用父级 Paint,然后打印另一条调试消息。

我已调用 view->setItemDelegate 并检查了我视图中的项目委托是否是我给它的委托(只是为了确定)。

但是,我的程序只是以与之前相同的方式呈现内容,并且从不在 paint 中打印调试消息。它确实在委托的构造函数中打印一条调试消息,所以我知道如果该函数被调用,它应该打印。

这是我的代码的精简版本,请让我知道其中之一:

  1. 你能看到哪里出了问题吗?
  2. 我如何调试这样的 Qt 类?
  3. 我还应该发布哪些其他相关代码来诊断此问题?
  4. 我可以尝试一些常见的解决方案吗?

谢谢!

这是整个委托 .h 和 .cpp 文件(减去包含/命名空间),

class QtCellItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT;
public:
    QtCellItemDelegate( QWidget *parent = NULL );

    void paint( QPainter *painter,
                const QStyleOptionViewItem &option,
                const QModelIndex &index );

    QSize sizeHint( const QStyleOptionViewItem &option,
                    const QModelIndex & index ) const;

    virtual ~QtCellItemDelegate();
};

QtCellItemDelegate::QtCellItemDelegate( QWidget *parent ) :
    QStyledItemDelegate( parent )
{
    qDebug() << "CONSTRUCT";
}

void QtCellItemDelegate::paint( QPainter *painter,
                                const QStyleOptionViewItem &option,
                                const QModelIndex &index )
{
    painter->save();
    qDebug() << "Begin Paint";
    QStyledItemDelegate::paint( painter, option, index );

    qDebug() << "   Begin custom paint";
    // omitted...

    qDebug() << "End paint";
    painter->restore();
}

QSize QtCellItemDelegate::sizeHint( const QStyleOptionViewItem &option,
                                     const QModelIndex & index ) const 
{
    return QSize( 60, 60 );
}

QtCellItemDelegate::~QtCellItemDelegate()
{
    qDebug() << "Destroy delegate!";
}

因此调试输出告诉我调用了构造函数(但不是析构函数...),并且我实际上正在为视图设置相同的委托我想我是。然而,paint 中的消息永远不会被打印。

Update: my paint method was not const

I have a custom QTableView connected to a QAbstractTableModel.

I recently created a QStyledItemDelegate subclass and its paint method prints a debugging message and then it calls the parent paint and then it prints another debugging message.

I have called view->setItemDelegate and I have checked that the item delegate in my view is the one I gave it (just to be sure).

However, my program just renders things the same way it did before and never prints the debug message in paint. It does print a debug message in the delegate's constructor, so I know it should be printing if that function ever gets called.

Here is a stripped down version of my code, let me know one of these:

  1. Can you see what is wrong?
  2. How I can debug Qt classes like this?
  3. What other relevant code I should post to diagnose this problem?
  4. Is there some common solution I could try?

Thanks!

Here is the entire delegate .h and .cpp files (minus includes/namespace)

class QtCellItemDelegate : public QStyledItemDelegate
{
    Q_OBJECT;
public:
    QtCellItemDelegate( QWidget *parent = NULL );

    void paint( QPainter *painter,
                const QStyleOptionViewItem &option,
                const QModelIndex &index );

    QSize sizeHint( const QStyleOptionViewItem &option,
                    const QModelIndex & index ) const;

    virtual ~QtCellItemDelegate();
};

QtCellItemDelegate::QtCellItemDelegate( QWidget *parent ) :
    QStyledItemDelegate( parent )
{
    qDebug() << "CONSTRUCT";
}

void QtCellItemDelegate::paint( QPainter *painter,
                                const QStyleOptionViewItem &option,
                                const QModelIndex &index )
{
    painter->save();
    qDebug() << "Begin Paint";
    QStyledItemDelegate::paint( painter, option, index );

    qDebug() << "   Begin custom paint";
    // omitted...

    qDebug() << "End paint";
    painter->restore();
}

QSize QtCellItemDelegate::sizeHint( const QStyleOptionViewItem &option,
                                     const QModelIndex & index ) const 
{
    return QSize( 60, 60 );
}

QtCellItemDelegate::~QtCellItemDelegate()
{
    qDebug() << "Destroy delegate!";
}

So the debugging output tells me the constructor is called (but not the destructor...) and that I am actaully setting the same deleagate to the view as I think I am. However, the messages in paint never get printed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤城病女 2024-11-11 00:46:18

我认为你的问题是你没有将子类的paint方法声明为const。因此,它不会覆盖超类方法(常量和非常量方法被认为具有彼此不同的签名),这就是您的绘制方法不会被调用的原因。

I think your problem is that you are not declaring your subclass's paint method as const. As such, it's not overriding the superclass method (const and non-const methods are considered to have different signatures from each other) and that's why your paint method doesn't get called.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文