Problem with dialog child



  • Hi all,
    i've done this little project to explain my problem.

    when i exec a QDialog i connect a LineEdit returnPressed to a PushButton (click) but the slot clicked() run twice when the return in the line edit is pressed.

    i did the same in the main window that work correctly but not in the executed dialog
    [0_1515019274947_PROVA.zip](Uploading 100%)

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <dialog.h>
    #include <QDebug>
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        qDebug()<< "test ";
        qDebug()<< "dsdas";
        Dialog dia(this);
        dia.exec();
    }
    
    
    #include "dialog.h"
    #include "ui_dialog.h"
    #include <QDebug>
    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::on_pushButton_clicked()
    {
        qDebug()<< "test dialog ";
        qDebug()<< "dsdas";
    }
    
    

    ![alt text](0_1515019130078_85bcdd81-87e5-4032-9333-4083b30fe6f0-immagine.png image url)



  • Do you mean that "test dialog" output was prompted twice?



  • Yes, the on_pushbutton_clicked is called twice



  • @Pantof

    Can you share the output, please?



  • this happens when only one return in line edit is pressed:
    ![alt text](0_1515023968801_30c94d4e-5d5e-4dcc-b465-d4aa33c2f9cd-immagine.png image url)



  • I try another way with the same result:

    
    Dialog::Dialog(QWidget *parent) :
        QDialog(parent),
        ui(new Ui::Dialog)
    {
        ui->setupUi(this);
        QObject::connect(ui->lineEdit,&QLineEdit::returnPressed,this,&Dialog::onReturnPressed);
        QObject::connect(ui->pushButton, &QPushButton::clicked, this, &Dialog::onReturnPressed);
    }
    
    Dialog::~Dialog()
    {
        delete ui;
    }
    
    void Dialog::onReturnPressed()
    {
        qDebug()<< "test dialog ";
        qDebug()<< "dsdas";
    }
    
    

    it seems there is a problem between signal click() and clicked()



  • It's normal, a dialog has a defaut button, when pressing enter, the defaut button emits the clicked signal.

    see: default button



  • @Pantof

    Can you share the declarations (.h) of your classes?

    In the while, can you try to develop this project without using the Design to see what happens?


  • Qt Champions 2016

    Hi
    I agree with @mpergand and also thinks its the dialog helping.

    Pressing enter in line edits triggers this

    QObject::connect(ui->lineEdit,&QLineEdit::returnPressed,this,&Dialog::onReturnPressed);
    However, the lineedit do not eat the enter. its send to button.
    the button is "default" (when created with Designer at least) meaning pressing enter
    trigger it and it emits
    QObject::connect(ui->pushButton, &QPushButton::clicked, this, &Dialog::onReturnPressed);



  • I don't understand what the problem here is.

    There are two separate signals going on here.

    • You have clicked RETURN in a QLineEdit, and you have chosen to catch that signal.

    • Since QLineEdit, unlike QTextEdit, is single-line, it does not accept RETURN. So it passes it on to the QDialog to handle. Which (as @mpergand said) has a default button, which is invoked when RETURN is pressed. That only happens in a dialog, not a window.

    What is the issue? The obvious question is why you are interested in handling QLineEdit::returnPressed() signal at all?



  • you're all right sorry for ignorance,
    i'm intrested in QLineEdit::returnPressed because with this dialog a user could insert more than 1 record in database and he could do it with returnPressed from QLineEdit.
    i think i should do it in another way :D



  • @Pantof
    Nothing to be sorry for!

    i'm intrested in QLineEdit::returnPressed because with this dialog a user could insert more than 1 record in database and he could do it with returnPressed from QLineEdit.

    What do you mean by this?

    It sounds a little odd, but if you have a legitimate reason it may be that the better solution is to prevent that RETURN from causing the default dialog action. The principle QDialog is working on is that a RETURN --- unless it is intended for the current widget --- is aimed at the dialog. That sounds like it may not correspond to what you are intending.



  • @JonB
    normally the application show show a table from database when you click a pushbutton you open a Form (done with qDialog) that give you the possibility to insert new records. to speed up this job i use the return pressed from qLineEdit so you shouldn't move out to grab the mouse.
    actually i switch to qWidget.



  • @Pantof

    i use the return pressed from qLineEdit

    If you choose to do that, I would think you need to not let that key/return press to do it's normal action of activating the dialog's default button action. Such as, you could use event filter on the dialog perhaps. Or, I imagine a QDialog can be told not to have a default button/return press.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.