QLabel的使用

1、文本顯示


    QLabel* infoTitleDetailLa = new QLabel();
    infoTitleDetailLa->setText(“文本顯示”);
    infoTitleDetailLa->setFixedSize(100,30);

2、文本樣式(背景色、字體類型、字體大小、字體顏色)

    infoTitleDetailLa->setStyleSheet("background:transparent;"
                                     "font-family:SimHei;"
                                     "font-size:16px;"
                                     "color:white");

3、文本對齊方式:主要是:水平、垂直、居中,詳細見枚舉AlignmentFlag

 enum AlignmentFlag {
        AlignLeft = 0x0001,
        AlignLeading = AlignLeft,
        AlignRight = 0x0002,
        AlignTrailing = AlignRight,
        AlignHCenter = 0x0004,
        AlignJustify = 0x0008,
        AlignAbsolute = 0x0010,
        AlignHorizontal_Mask = AlignLeft | AlignRight | AlignHCenter | AlignJustify | AlignAbsolute,

        AlignTop = 0x0020,
        AlignBottom = 0x0040,
        AlignVCenter = 0x0080,
        AlignBaseline = 0x0100,
        // Note that 0x100 will clash with Qt::TextSingleLine = 0x100 due to what the comment above
        // this enum declaration states. However, since Qt::AlignBaseline is only used by layouts,
        // it doesn't make sense to pass Qt::AlignBaseline to QPainter::drawText(), so there
        // shouldn't really be any ambiguity between the two overlapping enum values.
        AlignVertical_Mask = AlignTop | AlignBottom | AlignVCenter | AlignBaseline,

        AlignCenter = AlignVCenter | AlignHCenter
    };

這裏以居中顯示舉例,其他情況可以自行嘗試設置:

    infoTitleDetailLa->setAlignment(Qt::AlignCenter);

4、設置文本自動換行

    infoTitleDetailLa->setWordWrap(true);

 

發佈了13 篇原創文章 · 獲贊 0 · 訪問量 1302
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章