Important: Please read the Qt Code of Conduct -

[SOLVED] help getting signal and slot to work

  • I have signal and slot in my dialog.cpp file. the readyRead function that it refers to is at the mainwindow.cpp. how can i call the signal and slot function from outside of the class? Can i have an example please. thank you in advanced.

    also, i have socket defined in two places. at the mainwindow and dialog.cpp
    @socket = new QTcpSocket(this);@
    i think that it should only be defined at one place. how to tell qt to use socket from dialog for mainwindow? socket needs to be read at the mainwindow and dialog. how to only Instantiate it once while the second time it reads and writes to the server.

    @connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));@

    void MainWindow::readyRead(){

  • Hi kalster,

    that sounds a bit of a weird design.
    You want to have a main window which holds a dilog. The dialog has a socket as member. When the socket does something, the main widnow should access it and handle the socket?

    The connect si not the problem, if you have the pointer to the main window. Or you connect the sockets signal with a signal of the dialog (signal forwarding) and connect the dialogs signal with a slot in the main window.

    But I would not let the main window access a member of the dialog directly.

  • yes when the socket does something, the main window should access it and handle the socket

  • But if you want to access the socket in mainwindow, why do you put it to the dialog?
    Is it life time coupled to the dialog?
    Is it logically bnound to the dialog?
    Or is it more a business logic part which is used from both components?

  • the lineEdit in the dialog stores the username, i am having problems getting that username to the mainwindow. when the user clicks the ok button on the dialog then the mainwindow does all the sockets, but the time the dialog is opened, the mainwindow does the sockets stuff anyways which is too early to do and the result is i cant connect anymore. I basiclly need to delay the mainwindow from doing sockets until the user clicks the ok button on the dialog but i don't know how

    because i dont know how to get a variable from the dialog to the mainwindow, at the dialog open the socket then try to connect to the server. from there, the dialog is closed and the mainwindow does the rest. the problem is that the mainwindow does the rest before the dialog is closed.

  • So the problem is not to store a value for a later run with QSettings, your problem is to get a value that was entered in a dialog from the caller.

    That's pretty easy. Give your dialog a function gettext() and return the value of the edit field.
    Or store the value of the edit field in a variable in the virtual method QDIalog::accept()

  • I am kind of new to c++ programming. could you give me a working example please.

  • You could have something like that:
    @class LoginDialog : public QDialog
    explicit LoginDialog(QWidget *parent = 0);

    const QString login() const;
    const QString password() const;

    class QLineEdit *loginEdit, *passwordEdit;

    LoginDialog::LoginDialog(QWidget *parent)
    : QDialog(parent)
    , loginEdit(new QLineEdit(this))
    , passwordEdit(new QLineEdit(this))
    QFormLayout * form = new QFormLayout(this);
    form->addRow("Login", loginEdit);
    form->addRow("Password", passwordEdit);

    QDialogButtonBox *buttonBox =
            new QDialogButtonBox(QDialogButtonBox::Ok
                                 | QDialogButtonBox::Cancel,
            Qt::Horizontal, this);
    connect(buttonBox, SIGNAL(accepted()), SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), SLOT(reject()));


    const QString LoginDialog::login() const {
    return loginEdit->text();

    const QString LoginDialog::password() const {
    return passwordEdit->text();
    And you would use the class in your mainwindow function like that:
    @void MainWindow::askLoginInformation() {
    LoginDialog dialog(this);
    // Show the dialog and wait until the user clicks Ok/Cancel or closes it.
    if(dialog.exec() == QDialog::Accepted) {
    // The dialog was validated by the user
    QString login = dialog.login();
    QString password = dialog.password();

        // Do something with login and password
    } else {
        // The dialog was closed or the cancel button was clicked


  • thank you alexisdm. i will look at your code tonight

  • thanks for the code alexisdm.

Log in to reply