在网格布局中删除控件

删除一个控件 布局间距变大了

优化后

//删除按钮 更新布局
void ScreenInfoArea::deleteBtn()
{
	int count = m_screenInfoVec.size();
	ScreenInfoBtn *selectedButton = nullptr;
	for (int i = count-1; i > 0; i--) {
		 selectedButton = m_screenInfoVec[i];	
			if(selectedButton && selectedButton->getChecked()){
				m_screenInfoVec.remove(i); 
				if (m_screenLayout) {
					m_screenLayout->removeWidget(selectedButton);
					m_screenLayout->update();
				}
				delete selectedButton;
				selectedButton = Q_NULLPTR;
			}		
	}
	count = m_screenInfoVec.size();
	updateLayout();
}
void ScreenInfoArea::updateLayout() {

	int count = m_screenInfoVec.size();
	int row = (count-1) / m_colNum;
	int col = (count-1)% m_colNum;
	int height = (row + 1) * 300 + (row + 2) * 32;
	int width = (col+1) * 226 + (col + 2) * 32;
	if (row > 0)
		width = this->width();
	m_screenWidget->setFixedSize(width, height);
	m_colNum = 10;
}
//添加按钮 更新布局
void ScreenInfoArea::updatePicAndAvi(int type, QString filePath)
{
	int count = m_screenInfoVec.size();
	int row = count / m_colNum;
	int col = count % m_colNum;
      //获取按钮类型
	ScreenType screentype;
	if (type == 0) {
		screentype = ScreenType::picture;
	}
	else {
		screentype = ScreenType::video;
	}
     
    ScreenInfoBtn *screenButton = new ScreenInfoBtn(screentype);
    //设置数据
	screenButton->setBackPix(filePath);
	m_screenInfoVec.push_back(screenButton);

    m_screenLayout->addWidget(screenButton,row,col,1,1,Qt::AlignLeft);

	m_screenWidget->setStyleSheet("background:#ffEBEB");
	//m_screenWidget->setStyleSheet("background:transparent;");
	int height = (row + 1) * 300 + (row +2)* 32;
	int width = (col + 1) * 226 + (col + 2) * 32;
	if (row > 0)
		width = this->width();
	m_screenWidget->setFixedSize(width, height);
	m_screenWidget->setLayout(m_screenLayout);
 
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值