pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

從 QGridLayout 中刪除小部件

Removing widgets from QGridLayout(從 QGridLayout 中刪除小部件)
本文介紹了從 QGridLayout 中刪除小部件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我嘗試從 QGridLayout 中的指定行中刪除小部件,如下所示:

I try to remove widgets from a specified row in a QGridLayout like this:

void delete_grid_row(QGridLayout *layout, int row)
{
    if (!layout || row < 0) return;

    for (int i = 0; i < layout->columnCount(); ++i) {
        QLayoutItem* item = layout->itemAtPosition(row, i);
        if (!item) continue;

        if (item->widget()) {
            layout->removeWidget(item->widget());
        } else {
            layout->removeItem(item);
        }
        delete item;
    }
}

但是當我調用它時,應用程序在第一次迭代中在 delete item 上崩潰并顯示 SIGSEGV.有什么想法嗎?

But when I call it, the app crashes with SIGSEGV on delete item in the first iteration. Any ideas?

推薦答案

簡短回答:使用下面提供的代碼

QGridLayout 中刪除一行或一列(甚至單個單元格)是很棘手的.使用下面提供的代碼.

Short answer: Use the code provided below

Removing a row or column (or even a single cell) from a QGridLayout is tricky. Use the code provided below.

首先,請注意QGridLayout::rowCount()QGridLayout::columnCount() 總是返回內部分配 的行數和網格布局中的列.例如,如果您在新構建的網格布局上調用 QGridLayout::addWidget(widget,5,7),則行數將為 6,列數將為 8,并且所有單元格除了索引 (5,7) 上的單元格之外的網格布局將為空,因此在 GUI 中不可見.

First, note that QGridLayout::rowCount() and QGridLayout::columnCount() always return the number of internally allocated rows and columns in the grid layout. As an example, if you call QGridLayout::addWidget(widget,5,7) on a freshly constructed grid layout, the row count will be 6 and the column count will be 8, and all cells of the grid layout except the cell on index (5,7) will be empty and thus invisible within the GUI.

請注意,很遺憾不可能從網格布局中刪除這樣的內部行或列.換句話說,網格布局的行數和列數總是只會增長,而不會縮小.

Note that it's unfortunately impossible to remove such an internal row or column from the grid layout. In other words, the row and column count of a grid layout can always only grow, but never shrink.

可以做的是刪除行或列的內容,這將有效地具有與刪除行或列本身相同的視覺效果.但這當然意味著所有行和列計數和索引將保持不變.

What you can do is to remove the contents of a row or column, which will effectively have the same visual effect as removing the row or column itself. But this of course means that all row and column counts and indices will remain unchanged.

那么如何清除一行或一列(或單元格)的內容呢?不幸的是,這也并不像看起來那么容易.

So how can the contents of a row or column (or cell) be cleared? This unfortunately also isn't as easy as it might seem.

首先,您需要考慮您是否只想從布局中刪除小部件,或者您是否還希望它們被刪除.如果您只從布局中刪除小部件,則必須在之后將它們放回不同的布局中,或者手動為它們提供合理的幾何形狀.如果小部件也被刪除,它們將從 GUI 中消失.提供的代碼使用布爾參數來控制小部件刪除.

First, you need to think about if you only want to remove the widgets from the layout, or if you also want them to become deleted. If you only remove the widgets from the layout, you must put them back into a different layout afterwards or manually give them a reasonable geometry. If the widgets also become deleted, they will disappear from the GUI. The provided code uses a boolean parameter to control widget deletion.

接下來,您必須考慮到,布局單元不僅可以包含小部件,還可以包含嵌套布局,它本身可以包含嵌套布局,等等.您還需要處理跨越多行和多列的布局項.最后,還有一些行和列的屬性,比如最小寬度和高度,它們不依賴于實際內容,但仍然需要注意.

Next, you have to consider that a layout cell can not just only contain a widget, but also a nested layout, which itself can contain nested layouts, and so on. You further need to handle layout items which span over multiple rows and columns. And, finally, there are some row and column attributes like minimum widths and heights which don't depend on the actual contents but still have to be taken care of.

#include <QGridLayout>
#include <QWidget>

/**
 * Utility class to remove the contents of a QGridLayout row, column or
 * cell. If the deleteWidgets parameter is true, then the widgets become
 * not only removed from the layout, but also deleted. Note that we won't
 * actually remove any row or column itself from the layout, as this isn't
 * possible. So the rowCount() and columnCount() will always stay the same,
 * but the contents of the row, column or cell will be removed.
 */
class GridLayoutUtil {

public:

  // Removes the contents of the given layout row.
  static void removeRow(QGridLayout *layout, int row, bool deleteWidgets = true) {
    remove(layout, row, -1, deleteWidgets);
    layout->setRowMinimumHeight(row, 0);
    layout->setRowStretch(row, 0);
  }

  // Removes the contents of the given layout column.
  static void removeColumn(QGridLayout *layout, int column, bool deleteWidgets = true) {
    remove(layout, -1, column, deleteWidgets);
    layout->setColumnMinimumWidth(column, 0);
    layout->setColumnStretch(column, 0);
  }

  // Removes the contents of the given layout cell.
  static void removeCell(QGridLayout *layout, int row, int column, bool deleteWidgets = true) {
    remove(layout, row, column, deleteWidgets);
  }

private:

  // Removes all layout items which span the given row and column.
  static void remove(QGridLayout *layout, int row, int column, bool deleteWidgets) {
    // We avoid usage of QGridLayout::itemAtPosition() here to improve performance.
    for (int i = layout->count() - 1; i >= 0; i--) {
      int r, c, rs, cs;
      layout->getItemPosition(i, &r, &c, &rs, &cs);
      if (
          (row == -1 || (r <= row && r + rs > row)) &&
          (column == -1 || (c <= column && c + cs > column))) {
        // This layout item is subject to deletion.
        QLayoutItem *item = layout->takeAt(i);
        if (deleteWidgets) {
          deleteChildWidgets(item);
        }
        delete item;
      }
    }
  }

  // Deletes all child widgets of the given layout item.
  static void deleteChildWidgets(QLayoutItem *item) {
    QLayout *layout = item->layout();
    if (layout) {
      // Process all child items recursively.
      int itemCount = layout->count();
      for (int i = 0; i < itemCount; i++) {
        deleteChildWidgets(layout->itemAt(i));
      }
    }
    delete item->widget();
  }
};

這篇關于從 QGridLayout 中刪除小部件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 无线对讲-无线对讲系统解决方案-重庆畅博通信 | 领先的大模型技术与应用公司-中关村科金 | 环氧树脂地坪_防静电地坪漆_环氧地坪漆涂料厂家-地壹涂料地坪漆 环球电气之家-中国专业电气电子产品行业服务网站! | 金属回收_废铜废铁回收_边角料回收_废不锈钢回收_废旧电缆线回收-广东益夫金属回收公司 | 实验室pH计|电导率仪|溶解氧测定仪|离子浓度计|多参数水质分析仪|pH电极-上海般特仪器有限公司 | 减速机三参数组合探头|TSM803|壁挂式氧化锆分析仪探头-安徽鹏宸电气有限公司 | 广州迈驰新GMP兽药包装机首页_药品包装机_中药散剂包装机 | 儿童语言障碍训练-武汉优佳加感统文化发展有限公司 | PCB厂|线路板厂|深圳线路板厂|软硬结合板厂|电路板生产厂家|线路板|深圳电路板厂家|铝基板厂家|深联电路-专业生产PCB研发制造 | 对照品_中药对照品_标准品_对照药材_「格利普」高纯中药标准品厂家-成都格利普生物科技有限公司 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库 | 回收二手冲床_金丰旧冲床回收_协易冲床回收 - 大鑫机械设备 | 厌氧工作站-通用型厌氧工作站-上海胜秋科学仪器有限公司 | 加气混凝土砌块设备,轻质砖设备,蒸养砖设备,新型墙体设备-河南省杜甫机械制造有限公司 | 【法利莱住人集装箱厂家】—活动集装箱房,集装箱租赁_大品牌,更放心 | 丝印油墨_水性油墨_环保油墨油漆厂家_37国际化工 | 成都软件开发_OA|ERP|CRM|管理系统定制开发_成都码邻蜀科技 | 广东恩亿梯电源有限公司【官网】_UPS不间断电源|EPS应急电源|模块化机房|电动汽车充电桩_UPS电源厂家(恩亿梯UPS电源,UPS不间断电源,不间断电源UPS) | 青海电动密集架_智能密集架_密集架价格-盛隆柜业青海档案密集架厂家 | CNC机加工-数控加工-精密零件加工-ISO认证厂家-鑫创盟 | 低噪声电流前置放大器-SR570电流前置放大器-深圳市嘉士达精密仪器有限公司 | 海德莱电力(HYDELEY)-无功补偿元器件生产厂家-二十年专业从事电力电容器 | 【官网】博莱特空压机,永磁变频空压机,螺杆空压机-欧能优 | 济南网站建设|济南建网站|济南网站建设公司【济南腾飞网络】【荐】 | 杭州营业执照代办-公司变更价格-许可证办理流程_杭州福道财务管理咨询有限公司 | 等离子空气净化器_医用空气消毒机_空气净化消毒机_中央家用新风系统厂家_利安达官网 | 阿尔法-MDR2000无转子硫化仪-STM566 SATRA拉力试验机-青岛阿尔法仪器有限公司 | 铆钉机|旋铆机|东莞旋铆机厂家|鸿佰专业生产气压/油压/自动铆钉机 | 压装机-卧式轴承轮轴数控伺服压装机厂家[铭泽机械] | 集装箱箱号识别_自重载重图像识别_铁路车号自动识别_OCR图像识别 | 欧盟ce检测认证_reach检测报告_第三方检测中心-深圳市威腾检验技术有限公司 | 锡膏喷印机-全自动涂覆机厂家-全自动点胶机-视觉点胶机-深圳市博明智控科技有限公司 | 中医治疗皮肤病_潍坊银康医院「山东」重症皮肤病救治平台 | 海尔生物医疗四川代理商,海尔低温冰箱四川销售-成都壹科医疗器械有限公司 | 乐考网-银行从业_基金从业资格考试_初级/中级会计报名时间_中级经济师 | 主题班会网 - 安全教育主题班会,各类主题班会PPT模板 | 有声小说,听书,听小说资源库-听世界网| 吹塑加工_大型吹塑加工_滚塑代加工-莱力奇吹塑加工有限公司 | 深圳公司注册-工商注册公司-千百顺代理记账公司 | 儿童语言障碍训练-武汉优佳加感统文化发展有限公司 | 沥青车辙成型机-车托式混凝土取芯机-混凝土塑料试模|鑫高仪器 | BAUER减速机|ROSSI-MERSEN熔断器-APTECH调压阀-上海爱泽工业设备有限公司 |