Wt布局令人头疼

Wt layout headache

本文关键字:布局 Wt      更新时间:2023-10-16

我正在评估Wt,直到遇到布局问题,我的代码似乎与浏览器中的内容不一致,这给我留下了深刻的印象。主要是因为容器(显然)不应该重叠。这里有一个例子来说明我的意思:

#include <stdio.h>            
#include <stdlib.h>           
#include <string.h>           
#include <assert.h>           
#include <string>
using namespace std;
#include <Wt/WApplication>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>       
#include <Wt/WHBoxLayout>     
#include <Wt/WVBoxLayout>     
#include <Wt/WGroupBox>       
#include <Wt/WLabel>          
#include <Wt/WDateEdit>       
#include <Wt/WStackedWidget>  
#include <Wt/WMenu>           
#include <Wt/WBootstrapTheme> 
#include <Wt/WTabWidget>      
using namespace Wt;
class TSTApplication : public WApplication
{                                         
        WContainerWidget *body;           
    public:
        TSTApplication(const WEnvironment& env);
};                                              
class TSTConcoction : public WContainerWidget
{                                            
    public:                                  
        TSTConcoction();                     
};                                           
TSTConcoction::TSTConcoction()
{                             
    WVBoxLayout *vbox = new WVBoxLayout;
    WGroupBox *gbox = new WGroupBox("A Group Box");
    WTabWidget *tabw = new WTabWidget(gbox);       
    tabw->setStyleClass("tabwidget");              
    // tab1:
    {       
    WContainerWidget *tab = new WContainerWidget;
    WHBoxLayout *hbox = new WHBoxLayout;
    hbox->addWidget(new WLabel("First:"));
    hbox->addWidget(new WLineEdit);       
    hbox->addWidget(new WLabel("Second:"));
    hbox->addWidget(new WLineEdit);        
    hbox->addWidget(new WLabel("Third:")); 
    hbox->addWidget(new WLineEdit);        
    tab->setLayout(hbox);
    tabw->addTab(tab, "TAB1",  Wt::WTabWidget::PreLoading);
    }                                                      
    // tab2:
    {       
    WContainerWidget *tab = new WContainerWidget;
    WHBoxLayout *hbox = new WHBoxLayout;
    hbox->addWidget(new WLabel("Only:"));
    hbox->addWidget(new WLineEdit);      
    tab->setLayout(hbox);
    tabw->addTab(tab, "TAB2",  Wt::WTabWidget::PreLoading);
    }                                                      
    vbox->addWidget(gbox);
    // towards the bottom
    gbox = new WGroupBox("Below The Lot");
    WHBoxLayout *hbox = new WHBoxLayout;  
    hbox->addWidget(new WLabel("Left:"));
    hbox->addWidget(new WDateEdit);      
    hbox->addWidget(new WLineEdit);      
    hbox->addWidget(new WLabel("Middle:"));
    hbox->addWidget(new WDateEdit);        
    hbox->addWidget(new WLineEdit);        
    hbox->addWidget(new WLabel("Right:"));
    hbox->addWidget(new WLineEdit);       
    gbox->setLayout(hbox);
    vbox->addWidget(gbox);
    setLayout(vbox);
}                   
TSTApplication::TSTApplication
(                             
    const WEnvironment& env   
)                             
: WApplication(env)           
{                             
    WApplication::instance()->setTheme(new WBootstrapTheme);
    WContainerWidget *appcont = new WContainerWidget;
    WStackedWidget *contents = new WStackedWidget;   
    WMenu *menu = new WMenu(contents, Wt::Horizontal, appcont);
    menu->addStyleClass("nav-tabs");                           
    // tab1
    WContainerWidget *cont = new WContainerWidget;
    WVBoxLayout *vbox = new WVBoxLayout;          
    WHBoxLayout *hbox = new WHBoxLayout;
    WLabel *l = new WLabel("Dummy:");   
    hbox->addWidget(l);                 
    hbox->addWidget(new WLineEdit);     
    vbox->addLayout(hbox);              
    vbox->addWidget(new TSTConcoction);
    cont->setLayout(vbox);
    menu->addItem("TAB1", cont);
    // tab2
    cont = new WContainerWidget;
    vbox = new WVBoxLayout;
    vbox->addWidget(new WLabel("TODO"));
    cont->setLayout(vbox);
    menu->addItem("TAB2", cont);
    appcont->addWidget(contents);
    root()->addWidget(appcont);
}
WApplication *createApplication(const WEnvironment& env)
{
    return new TSTApplication(env);
}
int main(int argc, char **argv)
{
    return WRun(argc, argv, &createApplication);
}

这里是另一个(看似无关)的例子:

#include <stdio.h>                                                          
#include <stdlib.h>                                                         
#include <string.h>                                                         
#include <assert.h>                                                         
#include <string>
using namespace std;
#include <Wt/WApplication>
#include <Wt/WBreak>      
#include <Wt/WContainerWidget>
#include <Wt/WPushButton>     
#include <Wt/WText>           
#include <Wt/WVBoxLayout>     
#include <Wt/WLabel>          
#include <Wt/WStackedWidget>  
#include <Wt/WMenu>           
#include <Wt/WBootstrapTheme> 
using namespace Wt;
class TSTApplication : public WApplication
{                                         
        WContainerWidget *body;           
        void start();
    public:
        TSTApplication(const WEnvironment& env);
};                                              
class TSTMessage : public WContainerWidget
{                                         
    public:                               
        TSTMessage();                     
};                                        
class TSTRelatives : public WContainerWidget
{                                           
        void expand();                      
    public:
        TSTRelatives(const string &);
};                                   
class TSTChildren : public TSTRelatives
{                                      
    public:                            
        TSTChildren();                 
};                                     
class TSTParent : public TSTRelatives
{                                    
    public:                          
        TSTParent();                 
};                                   
const char *MESSAGE = "This is a messagen
to help illustrate the problem i amn     
having with the relative offsets ofn     
    container widgets";                    
const char *BODY = "  A Proferred Official ICPn
n                                              
By Initial Connection Protocol (ICP), I mean a third level protocoln
which is initiated by a user process at one site in order to contact an
server process at another site.  Typically, the user process will be an
Telnet and the server process will be a logger, but there may be othern
cases.n                                                                
n                                                                      
In this RFC, I wish to describe a family of ICPs suitable forn         
establishing one pair of connections (one in each direction) between anyn
user process and any server process, and to propose further a particularn
subset of this family as the standard ICP for connecting user processesn 
to loggers on systems which accept teletype-like devices.";                
TSTMessage::TSTMessage()
{                       
    WVBoxLayout *vbox = new WVBoxLayout;
    vbox->addWidget(new WText("<pre>" + string(MESSAGE) + "</pre>"));
    vbox->addWidget(new TSTParent);                                  
    vbox->addWidget(new TSTChildren);                                
    vbox->addWidget(new WText("<pre>" + string(BODY) + "</pre>"));   
    vbox->addWidget(new WPushButton("Dummy Button"));                
    setLayout(vbox);                                                 
}                                                                    
TSTRelatives::TSTRelatives
(                         
    const string &who     
)                         
{                         
    setLayoutSizeAware(true);
    WVBoxLayout *vbox = new WVBoxLayout;
    WPushButton *expand = new WPushButton("Expand " + who);
    expand->clicked().connect(this, &TSTRelatives::expand);
    vbox->addWidget(expand);                               
    setLayout(vbox);                                       
}                                                          
void TSTRelatives::expand()
{                          
    clear();               
    for (int i = 0; i < 2; i++)
    {                          
        this->addWidget(new WBreak);
        WString num = WString("{1}<hr/>").arg(i);
        this->addWidget(new WText(num));         
        this->addWidget(new TSTMessage);         
    }                                            
}                                                
TSTChildren::TSTChildren()
: TSTRelatives("Children")
{                         
    setStyleClass("children");
}                             
TSTParent::TSTParent()
: TSTRelatives("Parent")
{                       
    setStyleClass("parent");
}                           
void TSTApplication::start()
{                           
    for (int i = 0; i < 3; i++)
    {                          
        body->addWidget(new WBreak);
        WString num = WString("{1}<hr/>").arg(i);
        body->addWidget(new WText(num));         
        body->addWidget(new TSTMessage);         
    }                                            
}                                                
TSTApplication::TSTApplication
(                             
    const WEnvironment& env   
)                             
: WApplication(env)           
{                             
    WApplication::instance()->useStyleSheet("tstui.css");
    WApplication::instance()->setTheme(new WBootstrapTheme);
    WContainerWidget *appcont = new WContainerWidget;
    WStackedWidget *contents = new WStackedWidget;   
    WMenu *menu = new WMenu(contents, Wt::Horizontal, appcont);
    menu->addStyleClass("nav-tabs");                           
    // tab1
    WContainerWidget *cont = new WContainerWidget;
    WVBoxLayout *vbox = new WVBoxLayout;          
    body = new WContainerWidget;                  
    WPushButton *b = new WPushButton("Start");    
    b->clicked().connect(this, &TSTApplication::start);
    vbox->addWidget(b);                                
    vbox->addWidget(body);                             
    cont->setLayout(vbox);
    menu->addItem("TAB1", cont);
    // tab2
    cont = new WContainerWidget;
    vbox = new WVBoxLayout;
    vbox->addWidget(new WLabel("TODO"));
    cont->setLayout(vbox);
    menu->addItem("TAB2", cont);
    appcont->addWidget(contents);
    root()->addWidget(appcont);
}
WApplication *createApplication(const WEnvironment& env)
{
    return new TSTApplication(env);
}
int main(int argc, char **argv)
{
    return WRun(argc, argv, &createApplication);
}

加载时,在页面周围任意点击展开父级/子级按钮,在几次迭代中,一些不需要的容器重叠悄悄出现。

要使它们运行,请将每个示例放入一个文件(例如tst.cpp)中,并使用(在linux上)进行编译:

c++-o tst tst.cpp-lwthttp-lwt

并运行:

/tst—docroot--http地址0.0.0.0--http端口8080

通常在这一点上,人们可能会想继续评估下一个框架,但我知道C++和Qt,所以Wt非常适合,我希望我能找到一个简单的解释来继续我的Wt之旅。

没有人会反对任何帮助。

被Wt团队确认为布局管理器中的一个错误。http://redmine.webtoolkit.eu/issues/2488