gansm / finalcut

A text-based widget toolkit
https://github.com/gansm/finalcut/wiki/First-steps#first-steps-with-the-final-cut-widget-toolkit
GNU Lesser General Public License v3.0
981 stars 52 forks source link

How to acces FLineEdit text after event FFocusEvent #108

Closed wimstockman closed 1 year ago

wimstockman commented 1 year ago

Hi Gansm, I'm a bit stuck. I want to access a FLineEdit and put the content toUpper() after I tabbed out of it. I tried this but it won't work:

void onChildFocusOut (FFocusEvent* out_ev)
{

  const auto& focus = FWidget::getFocusWidget();
    if (focus->getClassName() == "FLineEdit"){ 
    ((FLineEdit)focus).setText("Test");
    this->redraw();
//  FString b = a->getText();
//  b.toUpper();
}

Can you help me. Thanks Wim

gansm commented 1 year ago

Hi Wim, I'm not sure what your intention is. onChildFocusOut() makes sense only if your widget has focusable child widgets. So I guess you are working with an FScrollView widget.

Unfortunately, I could not reproduce your problem:

#include <final/final.h>

class MyScrollview final : public finalcut::FScrollView
{
  public:
    explicit MyScrollview (finalcut::FWidget* parent = nullptr)
      : finalcut::FScrollView{parent}
    { }

  private:   
    void onChildFocusOut (finalcut::FFocusEvent* out_ev) override
    {
      const auto focus_widget = FWidget::getFocusWidget();

      if ( focus_widget->getClassName() == "FLineEdit" )
      {
        // Converts lowercase letters to uppercase
        const auto& text_field = static_cast<finalcut::FLineEdit*>(focus_widget);
        auto text = text_field->getText();
        text_field->setText(text.toUpper());
        this->redraw();
      }

      finalcut::FScrollView::onChildFocusOut(out_ev);
    }
};

auto main (int argc, char* argv[]) -> int
{
  // Application object
  finalcut::FApplication app(argc, argv);
  app.initTerminal();

  // Main dialog
  finalcut::FDialog dialog ("Focus out input field", &app);
  dialog.setGeometry ({25, 5}, {30, 16});

  // My own scrollview
  MyScrollview scrollview(&dialog);
  scrollview.setGeometry ({2, 1}, {26, 10});  // widget size
  scrollview.setScrollSize(finalcut::FSize{24, 14});  // scrollview size
  scrollview.useParentWidgetColor();
  scrollview.clearArea();

  // Adding input fields to the scroll view
  for (const auto& i : {0, 1, 2, 3, 4, 5, 6})
  {
    auto edit = new finalcut::FLineEdit(&scrollview);
    edit->setGeometry ({10, 1 + 2 * i}, {12, 1});
    edit->setLabelText("Value " + std::to_string(i));
  }

  // Add quit button to dialog
  finalcut::FButton quit_button("&Quit", &dialog);
  quit_button.setGeometry({18, 12}, {10, 1});
  quit_button.addCallback ("clicked", [&dialog] () { dialog.close(); } );

  app.setMainWidget(&dialog);
  dialog.show();
  return app.exec();
}

grafik

wimstockman commented 1 year ago

Thanks it was the "&" in the line const auto& focus = FWidget::getFocusWidget() That needed to be without the "&". ; I circumvented it with callbacks on every FLineEdit but this is way better. :-) By the end of the week I'll put my application on github. So maybe it will be an inspiration for others to use your framework. Kind regards, Wim Stockman