c++ - Qt 的 BorderLayout 示例如何工作?

标签 c++ qt

我正在尝试修改 borderlayout.cpp Example File .但是,在阅读代码时,我不明白以下几行是如何工作的:

item->setGeometry(QRect(item->geometry().x(),
    item->geometry().y(), rect.width(),
    item->sizeHint().height());

southHeight += item->geometry().height() + spacing();

item->setGeometry(QRect(rect.x(),
     rect.y() + rect.height() - southHeight + spacing(),
     item->geometry().width(),
     item->geometry().height()));

为什么 setGeometry 被调用了两次?另外,item->geometry() 是什么/如何设置为(我知道它可能是由 QLayout::setGeometry(rect) 设置的,但是查看 QLayout 的源代码,该函数所做的所有事情就是将 rect 分配给一个内部指针)?

完整的函数是:

void BorderLayout::setGeometry(const QRect &rect)
 {
     ItemWrapper *center = 0;
     int eastWidth = 0;
     int westWidth = 0;
     int northHeight = 0;
     int southHeight = 0;
     int centerHeight = 0;
     int i;

     QLayout::setGeometry(rect);

     for (i = 0; i < list.size(); ++i) {
         ItemWrapper *wrapper = list.at(i);
         QLayoutItem *item = wrapper->item;
         Position position = wrapper->position;

         if (position == North) {
             item->setGeometry(QRect(rect.x(), northHeight, rect.width(),
                                     item->sizeHint().height()));

             northHeight += item->geometry().height() + spacing();
         } else if (position == South) {
             item->setGeometry(QRect(item->geometry().x(),
                                     item->geometry().y(), rect.width(),
                                     item->sizeHint().height()));

             southHeight += item->geometry().height() + spacing();

             item->setGeometry(QRect(rect.x(),
                               rect.y() + rect.height() - southHeight + spacing(),
                               item->geometry().width(),
                               item->geometry().height()));
         } else if (position == Center) {
             center = wrapper;
         }
     }

     centerHeight = rect.height() - northHeight - southHeight;

     for (i = 0; i item;
         Position position = wrapper->position;

         if (position == West) {
             item->setGeometry(QRect(rect.x() + westWidth, northHeight,
                                     item->sizeHint().width(), centerHeight));

             westWidth += item->geometry().width() + spacing();
         } else if (position == East) {
             item->setGeometry(QRect(item->geometry().x(), item->geometry().y(),
                                     item->sizeHint().width(), centerHeight));

             eastWidth += item->geometry().width() + spacing();

             item->setGeometry(QRect(
                               rect.x() + rect.width() - eastWidth + spacing(),
                               northHeight, item->geometry().width(),
                               item->geometry().height()));
         }
     }

     if (center)
         center->item->setGeometry(QRect(westWidth, northHeight,
                                         rect.width() - eastWidth - westWidth,
                                         centerHeight));
 }

最佳答案

之前碰巧玩过demo。要回答你的问题,首先 setGeometry 只是为了计算出项目的高度,然后第二个 setGeometry() 将它放在布局的底部。 当项目被放置在布局的南/底部时,你一开始不知道它有多高,对吧?

这是GUI编程中的常规做法,例如,要在label中放置一个字符串,您会尝试将其放置在任何地方,只是为了计算出高度和长度,然后分配正确的rect,然后将字符串放入你想要的地方。

关于c++ - Qt 的 BorderLayout 示例如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21398607/

相关文章:

c++ - 以 Python 方式调用 Lua 函数,即命名参数

c++ - 对 vector 感到困惑

qt - 在 QTabWidget 选项卡中设置标签文本

c++ - QThread:线程仍在运行时被销毁,QMutex 被销毁

python - 通过网络发送结构化数据

android - 如何在 Android 的 QML WebView 中启用 LocalStorage?

c++ - 创建和写入文件 C++

c++ - 递归 C++ 字母顺序函数重复返回相同的值

c++ - 静态变量的位置

c++ - 将 QString 转换为 float 给我错误