Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is it possible to add some behavior at the beginning of Qt-slot or before?
Forum Updated to NodeBB v4.3 + New Features

Is it possible to add some behavior at the beginning of Qt-slot or before?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 4 Posters 424 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bduminyuk
    wrote on last edited by
    #1

    I use an auth form. Its implementation is not mine, so I cannot change it. The form has two fields for a login and password and also two buttons (ok + cancel).

    Let's imagine the authorize method looks like this (I replaced the code sending requests to a server with one condition inside the if statement):

    void AuthForm::on_btn_ok_clicked()
    {
        if (loginLineEdit->text() == "auth" && passwordLineEdit->text() == "auth")
            emit accepted();
        else
            emit rejected();
    }
    

    I can connect to signals AuthForm::accepted or AuthForm::rejected to get result of authorization. But the problem is the default implementation of AuthForm does not validate form fields. Before the program send requests to authorize the user I want it to check if the form fields are empty or if the login match a mask.

    It works:

    1. user clicks ok
    2. calling of on_btn_ok_clicked() immediately
    3. sending requests to get user logged in or not

    I want it to work:

    1. user clicks ok
    2. verifying if the login match mask and check if the fields are not empty
    3. using of default on_btn_ok_clicked() implementation

    I think it should be like a middle layer between signal and slot. But I don't know if it is possible and how to do it.

    So, is it possible to add some behavior at the beginning of Qt-slot not modifying it?

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You have to modify AuthForm to achieve what you want. How should it work otherwise? You don't have access to the ui elements outside AuthForm and even then you could not stop on_btn_ok_clicked() from being called without modifying the AuthForm code.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      3
      • M Offline
        M Offline
        mpergand
        wrote on last edited by
        #3

        You can listen to textChanged signal of lineEdit and enable/disable the button accordingly:

        ui->setupUi(this);
        
           connect(ui->loginLineEdit,&QLineEdit::textChanged,[this](const QString &text)
               {
               QPushButton* okBut=ui->buttonBox->button(QDialogButtonBox::Ok);
        
               // button OK enabled if the field is not empty
               if(text.isEmpty())
                   okBut->setEnabled(false);
               else
                   okBut->setEnabled(true);
        
               });
        
        1 Reply Last reply
        4
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi,

          Even shorter and just as clear:

          connect(ui->loginLineEdit,&QLineEdit::textChanged,[this](const QString &text)
             {
                 QPushButton* okBut=ui->buttonBox->button(QDialogButtonBox::Ok);
                 okBut->setDisabled(text.isEmpty());
             });
          

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved