翻譯|使用教程|編輯:龔雪|2024-11-27 10:41:48.117|閱讀 110 次
概述:本文主要介紹展示如何使用為窗口實現(xiàn)流程布局,歡迎下載最新版組件體驗~
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
Qt 是目前最先進、最完整的跨平臺C++開發(fā)工具。它不僅完全實現(xiàn)了一次編寫,所有平臺無差別運行,更提供了幾乎所有開發(fā)過程中需要用到的工具。如今,Qt已被運用于超過70個行業(yè)、數(shù)千家企業(yè),支持數(shù)百萬設(shè)備及應(yīng)用。
本文將展示如何為不同的窗口大小排列小部件。
流程布局實現(xiàn)了處理不同窗口大小的布局,小部件的位置取決于應(yīng)用程序窗口的寬度。
Flowlayout類主要使用和,而Window類使用和。
在上文中(點擊這里回顧>>),我們主要介紹了FlowLayout類定義、示例運行等,本文將繼續(xù)介紹FlowLayout類實現(xiàn),請繼續(xù)關(guān)注哦~
Qt技術(shù)交流群:166830288 歡迎一起進群討論
我們從構(gòu)造函數(shù)開始:
FlowLayout::FlowLayout(QWidget *parent, int margin, int hSpacing, int vSpacing) : QLayout(parent), m_hSpace(hSpacing), m_vSpace(vSpacing) { setContentsMargins(margin, margin, margin, margin); } FlowLayout::FlowLayout(int margin, int hSpacing, int vSpacing) : m_hSpace(hSpacing), m_vSpace(vSpacing) { setContentsMargins(margin, margin, margin, margin); }
在構(gòu)造函數(shù)中,我們調(diào)用setContentsMargins()來設(shè)置左、上、右和下邊距。默認情況下,QLayout使用當(dāng)前樣式提供的值(參見)。
FlowLayout::~FlowLayout() { QLayoutItem *item; while ((item = takeAt(0))) delete item; }
在這個例子中,我們重新實現(xiàn)了addItem(),它是一個純虛函數(shù)。當(dāng)使用addItem() 時,布局項的所有權(quán)被轉(zhuǎn)移到布局,因此它是布局的責(zé)任來刪除它們。
void FlowLayout::addItem(QLayoutItem *item) { itemList.append(item); }
addItem()用于向布局中添加項。
int FlowLayout::horizontalSpacing() const { if (m_hSpace >= 0) { return m_hSpace; } else { return smartSpacing(QStyle::PM_LayoutHorizontalSpacing); } } int FlowLayout::verticalSpacing() const { if (m_vSpace >= 0) { return m_vSpace; } else { return smartSpacing(QStyle::PM_LayoutVerticalSpacing); } }
我們實現(xiàn)了horizontalSpacing()和verticalSpacing() 來獲取布局中小部件之間的間距,如果該值小于或等于0,則使用此值。如果沒有,將調(diào)用smartSpacing()來計算間距。
int FlowLayout::count() const { return itemList.size(); } QLayoutItem *FlowLayout::itemAt(int index) const { return itemList.value(index); } QLayoutItem *FlowLayout::takeAt(int index) { if (index >= 0 && index < itemList.size()) return itemList.takeAt(index); return nullptr; }
然后實現(xiàn)count()來返回布局中的項數(shù),為了在項目列表中導(dǎo)航,我們使用 itemAt() 和 takeAt() 從列表中刪除和返回項目。如果一個項目被刪除,剩下的項目將重新編號,這三個函數(shù)都是來自的純虛函數(shù)。
Qt::Orientations FlowLayout::expandingDirections() const { return { }; }
expandingDirections()返回s,其中布局可以使用比sizeHint()更多的空間。
bool FlowLayout::hasHeightForWidth() const { return true; } int FlowLayout::heightForWidth(int width) const { int height = doLayout(QRect(0, 0, width, 0), true); return height; }
為了調(diào)整到高度依賴于寬度的小部件,我們實現(xiàn)了heightForWidth()。函數(shù)hasHeightForWidth()被用來測試這個依賴關(guān)系,并且heightForWidth()將寬度傳遞給doLayout(),后者反過來使用寬度作為布局矩形的參數(shù),即項目布局的邊界,該矩形不包括布局margin()。
void FlowLayout::setGeometry(const QRect &rect) { QLayout::setGeometry(rect); doLayout(rect, false); } QSize FlowLayout::sizeHint() const { return minimumSize(); } QSize FlowLayout::minimumSize() const { QSize size; for (const QLayoutItem *item : std::as_const(itemList)) size = size.expandedTo(item->minimumSize()); const QMargins margins = contentsMargins(); size += QSize(margins.left() + margins.right(), margins.top() + margins.bottom()); return size; }
setGeometry()通常用于執(zhí)行實際的布局,即計算布局項的幾何形狀。在這個例子中,它調(diào)用了doLayout()并傳遞了布局矩形。
sizeHint()返回布局的首選大小,minimumSize()返回布局的最小大小。
int FlowLayout::doLayout(const QRect &rect, bool testOnly) const { int left, top, right, bottom; getContentsMargins(&left, &top, &right, &bottom); QRect effectiveRect = rect.adjusted(+left, +top, -right, -bottom); int x = effectiveRect.x(); int y = effectiveRect.y(); int lineHeight = 0;
如果horizontalSpacing() 或 verticalSpacing()不返回默認值,則doLayout()處理布局,它使用getContentsMargins()來計算布局項的可用面積。
for (QLayoutItem *item : std::as_const(itemList)) { const QWidget *wid = item->widget(); int spaceX = horizontalSpacing(); if (spaceX == -1) spaceX = wid->style()->layoutSpacing( QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Horizontal); int spaceY = verticalSpacing(); if (spaceY == -1) spaceY = wid->style()->layoutSpacing( QSizePolicy::PushButton, QSizePolicy::PushButton, Qt::Vertical);
然后,它根據(jù)當(dāng)前樣式為布局中的每個小部件設(shè)置適當(dāng)?shù)拈g距。
int nextX = x + item->sizeHint().width() + spaceX; if (nextX - spaceX > effectiveRect.right() && lineHeight > 0) { x = effectiveRect.x(); y = y + lineHeight + spaceY; nextX = x + item->sizeHint().width() + spaceX; lineHeight = 0; } if (!testOnly) item->setGeometry(QRect(QPoint(x, y), item->sizeHint())); x = nextX; lineHeight = qMax(lineHeight, item->sizeHint().height()); } return y + lineHeight - rect.y() + bottom; }
然后通過將項目寬度和行高添加到初始x和y坐標來計算布局中每個項目的位置,這反過來又讓我們知道下一項是否適合當(dāng)前行,或者是否必須向下移動到下一行,我們還根據(jù)小部件的高度找到當(dāng)前行的高度。
int FlowLayout::smartSpacing(QStyle::PixelMetric pm) const { QObject *parent = this->parent(); if (!parent) { return -1; } else if (parent->isWidgetType()) { QWidget *pw = static_cast<QWidget *>(parent); return pw->style()->pixelMetric(pm, nullptr, pw); } else { return static_cast<QLayout *>(parent)->spacing(); } }
smartSpacing()被設(shè)計為獲取頂級布局或子布局的默認間距,當(dāng)父組件是QWidget時,頂級布局的默認間距將通過查詢樣式來確定。當(dāng)父布局為時,子布局的默認間距將通過查詢父布局的間距來確定。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都網(wǎng)