简体   繁体   English

如果有可用的位置,如何在布局中添加小部件? 否则不加

[英]How to add widget in layout if there is available place for it? Otherwise not to add

In my program I must show information (MarketWidgets with different size) divided in some pages (MarketWidgetsList that hold list of MarketWidgets). 在我的程序中,我必须显示信息(具有不同大小的MarketWidgets),该信息划分为某些页面(包含MarketWidgets列表的MarketWidgetsList)。 My first approach was to add widgets while there is available place for that widget, and if there isn't place another page must be created and add widget to that page. 我的第一种方法是在有可用小部件的地方添加小部件,如果没有放置小部件,则必须创建另一个页面并将小部件添加到该页面。 Is it an acceptable approach? 这是可以接受的方法吗? To get widget's size I'm using QWidget::sizeHint() because the widget still isn't painted, but it seems not correct. 为了获得小部件的大小,我使用QWidget::sizeHint()因为小部件仍未绘制,但似乎不正确。 Can sizeHint return not exact size if I'm using stretch in layout? 如果我在布局中使用拉伸,sizeHint是否可以返回不正确的尺寸?
Is there a way to add widget in layout if layout can show widget normally otherwise somehow indicate that there is not enough space? 如果布局可以正常显示小部件,是否有办法在布局中添加小部件,否则以某种方式表明没有足够的空间?

code example 代码示例

bool  Ui::DynamicMarketsWidget::add_group_market_widget(Ui::GroupMarketWidget *dynamic_group_market_widget)
{ 
    Q_ASSERT(dynamic_group_market_widget); 
    int tmp_height = dynamic_group_market_widget->sizeHint().height();                                     if(m_available_height > tmp_height) 
    { 
        m_market_layout->addWidget(dynamic_group_market_widget); 
        m_available_height -= tmp_height; 
        return true; 
    } 
    return false; 
}

The method sizeHint() returns the 'optimal' or 'desired' size of a widget. sizeHint()方法sizeHint()窗口小部件的“最佳”或“所需”大小。 If a widget is assigned to a layout it does not necessarily have this size. 如果将窗口小部件分配给布局,则它不一定具有此大小。 The size the widget currently actually has can be obtained by calling the methods size() , width() or height() . 当前小部件实际具有的大小可以通过调用方法size()width()height()

You could try to update your m_available_height with this value instead of the value obtained by the size hint method. 您可以尝试使用此值而不是通过大小提示方法获得的值来更新m_available_height Just make sure you assign your widget first before reading the size. 只需确保在读取尺寸之前先分配您的小部件即可。

You could try something like this: 您可以尝试这样的事情:

bool  Ui::DynamicMarketsWidget::add_group_market_widget(Ui::GroupMarketWidget *dynamic_group_market_widget)
{ 
    Q_ASSERT(dynamic_group_market_widget); 
    int tmp_height = dynamic_group_market_widget->sizeHint().height();                                     if(m_available_height > tmp_height) 
    { 
        m_market_layout->addWidget(dynamic_group_market_widget); 
        m_available_height -= dynamic_group_market_widget->height(); 
        return true; 
    } 
    return false; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM