天天看點

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

qgraphicsitem 類是 qgraphicsscene 中所有 item 的基類。

<a href="#%e7%ae%80%e8%bf%b0">簡述</a>

<a href="#%e5%b8%b8%e8%a7%81%e7%9a%84-qgraphicsitem">常見的 qgraphicsitem</a>

<a href="#qgraphicssimpletextitem">qgraphicssimpletextitem</a>

<a href="#%e8%af%a6%e7%bb%86%e6%8f%8f%e8%bf%b0">較長的描述</a>

<a href="#%e7%a4%ba%e4%be%8b">示例</a>

<a href="#qgraphicstextitem">qgraphicstextitem</a>

<a href="#%e8%af%a6%e7%bb%86%e6%8f%8f%e8%bf%b0-1">較長的描述</a>

<a href="#%e7%ba%af%e6%96%87%e6%9c%ac">純文字</a>

<a href="#%e5%af%8c%e6%96%87%e6%9c%ac">富文本</a>

<a href="#%e8%b6%85%e9%93%be%e6%8e%a5">超連結</a>

<a href="#%e7%bc%96%e8%be%91">編輯</a>

<a href="#qgraphicslineitem">qgraphicslineitem</a>

<a href="#%e8%af%a6%e7%bb%86%e6%8f%8f%e8%bf%b0-2">較長的描述</a>

<a href="#%e7%a4%ba%e4%be%8b-1">示例</a>

<a href="#qgraphicspixmapitem">qgraphicspixmapitem</a>

<a href="#%e8%af%a6%e7%bb%86%e6%8f%8f%e8%bf%b0-3">較長的描述</a>

<a href="#%e7%a4%ba%e4%be%8b-2">示例</a>

<a href="#qgraphicsrectitem">qgraphicsrectitem</a>

<a href="#%e8%af%a6%e7%bb%86%e8%af%b4%e6%98%8e">詳細說明</a>

<a href="#%e7%a4%ba%e4%be%8b-3">示例</a>

<a href="#qgraphicsellipseitem">qgraphicsellipseitem</a>

<a href="#%e8%af%a6%e7%bb%86%e8%af%b4%e6%98%8e-1">詳細說明</a>

<a href="#%e7%a4%ba%e4%be%8b-4">示例</a>

<a href="#qgraphicspathitem">qgraphicspathitem</a>

<a href="#%e8%af%a6%e7%bb%86%e6%8f%8f%e8%bf%b0-4">較長的描述</a>

<a href="#%e7%a4%ba%e4%be%8b-5">示例</a>

<a href="#qgraphicspolygonitem">qgraphicspolygonitem</a>

<a href="#%e8%af%a6%e7%bb%86%e6%8f%8f%e8%bf%b0-5">較長的描述</a>

<a href="#%e7%a4%ba%e4%be%8b-6">示例</a>

為友善起見,qt 為最常見的形狀提供了一組典型的标準 item。它們是:

qgraphicssimpletextitem:提供了一個簡單的文本标簽項

qgraphicstextitem:提供了一個格式化的文本項

qgraphicslineitem:提供了一個直線項

qgraphicspixmapitem:提供了一個圖像項

qgraphicsrectitem:提供了一個矩形項

qgraphicsellipseitem:提供了一個橢圓項

qgraphicspathitem:提供了一個路徑項

qgraphicspolygonitem:提供了一個多邊形項

qgraphicssimpletextitem 提供了一個簡單的文本标簽項,可以添加到 qgraphicsscene 中。

要設定 item 的文本,可以傳遞 qstring 到 qgraphicssimpletextitem 的構造函數,或在之後調用 settext() 來更改文本。要設定文本填充色,調用 setbrush()。

qgraphicssimpletextitem 可以具有填充和輪廓,setbrush() 用于設定文本填充(即,文本色),setpen() 用于設定繪制文本輪廓的畫筆,(後者可能很慢,特别是對于複雜的畫筆,以及具有長文本内容的 item)。如果隻想繪制一行簡單的文本,隻需要調用 setbrush(),不需要設定畫筆。qgraphicssimpletextitem 的畫筆預設是 qt::nopen。

qgraphicssimpletextitem 使用文本的格式化大小和相關聯的字型,為 boundingrect()、shape() 和 contains() 提供了一個合理的實作。可以通過調用 setfont() 設定字型。

qgraphicssimpletext 不顯示富文本,相反,可以使用 qgraphicstextitem,它提供全文控制功能。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem
常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

要将 qgraphicssimpletextitem 添加至場景中,有兩種方式:

構造 qgraphicssimpletextitem,然後通過 qgraphicsscene::additem() 添加。

使用 qgraphicsscene::addsimpletext() 建立并添加一個 qgraphicssimpletextitem,并傳回 item 的指針。

qgraphicstextitem 類提供了一個格式化的文本項,可以添加到 qgraphicsscene 中。

要設定 item 的文本,可以傳遞 qstring 到 qgraphicstextitem 的構造函數,或調用 sethtml()/setplaintext()。

qgraphicstextitem 使用文本的格式化大小和相關聯的字型,為 boundingrect()、shape() 和 contains() 提供了一個合理的實作。可以通過調用 setfont() 設定字型。

可以通過使用 settextinteractionflags() 設定 qt::texteditorinteraction 标志來使 item 可編輯。

item 的首選文本寬度可以使用 settextwidth() 設定,并使用 textwidth() 擷取。

注意:為了在中心對齊 html 文本,必須設定 item 的文本寬度。否則,可以在設定 item 的文本後調用 adjustsize()。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

注意: qgraphicstextitem 預設接受 hover 事件,可以使用 setaccepthoverevents() 更改此值。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

要将 qgraphicstextitem 添加至場景中,有兩種方式:

構造 qgraphicstextitem,然後通過 qgraphicsscene::additem() 添加。

使用 qgraphicsscene::addtext() 建立并添加一個 qgraphicstextitem,并傳回 item 的指針。

假設文本是 html 格式,顯示不同顔色的文本以及圖檔。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

我們需要使用标簽 <code>&lt;a&gt;&lt;/a&gt;</code>寫一段簡單的 html 超連結代碼

方式一:

比較簡單,直接調用 setopenexternallinks(true) 即可。

方式二:

連接配接 linkactivated() 信号,然後調用 qdesktopservices 的 openurl() 打開連結:

注意:這兩種方式都需要調用 settextinteractionflags(qt::textbrowserinteraction),指定互動方式為文本浏覽器互動。

通過使用 settextinteractionflags() 設定 qt::texteditorinteraction 标志來使項目可編輯。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

輸出如下:

“q” “qt” “qte” “qter”

qgraphicslineitem 類提供了一個直線項,可以添加到 qgraphicsscene 中。

要設定 item 的直線,可以傳遞 qlinef 到 qgraphicslineitem 的構造函數,或調用 setline() 函數。line() 傳回目前直線。預設情況下,該直線為黑色,寬度為 0,可以通過調用 setpen() 進行更改。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

qgraphicslineitem 使用直線和畫筆的寬度,為 boundingrect()、shape() 和 contains() 提供了一個合理的實作。paint() 函數使用 item 關聯的畫筆繪制直線。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

要将 qgraphicslineitem 添加至場景中,有兩種方式:

構造 qgraphicslineitem,然後通過 qgraphicsscene::additem() 添加。

使用 qgraphicsscene::addline() 建立并添加一個 qgraphicslineitem,并傳回 item 的指針。

qgraphicspixmapitem 類提供了一個圖像項,可以添加到 qgraphicsscene 中。

要設定 item 的圖像,可以傳遞 qpixmap 到 qgraphicspixmapitem 的構造函數,或調用 setpixmap() 函數,pixmap() 傳回目前的圖像。

qgraphicspixmapitem 使用 pixmap 的可選 alpha 掩碼,為 boundingrect()、shape() 和 contains() 提供了一個合理的實作。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

圖像在 item 的 (0, 0) 坐标處繪制,由 offset() 傳回。可以通過調用 setoffset() 更改繪圖偏移量。

可以通過調用 settransformationmode() 設定圖像的變換模式,預設情況下,使用 qt::fasttransformation,它提供了快速、不平滑的縮放。qt::smoothtransformation 在 painter 上啟用 qpainter::smoothpixmaptransform,品質取決于平台和視口。結果通常不如調用 qpixmap::scale() 直接,調用 transformmode() 擷取項目的目前轉換模式。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

要将 qgraphicspixmapitem 添加至場景中,有兩種方式:

構造 qgraphicspixmapitem,然後通過 qgraphicsscene::additem() 添加。

使用 qgraphicsscene::addpixmap() 建立并添加一個 qgraphicspixmapitem,并傳回 item 的指針。

qgraphicsrectitem 類提供了一個矩形項,可以添加到 qgraphicsscene 中。

要設定 item 的矩形,可以傳遞一個 qrectf 到 qgraphicsrectitem 的構造函數,或調用 setrect() 函數。rect() 傳回目前矩形。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

qgraphicsrectitem 使用矩形和畫筆寬度,為 boundingrect()、shape() 和 contains() 提供了一個合理的實作。paint() 函數使用 item 關聯的畫筆和畫刷繪制矩形,可以通過調用 setpen() 和 setbrush() 函數來設定。

注意:無效矩形(例如,寬度或高度為負)的呈現是未定義的。如果不能确定使用的是有效的矩形(例如,如果使用來自不可靠源的資料建立的矩形),那麼應該使用 qrectf::normalized() 建立标準化的矩形,然後使用它們。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

要将 qgraphicsrectitem 添加至場景中,有兩種方式:

構造 qgraphicsrectitem,然後通過 qgraphicsscene::additem() 添加。

使用 qgraphicsscene::addrect() 建立并添加一個 qgraphicsrectitem,并傳回 item 的指針。

qgraphicsellipseitem 類提供了一個橢圓項,可以添加到 qgraphicsscene 中。

qgraphicsellipseitem 表示一個帶有填充和輪廓的橢圓,也可以使用它的橢圓段(見 startangle()、spanangle())。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem
常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

要設定 item 的橢圓,可以傳遞一個 qrectf 到 qgraphicsellipseitem 的構造函數,或調用 setrect()。rect() 傳回目前橢圓的幾何形狀。

qgraphicsellipseitem 使用 rect 和畫筆寬度,為 boundingrect()、shape() 和 contains() 提供了一個合理的實作。paint() 函數使用 item 關聯的畫筆和畫刷來繪制橢圓,可以通過調用 setpen() 和 setbrush() 來設定。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

要将 qgraphicsellipseitem 添加至場景中,有兩種方式:

構造 qgraphicsellipseitem,然後通過 qgraphicsscene::additem() 添加。

使用 qgraphicsscene::addellipse() 建立并添加一個 qgraphicsellipseitem,并傳回 item 的指針。

qgraphicspathitem 類提供了一個路徑項,可以添加到 qgraphicsscene 中。

要設定 item 的路徑,可以傳遞 qpainterpath 到 qgraphicspathitem 的構造函數,或調用 setpath() 函數,path() 傳回目前路徑。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

qgraphicspathitem 使用路徑,為 boundingrect()、shape() 和 contains() 提供了一個合理的實作。paint() 函數使用 item 關聯的畫筆和畫刷來繪制路徑,可以通過調用 setpen() 和 setbrush() 函數來設定。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

要将 qgraphicspathitem 添加至場景中,有兩種方式:

構造 qgraphicspathitem,然後通過 qgraphicsscene::additem() 添加。

使用 qgraphicsscene::addpath() 建立并添加一個 qgraphicspathitem,并傳回 item 的指針。

qgraphicspolygonitem 類提供了一個多邊形項,可以添加到 qgraphicsscene 中。

要設定 item 的多邊形,傳遞 qpolygonf 到 qgraphicspolygonitem 的構造函數,或調用 setpolygon() 函數。polygon() 傳回目前的多邊形。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

qgraphicspolygonitem 使用多邊形和畫筆寬度,為 boundingrect()、shape() 和 contains() 提供了一個合理的實作。paint() 函數使用 item 關聯的畫筆和畫刷繪制多邊形,可以通過調用 setpen() 和 setbrush() 函數進行設定。

常見的QGraphicsItem簡述常見的 QGraphicsItemQGraphicsSimpleTextItemQGraphicsTextItemQGraphicsLineItemQGraphicsPixmapItemQGraphicsRectItemQGraphicsEllipseItemQGraphicsPathItemQGraphicsPolygonItem

要将 qgraphicspolygonitem 添加至場景中,有兩種方式:

構造 qgraphicspolygonitem,然後通過 qgraphicsscene::additem() 添加。

使用 qgraphicsscene::addpolygon() 建立并添加一個 qgraphicspolygonitem,并傳回 item 的指針。