QT开发中文网

no type named'draw'in'QAction'4

对不起,我犯了一个错误。QAction类没有名为draw()的方法。如果您想自定义绘制行为,可以考虑继承自QWidgetAction类并重写其paintEvent()方法。以下是修改后的代码:

#ifndef HIGHLIGHTABLEACTION_H
#define HIGHLIGHTABLEACTION_H

#include <QWidgetAction>
#include <QPainter>

class HighlightableAction : public QWidgetAction {
public:
    HighlightableAction(const QString& text, QObject* parent = nullptr)
        : QWidgetAction(parent), m_text(text) {}

protected:
    void paintEvent(QPaintEvent* event) override {
        QPainter painter(this);
        QStyleOption opt;
        opt.init(this);

        if (isChecked() || isDown()) {
            // 按钮被按下或选中时,设置为灰色背景,并向上突出一点
            opt.state |= QStyle::State_Sunken;
            painter.fillRect(event->rect(), QColor(200, 200, 200)); // 灰色背景颜色
        }

        style()->drawControl(QStyle::CE_ActionButton, &opt, &painter, this);

        QRect textRect = rect().adjusted(4, 4, -4, -4);
        painter.drawText(textRect, Qt::AlignCenter | Qt::TextWordWrap, m_text);
    }

private:
    QString m_text;
};

#endif // HIGHLIGHTABLEACTION_H

这个类继承自 QWidgetAction 类,并重写了 paintEvent() 方法来实现自定义的绘制行为。在 paintEvent() 中,我们使用 QPainter 绘制按钮,并根据按钮的状态来设置背景颜色和绘制文本。

请注意在使用此头文件时要包含相应的 Qt 库头文件,例如 <QWidgetAction><QPainter>


内容由零声教学AI助手提供,问题来源于学员提问

本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
转载请注明出处:https://qt.0voice.com/?id=9117

分享:
扫描分享到社交APP
上一篇
下一篇
发表列表
游客 游客
此处应有掌声~
评论列表

还没有评论,快来说点什么吧~

联系我们

在线咨询: 点击这里给我发消息

微信号:3007537140

上班时间: 10:30-22:30

关注我们
x

注册

已经有帐号?