c++ - Wt布局头痛

标签 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-地址 0.0.0.0 --http-端口 8080

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

任何帮助都不会被拒绝。

最佳答案

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

关于c++ - Wt布局头痛,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20280764/

相关文章:

c++ - 在 C++ 中创建字符串时出错

c++ - 未知语法 void (Type::m_function)()

c++ DLL\static 库名称问题

c++ - 我如何使用内置 Web 服务器部署 Wt 应用程序?

rest - 如何使用 Wt 启动并运行 Restful API

Wt 数据库 Wt::Dbo

c++ - C++ MFC SDI复制/粘贴无效

c++ - 在 Qt 中调整带有大图像的小部件的大小的常用习惯是什么?

c++ - 如何使用wt实现ajax请求处理?