删除qt小部件时删除空间

时间:2017-10-18 22:20:24

标签: c++ qt qt4

我有一个网格布局,其上有4个组框。当我尝试使用groupBox->hide()隐藏其中一个QGroupBox时,组框不再可见。但是空间保留在布局中(我使用QSizePolicy来保留空间)

如何确保其余小部件自动使用空间?

enter image description here

在图片中,我隐藏了'第2组'。我希望Group 3自动填满空间。我该怎么做?

1 个答案:

答案 0 :(得分:0)

In order for the layout to do what you want, its child widgets have to 'behave' correctly. I.e. if one of your group boxes contains a vertical spacer, it might mess up your layout.

I found that setting a stylesheet to the toplevel widget helps with debugging layout issues:

public class VisaTypeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    Context context;
    private List<VisaType> visaTypeList;

    int selected_position;


    public VisaTypeAdapter(Context context, List<VisaType> visaTypeList) {
        this.context = context;
        this.visaTypeList = visaTypeList;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.visa_type_row, parent, false);
        return new jp.workjapan.adapters.VisaTypeAdapter.ViewHolder(view);

    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final VisaType a = visaTypeList.get(position);
        final jp.workjapan.adapters.VisaTypeAdapter.ViewHolder viewHolder = (jp.workjapan.adapters.VisaTypeAdapter.ViewHolder) holder;
        viewHolder.visa_eng.setText(a.getName_en());
        viewHolder.visa_jp.setText(a.getName_jp());


        final long id = a.getId();
        if (id == AppSingleTon.visaType) {
            viewHolder.visa_layout.setSelected(true);
        } else {
            viewHolder.visa_layout.setSelected(false);
        }

        if (position == selected_position) {
            viewHolder.visa_layout.setSelected(true);
        } else {
            viewHolder.visa_layout.setSelected(false);
        }

        viewHolder.visa_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                selected_position = getAdapterPosition();
                notifyDataSetChanged();
            }
        });

    }

}

This will paint a red border around (some) widgets and clarifies who uses which space.

You could try to add a vertical spacer below the third group box to "push" them all upwards. If one of the group boxes is then set to invisible, the others should get pushed up and the space between group box 1 and 3 should disappear.