Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使QPalette可访问QIconEngine? 上下文_C++_Qt_Qicon - Fatal编程技术网

C++ 如何使QPalette可访问QIconEngine? 上下文

C++ 如何使QPalette可访问QIconEngine? 上下文,c++,qt,qicon,C++,Qt,Qicon,假设我想要实现一个定制的QWidget类,它应该实现一个按钮(类似于QPushButton) 要求: 它应遵循操作系统(以及图标)的颜色 它应显示QWidget::action中的图标。对于本练习,图标可以是使用QPaint手动绘制的简单矩形。在最终版本中,我使用SvgRender用操作系统的颜色为图标着色 它应允许不同的尺寸和分辨率 注意:qpalete有助于获得操作系统的颜色,用于多种用途。重要的是不要保存调色板,而是在需要时调用QWidget::palette():这允许操作系统在用户更

假设我想要实现一个定制的QWidget类,它应该实现一个按钮(类似于QPushButton)

要求:

  • 它应遵循操作系统(以及图标)的颜色
  • 它应显示
    QWidget::action
    中的图标。对于本练习,图标可以是使用QPaint手动绘制的简单矩形。在最终版本中,我使用SvgRender用操作系统的颜色为图标着色
  • 它应允许不同的尺寸和分辨率
  • 注意:
    qpalete
    有助于获得操作系统的颜色,用于多种用途。重要的是不要保存调色板,而是在需要时调用
    QWidget::palette()
    :这允许操作系统在用户更改操作系统上的颜色设置时正确更新UI

    为了说明我的尝试,我将使用以下示例:

    #include <QDebug>
    #include <QAction>
    #include <QApplication>
    #include <QIconEngine>
    #include <QPainter>
    #include <QPaintEvent>
    #include <QPushButton>
    #include <QVBoxLayout>
    #include <QWidget>
    
    class TestIconEngine: public QIconEngine
    {
    public:
        void paint(QPainter* p, const QRect &rect, QIcon::Mode mode, QIcon::State state) override
        {
            p->fillRect(rect, QColor(128,0,0)); // I SHOULD USE QPALETTE HERE!!! <------
            qDebug() << "icon engine paint: " << rect << mode << state;
        }
        
        QIconEngine* clone() const override
        {
            throw std::runtime_error("Not yet implemented");
        }
    };
    
    
    class TestWidget: public QPushButton
    {
        Q_OBJECT
    public:
        explicit TestWidget( QWidget* parent=nullptr):QPushButton(parent){}
        void paintEvent(QPaintEvent *event) override
        {
            QPainter p(this);
            p.setRenderHint(QPainter::Antialiasing, true);
            p.setClipRect(event->rect());
            
            const auto pal = palette();
    
            p.setBrush(pal.color(QPalette::Button));
            p.setPen(QPen(pal.color(QPalette::Highlight),2.0));
    
            const auto r = rect();
            p.drawRoundedRect(r.x()+1, r.y()+1, r.width()-2, r.height()-2, 10.0, 10.0);
            
            const auto& actList = actions();
            if (actList.size()>0)
            {
                const QAction* act = actList[0];
                const QRect iconRect(r.x()+5, r.y()+5, r.height()-10, r.height()-10);
                act->icon().paint(&p, iconRect);
            }
        }
    };
    #include "main.moc"
    
    
    int main(int argn, char* argv[])
    {
        QApplication app(argn, argv);
        
        QWidget window;
        window.resize(300, 200);
        window.show();
        QVBoxLayout layout(&window);
        layout.setContentsMargins(10, 10, 10, 10);
        window.setLayout(&layout);
        
        TestWidget test(&window);
        layout.addWidget(&test);
        
        QAction act;
        act.setIcon(QIcon(new TestIconEngine())); //This shit takes ownership of engine (!RAII)
        test.addAction(&act);
        
        return app.exec();
    }
    
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    #包括
    类TestIconEngine:公共QIconEngine
    {
    公众:
    无效绘制(QPainter*p、const QRect和rect、QIcon::Mode、QIcon::State)覆盖
    {
    p->fillRect(rect,QColor(128,0,0));//我应该在这里使用qpalete!!!