Wt布局头疼

时间:2013-11-29 07:49:24

标签: c++ wt

我正在评估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 message\n\
to help illustrate the problem i am\n\     
having with the relative offsets of\n\     
    container widgets";                    

const char *BODY = "  A Proferred Official ICP\n\
\n\                                              
By Initial Connection Protocol (ICP), I mean a third level protocol\n\
which is initiated by a user process at one site in order to contact a\n\
server process at another site.  Typically, the user process will be a\n\
Telnet and the server process will be a logger, but there may be other\n\
cases.\n\                                                                
\n\                                                                      
In this RFC, I wish to describe a family of ICPs suitable for\n\         
establishing one pair of connections (one in each direction) between any\n\
user process and any server process, and to propose further a particular\n\
subset of this family as the standard ICP for connecting user processes\n\ 
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-address 0.0.0.0 --http-port 8080

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

没有任何帮助会不受欢迎。

1 个答案:

答案 0 :(得分:2)

Wt团队确认布局管理员存在错误。 http://redmine.webtoolkit.eu/issues/2488