【问题标题】:Refresh/reset link/WResource of WPushButton after click but before redirection单击后但在重定向之前刷新/重置链接/WPushButton 的 WResource
【发布时间】:2015-10-09 14:41:36
【问题描述】:

有没有办法在点击 WPushButton 之后但在浏览器被定向到链接或资源之前重置它的链接?

背景是我有一个 WResource,它复制项目/表格模型(派生自 WAbstractTableModel),以冻结它的状态/数据并基于它进行 csv 文件转换。因此,当单击“将当前状态下载为 csv”按钮时,我需要创建一个新的 WResource 并将按钮链接设置为它,然后触发整个重定向机制。我怎样才能做到这一点?

编辑:目前看来,Wt 无法实现这一点,请参阅 my this post in the official Wt forum

【问题讨论】:

    标签: wt


    【解决方案1】:

    您可以创建一个资源,该资源在创建时采用模型,然后在 handleRequest 方法中从中提取数据。

    一个例子:

    class TableResource : public WResource
    {
    public:
        TableResource(WStandardItemModel *model, WObject *parent) :
            WResource(parent),
            model(model)
        {
    
        }
    
        ~TableResource()
        {
            beingDeleted();
        }
    
        void handleRequest(const Http::Request &request, Http::Response &response)
        {
            std::string data;
    
            for (int i = 0; i < model->rowCount(); ++i)
            {
                std::string row;
    
                for (int j = 0; j < model->columnCount(); ++j)
                {
                    row.append(model->item(i, j)->text().toUTF8());
                    row.append(";");
                }
    
                data.append(row);
                data.append("\n");
            }
    
            response.setMimeType("text/plain");
            response.out() << data;
        }
    
    private:
        WStandardItemModel *model;
    };
    

    用法:

    WApplication* createApplication(const Wt::WEnvironment& env)
    {
        WApplication *app = new WApplication(env);
    
        WStandardItemModel *model = new WStandardItemModel(app);
        for (int i = 0; i < 10; ++i)
        {
            std::vector<WStandardItem*> items;
    
            for (int j = 0; j < 4; ++j)
            {
                WStandardItem *item = new WStandardItem;
                item->setText(WString("item {1} {2}").arg(i).arg(j));
                items.push_back(item);
            }
    
            model->appendRow(items);
        }
    
        WTableView *table = new WTableView(app->root());
        table->setModel(model);
    
        WPushButton *btn = new WPushButton(app->root());
        btn->setText("export");
        btn->setResource(new TableResource(model, btn));
    
        return app;
    }
    

    【讨论】:

    • 感谢您的尝试,但您忽略了我在问题中描述的基本问题:“我有一个 WResource 可以复制项目/表格模型......以冻结它的状态/数据”。这是因为用户可以更改模型,即使在随后的handleRequest 调用之间,由于Wt::WRessource 的并发性质。我目前通过 2 个按钮调用解决了这个问题,请参阅 redmine.webtoolkit.eu/boards/2/topics/…
    猜你喜欢
    • 1970-01-01
    • 2013-04-10
    • 2020-08-18
    • 1970-01-01
    • 2016-07-12
    • 2021-09-08
    • 2014-01-14
    • 2019-02-07
    • 2017-07-04
    相关资源
    最近更新 更多