QT-常用函数参数详解之painter->fillRect(),预设值的颜色展示

项目场景:

在使用qt中的painter直接进行绘制时,有时会用到fillrect进行填充矩形的展示,此函数预设了很多的颜色,在网上我暂时没找到直接能够查看的颜色展示,在此我写一个程序用于直接展示出来,截图在此,方便自己以及有一样需求的人直接查看


代码描述:

通过生成pdf以及保存pdf的方式,直接调用QPdfWriter,QPrinter这两种库,进行编程展示。需要提前引入printsupport的插件(和gui,charts差不多的东西)。最终代码以及展示结果见正文,不想看的也可以直接看结果。(QT5.12.5以上版本才有)

1.fillRect代码和预设值有关的函数

inline void fillRect(int x, int y, int w, int h, QGradient::Preset preset);
inline void fillRect(const QRect &r, QGradient::Preset preset);
inline void fillRect(const QRectF &r, QGradient::Preset preset);

2.展示代码

	//设置pdf保存的路径
    QString file_path = pdfSaveAs(QString::fromLocal8Bit("展示QGradient预设值"));
    if(file_path.isEmpty())
        return;
    QFile pdfFile(file_path);
    pdfFile.open(QIODevice::WriteOnly);
    QPdfWriter* pWriter = new QPdfWriter(&pdfFile);

    //Init Page
    pWriter->setPageSize(QPagedPaintDevice::A4);
    pWriter->setResolution(300);    //设置dpi 每个平方英寸像素为300
    pWriter->setPageMargins(QMarginsF(30, 30, 30, 30));

    QPainter* pPainter = new QPainter(pWriter);
    //Painter PDF
    qDebug() << pPainter->viewport();
    int nPDFWidth = pPainter->viewport().width();
    int nPDFHeight = pPainter->viewport().height();
    
    //test start
    int realcolor = 0;
    int realheight = 0;
    while (realcolor < 180)
    {
        realcolor++;
        QGradient::Preset a = QGradient::Preset(realcolor);
        pPainter->fillRect(QRect(0, realheight, nPDFWidth, 100), a);
        pPainter->setFont(QFont("黑体", 26, 60));
        pPainter->drawText(QRect(0, realheight, nPDFWidth, 100), Qt::AlignCenter,
            QString::fromLocal8Bit("测试预设颜色%1").arg(realcolor));

        realheight += 100;
        if (realheight >= nPDFHeight)
        {
            pWriter->newPage();
            realheight = 0;
        }
    }
    //test end
    
     //绘制完毕
    delete pPainter;
    delete pWriter;
    pdfFile.close();
    //通过其它PDF阅读器来打开PDF
    QDesktopServices::openUrl(QUrl::fromLocalFile(file_path));

结果图展示:

enum Preset {
        WarmFlame = 1,
        NightFade = 2,
        SpringWarmth = 3,
        JuicyPeach = 4,
        YoungPassion = 5,
        LadyLips = 6,
        SunnyMorning = 7,
        RainyAshville = 8,
        FrozenDreams = 9,
        WinterNeva = 10,
        DustyGrass = 11,
        TemptingAzure = 12,
        HeavyRain = 13,
        AmyCrisp = 14,
        MeanFruit = 15,
        DeepBlue = 16,
        RipeMalinka = 17,
        CloudyKnoxville = 18,
        MalibuBeach = 19,
        NewLife = 20,
        TrueSunset = 21,
        MorpheusDen = 22,
        RareWind = 23,
        NearMoon = 24,
        WildApple = 25,
        SaintPetersburg = 26,
        PlumPlate = 28,
        EverlastingSky = 29,
        HappyFisher = 30,
        Blessing = 31,
        SharpeyeEagle = 32,
        LadogaBottom = 33,
        LemonGate = 34,
        ItmeoBranding = 35,
        ZeusMiracle = 36,
        OldHat = 37,
        StarWine = 38,
        HappyAcid = 41,
        AwesomePine = 42,
        NewYork = 43,
        ShyRainbow = 44,
        MixedHopes = 46,
        FlyHigh = 47,
        StrongBliss = 48,
        FreshMilk = 49,
        SnowAgain = 50,
        FebruaryInk = 51,
        KindSteel = 52,
        SoftGrass = 53,
        GrownEarly = 54,
        SharpBlues = 55,
        ShadyWater = 56,
        DirtyBeauty = 57,
        GreatWhale = 58,
        TeenNotebook = 59,
        PoliteRumors = 60,
        SweetPeriod = 61,
        WideMatrix = 62,
        SoftCherish = 63,
        RedSalvation = 64,
        BurningSpring = 65,
        NightParty = 66,
        SkyGlider = 67,
        HeavenPeach = 68,
        PurpleDivision = 69,
        AquaSplash = 70,
        SpikyNaga = 72,
        LoveKiss = 73,
        CleanMirror = 75,
        PremiumDark = 76,
        ColdEvening = 77,
        CochitiLake = 78,
        SummerGames = 79,
        PassionateBed = 80,
        MountainRock = 81,
        DesertHump = 82,
        JungleDay = 83,
        PhoenixStart = 84,
        OctoberSilence = 85,
        FarawayRiver = 86,
        AlchemistLab = 87,
        OverSun = 88,
        PremiumWhite = 89,
        MarsParty = 90,
        EternalConstance = 91,
        JapanBlush = 92,
        SmilingRain = 93,
        CloudyApple = 94,
        BigMango = 95,
        HealthyWater = 96,
        AmourAmour = 97,
        RiskyConcrete = 98,
        StrongStick = 99,
        ViciousStance = 100,
        PaloAlto = 101,
        HappyMemories = 102,
        MidnightBloom = 103,
        Crystalline = 104,
        PartyBliss = 106,
        ConfidentCloud = 107,
        LeCocktail = 108,
        RiverCity = 109,
        FrozenBerry = 110,
        ChildCare = 112,
        FlyingLemon = 113,
        NewRetrowave = 114,
        HiddenJaguar = 115,
        AboveTheSky = 116,
        Nega = 117,
        DenseWater = 118,
        Seashore = 120,
        MarbleWall = 121,
        CheerfulCaramel = 122,
        NightSky = 123,
        MagicLake = 124,
        YoungGrass = 125,
        ColorfulPeach = 126,
        GentleCare = 127,
        PlumBath = 128,
        HappyUnicorn = 129,
        AfricanField = 131,
        SolidStone = 132,
        OrangeJuice = 133,
        GlassWater = 134,
        NorthMiracle = 136,
        FruitBlend = 137,
        MillenniumPine = 138,
        HighFlight = 139,
        MoleHall = 140,
        SpaceShift = 142,
        ForestInei = 143,
        RoyalGarden = 144,
        RichMetal = 145,
        JuicyCake = 146,
        SmartIndigo = 147,
        SandStrike = 148,
        NorseBeauty = 149,
        AquaGuidance = 150,
        SunVeggie = 151,
        SeaLord = 152,
        BlackSea = 153,
        GrassShampoo = 154,
        LandingAircraft = 155,
        WitchDance = 156,
        SleeplessNight = 157,
        AngelCare = 158,
        CrystalRiver = 159,
        SoftLipstick = 160,
        SaltMountain = 161,
        PerfectWhite = 162,
        FreshOasis = 163,
        StrictNovember = 164,
        MorningSalad = 165,
        DeepRelief = 166,
        SeaStrike = 167,
        NightCall = 168,
        SupremeSky = 169,
        LightBlue = 170,
        MindCrawl = 171,
        LilyMeadow = 172,
        SugarLollipop = 173,
        SweetDessert = 174,
        MagicRay = 175,
        TeenParty = 176,
        FrozenHeat = 177,
        GagarinView = 178,
        FabledSunset = 179,
        PerfectBlue = 180,

        NumPresets
    };

QT-常用函数参数详解之painter->fillRect(),预设值的颜色展示_第1张图片
QT-常用函数参数详解之painter->fillRect(),预设值的颜色展示_第2张图片
QT-常用函数参数详解之painter->fillRect(),预设值的颜色展示_第3张图片

你可能感兴趣的:(QT6,实用技巧,学习总结,qt,c++)