Qt6 topics merged with General

How to get info from one form to another

  • C++ provides a lot of obstacles for a noob, namely this noob. The latest brick wall I hit is my trying to use the value held in a MainWindow lineEdit on a Dialog form. I've tried this:

    ui->label_6->setText( MainWindow::ui->lineEdit_MyInt->text() );

    But since ui is a MainWindow private member, I get an error. I have to access the MainWindow lineEdit_MyInt (holds the cust_id of a customer selected in a tablewidget) so that I can write the the entered Dialog info to the correct customer. And, of course, I don't have to use label_6; that's just a visual aid as I try to sort this out.

    How can I access MainWindow's lineEdit_MyInt on the Dialog form?

  • Lifetime Qt Champion

    @Driftwood I guess you instantiate and call the dialog in your main window?
    In that case it is as simple as pass all needed data to the dialogs constructor:

    class MyDialog : public QDialog
        MyDialog(const QString text, ...) {...}
    // In MainWindow:
    MyDialog myDialog(ui->lineEdit_MyInt->text());

  • For sparsely used dialogs passing the data in the constructor as @jsulm mentioned, is adequate. For dialogs that are frequently displayed it is arguably more optimal to create the dialog early and use the signal slot mechanism to feed data to its fields, then exec() it when it is to be realized.

    setText() is a slot, albeit private in the conext of the ui...so subclass the dialog, making a public setText() slot for the fields you need to send data to, then connect that slot to the signals that will feed it data.

    As a noob, you must well learn the signal/slot mechanism to use QT effectively.

Log in to reply